The C++ IF Statement Part 2 Copyright © 2004-2012 Curt Hill Not much left should be merged into 1 or 3 Copyright © 2004-2012 Curt Hill
What else is there to know? More forms of the if? More forms of the condition? More Boolean information? Copyright © 2004-2012 Curt Hill
Two forms of if statement if (boolean) statement if (boolean) statement else statement Copyright © 2004-2012 Curt Hill
The Else There is no THEN like in Pascal or BASIC The else is optional The statement to do if condition is true immediately follows the parenthesized condition The else is optional Else is a reserved word and signals the start of the statement to execute if the condition is false The if without an else means else do nothing Copyright © 2004-2012 Curt Hill
Compound statements Either the then statement or the else statement may be replaced with a compound statement In any combination Even the compound statement of an if may create local variables Their scope is confined to the compound statement Copyright © 2004-2012 Curt Hill
Example Revisited Let’s redo the quadratic formula program Copyright © 2004-2012 Curt Hill