- Generally speaking, a program
executes statements as they appear
in the source code, one line after another.
To change that order is to alter the program's flow.
One of the most common flow control words
in the C language is "If", which is similar
to its human language counterpart, also called if.
In this movie, I introduce you to the if keyword.
You'll also discover how comparisons
are made in the C language, as well
as how to handle multiple decisions.
Before you can use the if keyword,
you must understand how comparisons work in C.
Two values or expressions are evaluated.
To make that evaluation, you use one
of the C language's comparison operators.
Here are the most common comparison operators.
"Is equal to," which, you'll notice, two equal signs.
One equal sign is the assignment operator.
This is why I say, "Is equal to," for the operator's name
instead of "is equal" or "equals."
"Less than" and "greater than"
you're probably familiar with.
Then, there's "less than and equal to"
and "greater than and equal to."
Finally, the "not equal to" or "does not equal" operator.
The exclamation point in the C language
is often pronounced "not," which is
how you remember this one.
The user is prompted to type an integer value.
The value is displayed in line nine.
In line 10, and if statement evaluates that value.
If it's greater than 10, line 11 is executed.
Otherwise, line 11 is skipped.
Build and run.
Let's type in 50.
You typed in 50, 50 is greater than 10, it worked.
I'm going to experiment a few times
as you should with other values to see how it works.
Seven, we've typed seven, and of course,
there's no other output.
And ten.
But values greater than 10 will display that string.
An if statement is traditionally formatted
differently from other statements.
It's split between two or more lines.
The first line sets the if condition in parenthesis.
If that condition is evaluated true,
the next line is executed.
Actually, it's just one statement split between two lines.
See how the semi-colon is at the
end of line 11 and not line 10.
You could also right it as a
single line, but that's uncommon.
More common is to enclose the statements
belonging to if in curly brackets, like this.
This format is actually required when more than one
statement is executed based on the if condition.
For example, let's move line nine down to line 12.
Save, build and run.
We type in 25, and because the value is greater
than 10, both lines were displayed.
Open exercise file if2.
This code shows two if comparisons in a row,
each of which has its own set of curly brackets.
Two conditions are evaluated.
Greater than, in line 10, and less than at line 14.
Build and run.
You see the appropriate output for values less than 10.
Run again.
You see appropriate output for values greater than 10.
And run again.
But not output for 10 itself.
Edit line 14, so that less than
or equal to is used as the operator.
You can remember the order of how the symbols go
if you say, "less than or equal to,"
or "greater than or equal to."
Edit line 16 to reflect the same results.
Save, build and run.
Now we can type 10.
Another way to handle an either or decision,
is to use the keyword else.
Load exercise file ifelse.
This code is a rewrite of exercise if2,
but it uses the else keyword.
The if condition at line 10 makes an evaluation.
Its statements are executed when that condition is true.
When the condition is false, the statements
belonging to else are executed instead.
Build and run this code.
Let's try it with 10, that's true.
Run it again, that's true.
Note that else doesn't have a semi-colon.
Instead, it has curly brackets.
Or, it could be written without brackets
because it's only followed by one statement.
When three or more conditions are present,
you can evaluate them by using an else if structure.
Open exercise ifelseifelse to see what that looks like.
This structure starts with an if statement here at line 10.
When that condition is true, those statements
belonging to that condition are executed,
and the rest is skipped over.
Otherwise, a second condition,
else if, is examined at line 14.
If that condition is true, its statements are executed
and the rest of the structure is skipped.
At the end of the structure is an else all by itself.
Its statements are executed when
the preceding conditions aren't met.
Build and run the code.
The results are similar to previous examples.
15 is greater than 10, run again.
10 is 10, run again.
Five is less than 10.
You can have as many else if
statements stacked up as you like,
which helps evaluate complex situations.
Finally, a reminder, the if, else if
and else keywords are generally not
immediately followed by a semi-colon.
In the C language, a semi-colon by itself is a statement.
When one lingers after an if evaluation,
the compiler believes that the semi-colon
is the statement, and that not anything follows it.
This is a common mistake.
Load exercise if3, build and run.
Well, obviously, negative five is not a positive number.
Now if you're using the Clang compiler,
you can click on the Build Log tab
and review a specific error message.
Other compilers may not be as smart.
To fix the problem, simply remove
the semi-colon dangling at the end of line 7.
Save, build and run.
No output means that the negative five fails the condition.
Remember, the semi-colon goes after the statement,
not the parenthesis that hold if's condition.