1.3.2 Basic If/Else Statements
Conditional Statements in C++ work exactly the same as Java and similar to other languages:
If Statements
If statements only execute code if the condition is true, otherwise it skips that block of code.
Else Statement
Else statements only execute when the if
condition is false. With an if
/else
combination, one of the coding blocks will always execute, but never both.
Else If Statements
With an else if
clause, the code will evaluate the else if
clause only when the previous if statements are negative. If it evaluates to true, the coding block will be executed.
Summary
Below is a summary of the options you can use with if
statements.
if (condition)
Required to start conditional block and can only have one
else if (condition)
Optional and can have as many as needed; Follows if
statement and before else
clause
else
Optional and can have only one; must be the last clause and contain no conditional statement.
Try This Example
Last updated