03 Control Statements in C++
03 Control Statements in C++
Continue
statement
Goto statement
If Selection Statement
• Selection structure
• Choose among alternative courses of action Syntax:
• Pseudocode example: if ( condition )
{
If student’s grade is greater than or equal to 60
statement ;
Print “Passed” }
• If the condition is true
• Print statement executed, program continues to next statement
• If the condition is false
• Print statement ignored, program continues
• Indenting makes programs easier to read
• C++ ignores whitespace characters (tabs, spaces, etc.)
If Structure
• Translation into C++
If student’s grade is greater than or equal to 60 if ( grade >= 60 )
Print “Passed” cout << "Passed";
• if structure
• Single-entry/single-exit A decision can be made on
any expression.
zero - false
true nonzero - true
grade >= 60 print “Passed”
Example:
3 - 4 is true
false
If-Else Structure FLOW Chart
• if
• Performs action if condition true False True
Test Condition
• if/else
• Different actions if conditions true or false
• Pseudocode Executable Y - Statement Executable X - Statement
FALSE
TRUE
FALSE TRUE
Test Condition_3 Exec. Stat_3
FALSE TRUE
Test Condition_n