Download CONTROL STRUCTURES IN C++ and more Slides C programming in PDF only on Docsity!
Fundamentals of Programming
(FOP)
Lecture 5
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
Control Structures
- (^) C++ keywords
- (^) Cannot be used as identifiers or variable names C ++ Keywords auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while
Control Structures
There are only 2 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
1. Sequence Structure
- (^) 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
2. Selection Structures/Statements
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
: switch
- (^) Selects some out of many actions
‘if’ Selection Structure
- (^) Translation into C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed";
‘if’ Selection Structure
- (^) 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) true false grade >= 60 print “Passed” if Performs action if condition true
false true print “Failed” print “Passed” grade >= 60 If/else Performs different actions if condition true/false
‘if/else’ Selection Structure
‘if/else’ Selection Structure
- (^) Conditional operator (?:)
- (^) The only ternary operator in C++
- (^) Operates on 3 operands
- (^) Three Operands are (condition, value if true, value if
false)
‘if/else’ Selection Structure
- (^) 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” 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”
‘if/else’ Selection Structure
- (^) Example if ( grade >= 90 ) // 90 and above cout << "A"; else if ( grade >= 80 ) // 80- cout << "B"; else if ( grade >= 70 ) // 70- cout << "C"; else if ( grade >= 60 ) // 60- cout << "D"; else // less than 60 cout << "F";
‘if/else’ Selection Structure
- (^) 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"; }
- (^) Without braces, cout << "You must take this course again.\n";
Will always be executed