0% found this document useful (0 votes)
63 views11 pages

03 Control Statements in C++

Control statements in C allow programmers to control the flow of program execution. There are two main types of control statements: selection statements (if, if-else, else-if, switch) and iteration statements (while, do-while, for). Selection statements allow choosing between optional sections of code based on conditions, while iteration statements allow repeating important sections. The if statement executes code if a condition is true, if-else executes one code block if true and another if false, and else-if provides multiple conditions to check. The switch statement allows selecting between multiple constant cases.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views11 pages

03 Control Statements in C++

Control statements in C allow programmers to control the flow of program execution. There are two main types of control statements: selection statements (if, if-else, else-if, switch) and iteration statements (while, do-while, for). Selection statements allow choosing between optional sections of code based on conditions, while iteration statements allow repeating important sections. The if statement executes code if a condition is true, if-else executes one code block if true and another if false, and else-if provides multiple conditions to check. The switch statement allows selecting between multiple constant cases.

Uploaded by

Raza Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Control Statements in C

Selection statement and Iteration Statements


Control Structures
• A program consists of a number of statements which are usually
executed in sequence. Programs can be much more powerful if we
can control the order in which statements are run.

• Statements fall into three general types;


1. Assignment, where values, usually the results of calculations, are stored in
variables.
2. Input / Output, data is read in or printed out.
3. Control, the program makes a decision about what to do next.
Control Statement Continued…
Control statements in C++ are Control
statements

used to write powerful programs Selection Iteration


by; Statements statements

The if else The while loop &


statement Do while loop

1. Selecting between optional The switch


sections of a program. statements
The for loop

2. Repeating important sections of The break

the program. statement

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

if student’s grade is greater than or equal to 60


print “Passed”
else
print “Failed”
Syntax:
• C++ code if ( condition )
{
if ( grade >= 60 ) }
statement 1 ; (if the condition is true this statement will be executed)

cout << "Passed"; else


{
else statement 2 ; (if the condition is false this statement will be executed)
cout << "Failed"; }
Else-If Structure
• Nested if/else structures • Example
One inside another, test for multiple cases
Once condition met, other statements skipped
if ( grade >= 90 ) // 90 and above
if student’s grade is greater than or equal to 90
Print “A” cout << "A";
else else if ( grade >= 80 ) // 80-89
if student’s grade is greater than or equal to 80 cout << "B";
Print “B”
else
else if ( grade >= 70 ) // 70-79
if student’s grade is greater than or equal to 70 cout << "C";
Print “C” else if ( grade >= 60 ) // 60-69
else
if student’s grade is greater than or equal to 60 cout << "D";
Print “D” else // less than 60
else cout << "F";
Print “F”
FALSE TRUE
Test Condition_1 Exec. Stat_1

FALSE
TRUE

Control Flow of else-if Test Condition_2 Exec. Stat_2

FALSE TRUE
Test Condition_3 Exec. Stat_3

FALSE TRUE
Test Condition_n

Exec. Stat_X Exec. Stat_n


Compound statement
• 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";
always executed
• Block
Set of statements within braces
Switch Statement
• The control statements which
allow us to make a decision from switch (expression)
the number of choices is called {
case constant 1:
switch (or) Switch-case simple statement (or)
statement. compound statement;
case constant 2:
• It is a multi way decision simple statement (or)
statement, it test the given compound statement;
case constant 3:
variable (or) expression against a simple statement (or)
list of case value. compound statement;
}
Rules for Switch Statement
 The expression in the switch statement must be an integer or character constant.
 No real numbers are used in an expression.
 The default is optional and can be placed anywhere, but usually placed at end.
 The case keyword must be terminated with colon (:);
 No two case constant are identical.
 The values of switch expression is compared with case constant in the order specified i.e from
top to bottom.
 The compound statements are no need to enclose within pair of braces.
 Integer Expression used in different case statements can be specified in any order.
 A switch may occur within another switch, but it is rarely done. Such statements are called as
nested switch statements.
 The switch statement is very useful while writing menu driven programs.

You might also like