0% found this document useful (0 votes)
52 views21 pages

Chapter 4: Control Flow in C++

Chapter Four covers control flow statements in programming, specifically focusing on selection and repetition statements in C++. It explains sequential execution, the use of selection statements like 'if' and 'switch', and details various forms of the 'if' statement, including nesting. The chapter emphasizes how flow control allows programmers to dictate the execution path based on conditions.

Uploaded by

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

Chapter 4: Control Flow in C++

Chapter Four covers control flow statements in programming, specifically focusing on selection and repetition statements in C++. It explains sequential execution, the use of selection statements like 'if' and 'switch', and details various forms of the 'if' statement, including nesting. The chapter emphasizes how flow control allows programmers to dictate the execution path based on conditions.

Uploaded by

samisha1913
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter Four

Control Flow Statement


(Selection and Repetition Statements)
Contents:
• Overview
• Sequential Statements
• Selections Statements
• Repetition Statements
• Continue and Break Statements
4.1 Over view
• Flow of the program:
• The order in which statements in a program are executed.
• Flow control:
▪ Programmers can control which instruction to be executed in a program.
• Flow control in a program is typically sequential, from one statement to the next.
• But we can also have execution that might be divided to other paths by
branching statements.
• Or perform a block of statement repeatedly until a condition fails by Repetition or
looping.
Cont…

• C++ statements
▪ Sequential
▪ Selection
▪ Repetition
4.1.1 Sequential Statements

• Program will executed one after the other in the order scripted in
the program.

• The order will be determined during program development and


cannot be changed.
4.1.2 Selection Statements

• Program will decide at runtime whether some part of the code should or
should not be executed.

• There are two types of selection statements in C++:


▪ if statement, and
▪ switch statement
[Link] The if statement
• It is sometimes desirable to make the execution of a statement
dependent upon a condition being satisfied.
• The different forms of the ‘if ” statement will be used to decide whether to
execute part of the program based on a condition which will be tested either
for:
• TRUE, or
• FALSE result.
Cont…

• The different forms of the “If ” statement are:


▪ The simple if statement
▪ The if else statement
▪ The if else if statement
i. The simple if statement
• The simple if statement will decide only one part of the program to be
executed:
▪ if the condition is satisfied, or
▪ ignored if the condition fails.
• The General Syntax is:
if (expression)
statements;
ii. The if else statement
• Another form of the “if ” is the “if …else” statement. The “if else if ”
statement allows us to specify two alternative statements:
• One which will be executed if a condition is satisfied and
• Another which will be executed if the condition is not satisfied.
Cont…
• The General Syntax is:
if (expression)
statements1;
else statements2;
• First “expression” is evaluated and if the outcome is none zero (true), then
“statements1” will be executed.
• Otherwise, which means the “expression” is false “statements2” will be executed.
iii. The if else if statement

• The third form of the “if ” statement is the “if …else if ” statement.
• The “if else if ” statement allows us to specify more than two
alternative statements each will be executed based on testing one or more
conditions.
Cont…
• The General Syntax is:
if (expression1)
statements1;
else if(expression2)
statements2;
.
.
.
else if(expressionN)
statementsN;
else statements
iv. Nesting if statement within another
statement
• One or more if statements can be nested with in another if
statement.

• The nesting will be used to test multiple conditions to perform a task.


• It is always recommended to indent nested if statements to enhance
readability of a program.
• The General Syntax might be:
if (expression1)
{
if (expression2)
statementsN;
else
statementsM;
}
else
{
if (expression3)
statementsR;
else
statementsT;
}
Cont…
• StatementsN will be executed if and only if “expression1” and “expression2”
are evaluated and if the outcome of both is none zero (TRUE).
• StatementsM will be executed if and only if “expression1” is TRUE and
“expression2” is FALSE.
• StatementsR will be executed if and only if “expression1” is FALSE and
“expression3” is TRUE.
• StatementsT will be executed if and only if “expression1” is FLASE and
“expression2” is FALSE.

You might also like