Lecture 4A of Programming Language.

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Introduction to Information

Technology
(IIT)
Lecture 4A
C++ Control Structures

Instructor: Dr. Komal Nain Sukhia


komal.sukhia93@gmail.com
2

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

• There are 3 control structures;


– Sequence structure
• Programs executed sequentially by default
– Selection structures
• if, if/else, switch
– Repetition structures
• while, do/while, for
4

Control Structures

• C++ keywords
– Cannot be used as identifiers or variable names
C ++ Ke yw o rd s

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
5

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
6

Lets look at the three control structures


one by one
7

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
8

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

– Multiple Selection Statement : switch


• Selects some out of many actions
9

‘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


10

‘if’ Selection Structure

• Translation into C++


If student’s grade is greater than or equal to 60
Print “Passed”

if ( grade >= 60 )
cout << "Passed";

 
11

‘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)

if
true
grade >= 60 print “Passed”
Performs action
if condition true
false
12

‘if/else’ Selection Structure

• 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";

Remember: Select 1 out of 2 actions


13

‘if/else’ Selection Structure

false true
grade >= 60

print “Failed” print “Passed”

If/else
Performs
different actions
if condition
true/false
14

‘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)
15

‘if/else’ Selection Structure

• Code could be written:


cout << ( grade >= 60 ? “Passed” : “Failed” );

Condition Value if true Value if false

• Could also be written as :


grade >= 60 ? Cout << “Passed” : cout << “Failed” ;

Condition Value if true Value if false


16

‘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”
17

‘if/else’ Selection Structure

• 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

Dangling ‘else’ Problem

Remember : C++ compiler always associates an


‘else’ with the immediately preceding ‘if’
if ( x > 5 )
Will be
if ( y > 5 ) interpreted
cout << “x and y are both > 5"; as
else
cout << “x is <= 5";

if ( x > 5 )
if ( y > 5 )
cout << “x and y are both > 5";
else
cout << “x is <= 5";
19

‘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

You might also like