0% found this document useful (0 votes)
2 views44 pages

Dfc20303 - Chapter 3 (Program Control Structure)

This document covers programming fundamentals with a focus on program control structures in C++. It details three main types of control structures: sequence, selection, and repetition, along with their definitions, syntax, and examples. Additionally, it includes learning outcomes and programming exercises to reinforce understanding of these concepts.

Uploaded by

Siva Kumar
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)
2 views44 pages

Dfc20303 - Chapter 3 (Program Control Structure)

This document covers programming fundamentals with a focus on program control structures in C++. It details three main types of control structures: sequence, selection, and repetition, along with their definitions, syntax, and examples. Additionally, it includes learning outcomes and programming exercises to reinforce understanding of these concepts.

Uploaded by

Siva Kumar
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/ 44

DFC20303

PROGRAMMING
FUNDAMENTALS
CHAPTER 3
PROGRAM CONTROL STRUCTURE
01 05
COURSE LEARNING PROGRAMMING
OUTCOME EXERCISES

TABLE OF 02
TYPES OF PROGRAM

CONTENT
CONTROL STRUCTURE

03 SELECTION CONTROL
STRUCTURES

04
REPETITION CONTROL
STRUCTURES
COURSE LEARNING
OUTCOMES
CLO 1:
Apply programming elements and articulate how they are
used to achieve a working program. (C3, PLO2)

CLO 2:
Demonstrate appropriate algorithm based on well-defined
problem using C++ programming language. (P3, PLO3)
02
TYPES OF CONTROL
STRUCTURE
LEARNING OUTCOMES
3.1 Understand program control structure
Describe control structure
Describe the approaches can be chosen depending on problem statement:
a) Sequential
b) Selection
c) Iterational (Repetition)
TYPES OF PROGRAM CONTROL STRUCTURE
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.

Three types of program control structure:


a) Sequence
b) Selection
c) Repetition
TYPES OF PROGRAM CONTROL STRUCTURE
SEQUENCE
This control structure represents the linear and sequential execution of code, where statements are
executed one after another in the order they appear. This is the simplest type of programming control
structure and forms the foundation of most programs.

SELECTION
This control structure enables a program to choose between two or more paths of execution based on
specific conditions. Selection structures include conditional statements such as if-else and switch-case,
which use Boolean expressions to evaluate the conditions and execute the appropriate block of code.

REPETITION
This control structure allows certain blocks of code to be executed repeatedly as long as a
condition remains true. Iteration structures include loops like the for loop, while loop, and do-while
loop.
FLOWCHART FOR TYPES OF CONTROL STRUCTURE

Repetition
Sequence Selection
03
SELECTION CONTROL
STRUCTURES
LEARNING OUTCOMES
3.2 Solve problem using selection control structures
Identify the selection control structure statement:
a. if statement
b. switch case statement
Explain the ‘if’ statement type:
a. if statement
b. switch case statement
Identify the syntax for each ‘if’ statement type
Write program for each ‘if’ statement type based on a problem statement
Describe the switch case statement
Identify the syntax for switch case statement
Describe the use of break statement
Write program using switch case statement.
Write program to convert ‘if’ statement to switch case statement vice versa.
SELECTION CONTROL STRUCTURE
DEFINITION
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.

Types of selection
IF STATEMENTS
Used to execute a set of statements when the
given condition is satisfied. Conditional
Nested if
statements within the block are executed when The if statements written within the body of
the condition in the if statement is satisfied. another if statement to test multiple
conditions is called nested if.
Single 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.

if-else
IF STATEMENTS SYNTAX
SINGLE IF
IF ELSE
NESTED IF
SWITCH STATEMENTS
DEFINITION
Is a multi-way selection
statement and contains various
case statements.
The case statements are
executed based on the value of
the expression.
SWITCH STATEMENTS

(Expression)
Data type must be INTEGER
or
CHAR only
SWITCH STATEMENTS
BREAK STATEMENTS
DEFINITION

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 to be skipped and terminates the loop.

In switch case statements, break causes the remaining cases to be skipped and it prevents
"falling through" to other cases.
BREAK STATEMENTS

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 be printed.
CONVERT IF STATEMENT TO SWITCH STATEMENT
PROGRAMMING EXERCISES
1. Based on the program given, convert it to 2. Write a C++ program to get 3 marks from user. Then
switch-case statement. calculate the total mark and average. If average > 50
display ‘You are excellent’.

3. 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.
04
REPETITION CONTROL
STRUCTURE
LEARNING OUTCOMES
3.3 Solve problems using loops control structure Identify the syntax for each loops statement type
Compare the difference among each loops statement
Identify the structure of the loops statement: Write simple programme for each type of loop
a. While statement
b. Do while Explain the jump statements
c. For a. break
Explain the components of loops statement: b. continue
a. Initialization c. exit()
b. Termination Compare the break, continue and ‘exit’ statements
c. Increment or decrement Write simple program using any jump statement
REPETITION 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.
REPETITION CONTROL STRUCTURE
condition tested
first

condition tested
latest
REPETITION CONTROL STRUCTURE
From one number to another number do while
and increases by a specified value
each time
Loops are useful for things that want to loop
at least once.

for
The while loop can be used if we
don’t know how many times a loop
must run.

while
FOR LOOP STATEMENT
Syntax:
for(initialization; condition; increment/decrement)
{
statement;
}

Example:
for (i=0; i<5; i++)
{
cout << “\n” << i ;
}
FOR LOOP STATEMENT
Program Code

Flow Chart
WHILE LOOP STATEMENT
Syntax: Example:
initialization; int i = 0;
while (condition) while (i<5)
{ {
statement; cout << “\n” << i;
increment/decrement; i++;
}
}
WHILE LOOP STATEMENT
Program Code

Flow Chart
DO WHILE LOOP STATEMENT
Syntax: Example:
initialization; int i = 0;
do do
{ {
statement; cout << “\n” << i;
increment/decrement; i++;
}while (i<5);
}while (condition);
DO WHILE LOOP STATEMENT
Program Code

Flow Chart
NESTED FOR LOOP STATEMENT

A loop within another


loop is called a nested
loop.
NESTED FOR LOOP STATEMENT
Suppose we want to loop through each day of a week for 3 weeks.
To achieve this, we can create a loop to iterate three times (3
weeks). And inside the loop, we can create another loop to iterate 7
times (7 days).
WHILE VS DO WHILE STATEMENTS
JUMP STATEMENT
Program Code
continue
The continue s
provides a ta tement
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.
JUMP STATEMENT
Program Code
exit()
The exit(
) function
a program also ends
before its
terminatio normal
n. It requi
Standard res the
Library he
ader file,
stdlib.h. T
he format
is:
exit(value
where val );
ue is an in
teger
variable o
r value.
BREAK VS CONTINUE VS EXIT() STATEMENTS
BREAK VS CONTINUE VS EXIT() STATEMENTS

break exit()

continue
05
PROGRAMMING
EXERCISE
PROGRAMMING EXERCISES
1. Write a program that prints the even number between number 1 to 20 by using for loops control
structures.
2. Write a program that enables user to input 5 hobbies by using while loops control structures.
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:
THANK YOU

You might also like