Lecture 4A of Programming Language.
Lecture 4A of Programming Language.
Technology
(IIT)
Lecture 4A
C++ Control Structures
Flow of Program
• Sequential execution
– Statements are normally executed in the order in which they
are written
• Transfer of control
– You can specify Next statement to be executed IS
NOT next one in sequence
3
Control Structures
Control Structures
• C++ keywords
– Cannot be used as identifiers or variable names
C ++ Ke yw o rd s
Control Structures
1. Sequence Structure
• Simplest of all
2. Selection Structures/Statements
– Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
if ( grade >= 60 )
cout << "Passed";
11
if
true
grade >= 60 print “Passed”
Performs action
if condition true
false
12
• Pseudocode Example:
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
false true
grade >= 60
If/else
Performs
different actions
if condition
true/false
14
• Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
18
if ( x > 5 )
if ( y > 5 )
cout << “x and y are both > 5";
else
cout << “x is <= 5";
19
– Without braces,
cout << "You must take this course again.\n";
Will always be executed