After discussing algorithms and pseudocode, let us move beyond simple programs, and learn to build more complex programs, of which control structures are an integral part.
Flow of Program
Flow of Program
•Sequential execution
–Statements are
normally executed in the order in which they are written
•Transfer of control
–You can
specify, for example, Next statement to be
executed IS NOT next one in sequence
Control Structures
•
There are 3 control structures;
–
Sequence structure
•
Programs executed sequentially by default
–
Selection structures
•
if, if/else, switch
–
Repetition structures
•
while, do/while, for
•
C++ keywords
–
Cannot be used as identifiers or variable names
There are
only two ways of combining control structures;
•
Control Statement Stacking
–
One control structure is followed by another
control structure
–
Control Statement Nesting
–
One control structure is contained in another
control structure
Sequence Structures
•Simplest of all
•This one is in place by default
if no other control structure has been defined to alter the flow of your
program
•Key Rule :
Execute instructions one by one in the order they appear in the source code
Selection Structures
Key Rule :Choose
among alternative courses of actions
•Three selection statements
available in C++
–Single
Selection Statement :if
•Selects/ignores
a single action
–Double
Selection Statement :if/else
•Selects 1 out
of 2 actions
–Multiple
Selection Statement : switch
•Selects some
out of many actions
'if' Selection Structure
• Pseudocode
example:
If
student’s grade is greater than or equal to 60
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
Remember: Select OR ignore a single action
• Translation into C++
If student’s grade is greater than or equal to
60
Print “Passed”
if ( grade >= 60 )
cout << "Passed";
cout << "Passed";
• Diamond symbol (decision symbol)
– Indicates decision is to be made
– Contains an expression that can be true or false
• Test the condition and follow one of the two
paths (True/False)
'if/else' Selection Structure
•
Pseudocode Example:
if student’s grade is greater
than or equal to 60
print “Passed”
print “Passed”
else
print “Failed”
Remember: Select 1 out of 2 actions
•
C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
cout << "Passed";
else
cout << "Failed";
•
Conditional operator (?:)
–
The only ternary operator in
C++
–
Operates on 3 operands
–
Three Operands are (condition, value if true,
value if false)
•
Nested if/else structures
–
One inside another, test for multiple cases
–
Once condition met, other statements skipped
if student’s grade is greater than
or equal to 90
Print “A”
Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
•
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";
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";
•
Compound statement/ Block
–
Set of statements within a pair of braces
if ( grade >= 60 ) {
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
–
Without
braces,
cout << "You must take this course
again.\n";
Will always be executed
Dangling 'else' Problem
Remember : C++ compiler always associates an ‘else’ with the immediately preceding ‘if’
Post a Comment