Section 9.1 if
Statements
We are ready for a really important and powerful new concept: the ability to create computer instructions that are executed only if certain conditions are met. Let’s take a look.
If you cannot see this codecast, please click here .
Video Description.
Branching using the if
statement is introduced
Branching can be used to execute code only in the case that some condition is true (or false)
NEW SYNTAX: if (something is true)
{ do this };
will check if the statement in parenthesis is true, and if so, will execute the statement(s) within curly brackets
Check Your Understanding Check Your Understanding
1.
Which of the following shows the correct syntax for an
if
statement?
if (cloudy) printf("It's not sunny :-(");
Correct
printf("It's not sunny :-(") if (cloudy);
Not quite - try again!
if (cloudy) printf("It's not sunny :-(")
Not quite - try again!
if cloudy printf("It's not sunny :-(");
Not quite - try again!
Hint .
Review the video if you are stumped.