Control Flow Structures - Conditional Control Structures
Control Flow Structures - Conditional Control Structures
Facilitators
Mr. Luyima Alex Cedric
aluyima@ndejjeuniversity.ac.ug
0772 302775 / 0708 045 305
3.1 Introduction
The order in which statements are executed in a program is called a control flow. Control structures
form the basic entities of a structured programming language. C, C++ or Java are all structured
programming languages. Control structures are used to alter the flow of execution of the program.
So, why do we need to alter the program flow?
The reason why we alter program flow is predominantly to aid “decision making“. In life, we may be
given with a set of options like doing “Electronics” or “Computer science”. We do make a decision by
analyzing certain conditions (like our personal interest, scope of job opportunities etc.). With the
decision we make, we alter the flow of our life’s direction. This is exactly what happens in a C program.
Control structures combine instructions into logical units. A Logical unit has one entry point and one
exit point.Control flow is a term routinely used in control structures. Control flow refers to the order in
which programming statements in a program are executed. There are three types of control structures
available in C
• Sequence Control Structures
• Conditional / Selection Control Structures
• Repetition Control Structures
if (condition)
statement;
It starts with the keyword if, followed by a condition (a logical expression) enclosed within parenthesis,
followed by the result statement.
The resulting statement is executed if the condition is evaluated as TRUE. Note that there is no semicolon
(;) after the condition expression. Consider the following example:
if (marks >50)
If the value of the variable “marks” is greater than 50, the message “You have passed the exam!” is
displayed on the screen; otherwise the statement is skipped and no message is displayed.
/*Program 4.1 illustrates the use of if statement in a C program */
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks); //get marks
if(marks > 50) // if marks > 50 display message
printf("You have passed the exam!");
return 0;
}
Executing Program-4.1 with different inputs will behave as follows:
Sample Run
Let’s take our previous example, If the value of the variable “marks” is greater than 50, the message
“You have passed the exam!” is displayed on the screen; otherwise the statement “You have failed
the exam!” is displayed.
In many programs it is required to perform some action when a condition is TRUE and another action
when it is FALSE. In such cases, the if clause can be used to check the TRUE condition and act upon
it.
However, it does not act upon the FALSE condition. Therefore, the expression resulting the FALSE
condition needs to be re-organized. Hence the use of if-else structure.
Example 4.1 –Write a program to display the student’s grade based on the following table:
Marks Grade
Example 4.2 – A car increases it velocity from u ms-1 to v ms-1 within t seconds. Write a
program to calculate the acceleration.
The relationship among acceleration (a), u, v and t can be given as
v = u + at Therefore the acceleration can be found by the
formula 𝐚 = 𝐯 −𝐮 .
𝐭
In the program we implement users to input any values for u, v and t. However, to find the correct
acceleration, the time has to be non-zero and positive (since we cannot go back in time and a number
should not be divided by zero).
So our program should make sure it accepts only the correct inputs (Time cannot be 0 or less than 0).
The Program-4.7 calculates the acceleration given u, v and t.
Example 4.3
Consider the following example which determines the value of variable “b” based on the whether the
given input is greater than 50 or not. If the input is greater than 50 variable “b” will be assigned “1”
and if not it will be assigned “2”.
Sample Run:
Enter value of a: 49
Value of b: 2
Conditional operator can be used to implement simple if-else constructs. However, use of conditional
operator is not recommended as a good programming practice.
THANK YOU