IF Statements: Should I Put On Sunglasses When It's Sunny?

  • Thread starter Thread starter Travian
  • Start date Start date
AI Thread Summary
The discussion centers on understanding the use of IF statements in programming, specifically in the context of dressing based on weather conditions. When it is raining, the algorithm specifies that only a hat and shoes are worn. Conversely, when it is sunny, the algorithm indicates that sunglasses, a hat, and shoes are worn. The key point is that the IF statement only affects the code within its block, while the surrounding code remains unaffected. This clarification confirms that both conditions lead to the wearing of a hat and shoes, with sunglasses added only for sunny weather.
Travian
Messages
64
Reaction score
0

Homework Statement


I have a question regarding IF statements.

Here's an algorithm:

put on hat ;
IF (weather is sunny)
Put on sunglasses ;
ENDIF
Put on shoes ;

What items of clothing will be put on a) when it is raining and b) when it is sunny?



Homework Equations





The Attempt at a Solution



a) hat, shoes.
b) sunglasses, hat, shoes.

What i want to know is:

if its raining then obviously i put on a hat and shoes. Now if it's sunny (like indicated in the IF statement), then not only i wear sunglasses, but hat and shoes as well?
 
Physics news on Phys.org
right. an if statement doesn't obliterate code surrounding it :)

It works like this: the code from the if to the end if is conditional. If the condition is true, the code within the if block executes. Otherwise, it skips it. Everything else in the program remains unchanged.

You can THINK of it like this (though i wouldn't ever put it like this on a test or anything. It's just to get the point across):

if rainy the code becomes:
put on hat ;
Put on shoes ;
and if sunny, the code becomes:
put on hat ;
Put on sunglasses ;
Put on shoes ;
 
Thank you for confirmation:)
 
Back
Top