Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

DBT 1201 PROGRAMMING

CONCEPTS

Peter Mackenzie petnjau@gmail.com


CHAPTER SIX:
FLOW CONTROL STRUCTURES
CONTROL STRUCTURE

 Control Structures –is a Statements used to control


the flow of execution in a program.
 However, various C statements enable the programmer
to specify that the next statement to be executed might
not be the next one in sequence.
 A transfer of control occurs when a statement other
than the next one in the program executes.
 Normally, statements in a program execute one after
the other in the order in which they are written. This is
called sequential execution.
TYPES OF CONTROL STRUCTURES

 Types of control structures :


1. Sequential control structure
2. Selection/Decision control structure
3. Iteration control structures
SEQUENTIAL CONTROL STRUCTURES

 The sequential construct


means the statements are
being executed sequentially
in the sequence in which
they are written in the
program.
DECISION/SELECTION CONTROL STRUCTURE

 The selection construct means the execution of


statement(s) depending upon the condition-test.
 If a condition evaluates to true, a course-of-action (a
set of statements) is followed otherwise another
course-of-action is followed.
 This construct is also called decision construct as
it helps in decision making.
SELECTION CONTROL STRUCTURE

 Types of selection controls structures:


1. If….
2. If...else…
3. If…else if…
4. Select case
SELECTION CONTROL STRUCTURE

a) If….
 An If… statement tests a particular condition; if
the condition evaluates to true, a course-of-
action is followed otherwise it is ignored.
 Syntax :
If (Boolean expression) {
statements
}
EXAMPLE

 A C program for displaying the message


“You are an adult” if the age typed by the
user is greater than or equals to18.
OUTPUT WHEN VALUE >=18
OUTPUT WHEN VALUE <18
2. IF…ELSE STATEMENT
 This structure executes one block of statement
if the condition is True and another block if the
condition is False.
 If…Else statement provides an alternate choice
to the user
 i.e. if the condition is true then a set of
statements are executed otherwise another set
of statements are executed.
CONT…

 Syntax
If (Boolean Expression) {
Statement(s)
}
else{
Statement(s)
}
EXAMPLE

 A C program for displaying the message “You


are an adult” if the age typed by the user is
greater than or equals to18 and a message
“You are a teenager” if the age typed is
less than 18.
OUTPUT WHEN VALUE >=18
OUTPUT WHEN VALUE <18
3) IF….ELSE IF

 You can test for more than two choices.


 For example, what if we wanted to test for
more age ranges, say 19 to 39, and 40 and
over?
 For more than two choices, the IF … ELSE
IF statement can be used.
THE STRUCTURE OF AN IF … ELSE IF IS THIS:

if ( condition_one ) {
statements
}
else if ( condition_two ) {
statement
}
else {
statements
}
EXAMPLE

 A C program for displaying the message “Your


are a child” if the age typed by the user is less
than or equals to12 and a message “You are a
teenager” if the age typed is greater than 12
and less than 18. and the message “Your are a
youth” if the age is between 18 and 35. also
“You are adult” if the age is greater than 35.
EXERCISE
 Write a C program is for grading a student based on the following
criteria and the marks are entered by user:-
Average Mark Remarks
70 to 100 Distinction
60 to 69 Credit
50 to 59 Pass
0 to 49 Fail
Any other Invalid
SWITCH STATEMENT

 A switch statement allows a variable to be


tested for equality against a list of values.
 Each value is called a case, and the variable
being switched on is checked for each switch
case.
CONT….
 Syntax
 The syntax for a switch statement in C programming language
is as follows:
CON..
 The following rules apply to a switch statement:
 The expression used in a switch statement must have an
integral or enumerated type, or be of a class type in which
the class has a single conversion function to an integral or
enumerated type.
 You can have any number of case statements within a switch.
Each case is followed by the value to be compared to and a
colon.
 The constant-expression for a case must be the same
data type as the variable in the switch, and it must be a
constant or a literal.
CONT.…
 When the variable being switched on is equal to a case, the
statements following that case will execute until a break statement
is reached.
 When a break statement is reached, the switch terminates, and the
flow of control jumps to the next line following the switch
statement.
 Not every case needs to contain a break. If no break appears, the
flow of control will fall through to subsequent cases until a break is
reached.
 A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is
needed in the default case.
EXAMPLE

 A C program for displaying the message “Your


are a child” if the age typed by the user is less
than or equals to12 and a message “You are a
teenager” if the age typed is greater than 12
and less than 18. and the message “Your are a
youth” if the age is between 18 and 35. also
“You are adult” if the age is greater than 35.
ITERATION CONTROL STRUCTURES

 These structures allow for execution of one or


more lines of code repetitively.
 A loop structure is used to execute a certain set of
actions for a predefined number of times or until a
particular condition is satisfied.
Types of iteration control structures:
1. For…..loop
2. While loop
3. Do….. while
1. FOR… LOOP
 This type of statement is used when the user knows in
advance how many times the loop is going to be executed.
 This loop uses a variable (counter) that increases or
decreases in value during each repetition of the loop.
 The structure of the For Loop is this
CONT…..
 Here is the flow of control in a for loop:
1. The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables.You are not required to put a statement
here, as long as a semicolon appears.
2. Next, the condition is evaluated. If it is true, the body of the loop is executed.
If it is false, the body of the loop does not execute and flow of control jumps to
the next statement just after the for loop.
3. After the body of the for loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop
control variables. This statement can be left blank, as long as a semicolon appears
after the condition.
4. The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the for loop terminates
EXAMPLE

 Write C program for listing the numbers


between 1 and 10 using the for loop.
OUTPUT
EXERCISE

 Write a C program for generating a


multiplication table of all the numbers between
1 and 9 using the for loop
EXERCISE

 Write a C program that will generate the


following pattern using the for loop
OUTPUT
EXERCISE

1. Write a C program for listing all even


numbers between 1 and 20 using the for
loop
2. Write a C program for displaying numbers
between 20 and 1 using the for loop
WHILE LOOP
 while loop is an entry controlled loop in which the
condition is placed at the entry point.
 This control executes the statements specified in the body of
the loop till the condition evaluates to true.
 The loop may not be executed at all the if the condition is
initially false.
EXAMPLE

 Write C program for listing the numbers


between 1 and 10 using the while loop.
OUTPUT
EXERCISE

1. Write a C program for listing all even


numbers between 1 and 20 using the while
loop
2. Write a C program for displaying numbers
between 20 and 1 using the while loop
DO… WHILE LOOP
 Do… While is an exit controlled loop as the condition is
placed at exit point.
 The body of the loop is going to be executed at least once
whether the condition evaluates to true or false.
 Loop is executed as long as the result of the condition remains
true.
 Syntax :
do{

}while(condition);
CONT…

 Unlike for and while loops, which test the loop


condition at the top of the loop, the do...while loop
in C programming language checks its condition at
the bottom of the loop.
 A do...while loop is similar to a while loop, except
that a do...while loop is guaranteed to execute at
least one time.
EXAMPLE

 Write a C program for listing the numbers


between 1 and 10 using the do….while
loop.
OUTPUT
EXERCISE

1. Write a C program for listing all even


numbers between 1 and 20 using the
do…while loop
2. Write a C program for displaying numbers
between 20 and 1 using the do…while loop
Next Topic
ARRAY, POINTERS & STRUCTURES

You might also like