0% found this document useful (0 votes)
38 views46 pages

DFC20113 - CHAPTER 3 (PROGRAM CONTROL STRUCTURES)

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)
38 views46 pages

DFC20113 - CHAPTER 3 (PROGRAM CONTROL STRUCTURES)

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
You are on page 1/ 46

CHAPTER 3

PROGRAM CONTROL STRUCTURE


DFC20113
PROGRAMMING FUNDAMENTALS
Course Learning Outcome
Show simple programs by
developing code to solve
Implement programming problems in a computer
element and articulate how using C++ programming
they are used to achieve a language.
working program. ( P2, PLO 3 )
( C3, PLO 2 ) Page 02
LEARNING OUTCOMES
at the end of this sub-chapter, students should be able
to:

Describe control structure


3.1 Describe the approaches can be chosen
Understand depending on problem statement:
program control a) Sequential
structure b) Selection
c) Iterational (Repetition)

Page 03
Introduction
A program is usually not limited to a linear sequence of
instructions.
During its process it may bifurcate, repeat code or take
decisions.
For that purpose, C++ provides control structures that
serve to specify what has to be done by our program, when
and under which situations.
Program control typically involves executing particular code
based on the outcome of a prior operation or a user input.
FLOWCHART FOR TYPES OF CONTROL
STRUCTURE

Page 05
LEARNING OUTCOMES
at the end of this sub-chapter, students should be able
to: Identify the selection control structure statement:
‘if’ statement
‘switch’ statement
Explain the ‘if’ statement type:
Simple if statement
Else statement
3.2 Else if statement
Solve problem using Write an algorithm for each ‘if’ statement type and code into a program based on
a problem statement
selection control Differentiate the assignment (=) and equality (==) operator.
structures Apply the nested if.
Describe the switch case statement
Identify the syntax for switch case statement
Describe the uses of break statement
Convert the nested if statement to switch
Page 03
Solve a given problem by writing algorithm, convert into a program, run, test and
debug using selection statement.
Selection Control Structure

It allows instructions to be executed in a non-


sequential ways.
The user can choose what he want to
process.
It compares two expressions.
Based on the comparison, it takes a certain
course of action.
Page 06
Selection

If Switch

Single if
Types of
Selection Control Structure
if-else

Nested if Page 07
Selection Control Structure
Used to execute a set of statements
when the given condition is satisfied.
Conditional statements within the
block are executed when the condition The if statements written within the
in the if statement is satisfied. body of another if statement to test
multiple conditions is called nested if.

Executes the set of statements in if block,


when the given condition is satisfied.
Executes the statements in the else block,
when the condition is not satisfied. Page 08
Selection Control Structure

Page 09
Page 10
Page 10
Page 10
Switch Statement
Is a multi-way selection statement and contains various case
statements.
The case statements are executed based on the value of the
expression.

Page 05
Switch Statement
(Expression)
Data type must be INTEGER
or
CHAR only

Page 08
Page 10
BREAK Statement
Using break we can leave a loop even if the condition
for its end is not fulfilled.
It can be used to end an infinite loop, or to force it to
end before its natural
In looping, it causes the //code (next slide) to be skipped
and terminates the loop.
In switch case statements, break causes the remaining
cases to be skipped--it prevents "falling through" to
other cases.
Page 09
In the program code, the while loop will run,
as long i is smaller then 20. In the while
loop there is an if statement that states
that if i equals 10 the while loop must stop
(break). The result is that only 10 Hello will Page 10
be printed.
switch if-else Page 10
Programming Exercise
1. Based on the program given, convert it to switch-case statement.

Page 08
Programming Exercise

2. Write a C++ program to accept name, registration number and


CGPA from user. Then display all the information.

3. Write a C++ program to get 3 marks from user. Then calculate


the total mark and average. If average > 50 display ‘You are
excellent’.

4. Write a C++ program to input total salary from employee. If total


salary > 2000, calculate the 10% income tax and display total salary
after tax. Otherwise just display total salary. Page 08
LEARNING OUTCOMES
at the end of this sub-chapter, students should be able
to: Describe the Iterational (repetitive) control structure
Identify the structure of loops statement:
•While, Do while, For
3.3 Summarize the difference between ‘while’ and ‘do while’
Solve problems Describe the advantage of for loops
using loops Identify the components and structure of the loops:
control structure •Initialization, Termination-condition, Increment – step
Apply the control transfer statements.
Quitting the loops – break statement
Continuing the loops – continue statement Page 03

Terminating the program using exit() function


Loops Control Structure

Loops are used to repeat a block of code

Looping statements are used to execute a set


of instructions repeatedly, as long as the
specific condition is satisfied.

Page 06
Structure of loops statement
Loop condition

condition tested condition tested


first later

Page 07
Structure of loops statement
condition tested
Loop condition
first

condition tested Page 07

latest
Types of loops
From one number to another
number and increases by a specified
value each time Loops are useful for things
that want to loop at least
once.

The while loop can be used if


we don’t know how many
times a loop must run. Page 08
for loops statement
Syntax:
for(initialization; condition; increment/decrement)
{
statement;
}

Example:
for (i=0; i<5; i++)
{
cout << “\n” << i ;
}

Page 09
for loops statement
Program code Loop condition

Flow chart Page 07


for loops statement

Page 09
Structure of loops statement
while
Loop condition

do-while Page 07
while loops statement

Syntax: Example:
initialization; int i = 0;
while (condition) while (i<5)
{ {
statement; cout << “\n” << i;
increment/decrement; i++;
} }

Page 09
while loops statement

Page 09
do-while loops statement
Syntax: Example:
initialization; int i = 0;
do do
{ {
statement; cout << “\n” << i;
increment/decrement; i++;
} while (i<5);
}while (condition);

Page 09
do-while loops statement

Page 09
Nested for loops statement

Page 06
Nested for loops statement

Page 09
Differences between while and do..while
statements

Page 09
Control transfer statement
Program code
Loop condition
continue
The conti
nue statem
provides a ent
convenient
jump to th way to
e next of
body for t the loop
he current
•It is some iteration.
times nece
skip some ssary to
statement
/s inside
the loop.
Page 07
Control transfer statement
Program code
Loop condition
exit()
The exit(
) function
a program also ends
before its
terminatio normal
n. It requi
Standard res the
Library he
stdlib.h. T ad er file,
he format
is:
exit(value
where val );
ue is an in
teger
variable o
r value. Page 07
Differences between BREAK, CONTINUE and EXIT() statements

Page 09
break statement

Page 09
continue statement

Page 09
exit() statement

Page 09
Programming Exercise

1.Write a program that prints the even number


between number 1 to 20 by using loops control
structures.

2.Write a program that enables user to input 5


hobbies by using while loops control structures.
Page 08
Programming Exercise

3. Write a program in C++ to get input one number from user. The
program will repeat until user entered 0. Below is the example output:

Page 08
THANK
YOU
Page 15

You might also like