Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: IF statement

  1. Aug 26, 2010 #1
    1. The problem statement, all variables and given/known data
    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?



    2. Relevant equations



    3. 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?
     
  2. jcsd
  3. Aug 26, 2010 #2
    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 ;
     
  4. Aug 26, 2010 #3
    Thank you for confirmation:)
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook