What is the if/else control statement?
The if/else control statement is a group of code that tells the program to execute some code (and not others) based on a condition
Why is the if/else control statement important?
- it allows a program to have logic and branching
- that means: it allows your program to handle different scenarios
- a program without branching is like a train that:
- starts at Honolulu
- ends at Waipahu
- a program with branching is like a train that:
- starts at Honolulu
- pauses at Pearl City and thinks about where to go
- it could end up in Mililani
- it could end up in Waipahu
How to use the if/else control statement?
A group of code that includes:
- a
condition
- a block of code that is executed when the
condition is true
- a block of code that is executed when the
condition is false
- the
false block of code is optional
- when there's no
false block of code, then the control statement is called the if control statement
Syntax:
- the keywords are
if and else
- the
if keyword starts the if/else control statement, if (condition)
- the condition follows the
if keyword
- the condition is wrapped in parenthesis,
(condition)
- the
true and false blocks of code are wrapped in curly braces, { /*true block of code*/ }
- the
else keyword is between the ending curly braces of the true block and the starting curly braces of the false block, { /* true block */ } else { /* false block */ }
- read more at http://www.tutorialspoint.com/cprogramming/if_else_statement_in_c.htm