CONTROL STRUCTURE OF
C - LANGUAGE
LUIMM
COMPUTER FUNDAMENTAL & PROGRAMMING
Lesson 3
TABLE OF CONTENT
If statement
If else if statement
Nested If structure
COMPUTER FUNDAMENTAL & PROGRAMMING
Switch Case structure
If else statement
LEARNING OUTCOME
1
❖Understand how to use logical expressions to control
program flow.
❖Learn to write and interpret simple and nested if-else
statements.
❖Develop an appreciation for code efficiency and
optimization when using control structures.
❖Apply control statements to practical programming
scenarios.
❖Understand how control structures are used in real-
world applications to control program behavior.
COMPUTER FUNDAMENTAL & PROGRAMMING
2
Control structures allow to change the ordering of how the statements in a program is
executed
Control Structure
COMPUTER FUNDAMENTAL & PROGRAMMING
Types of control structures:
❖Decision control structures
allow to select specific sections of code to be executed
❖Repetition control structures
allow to execute specific sections of the code a number of times
3
If else statement
COMPUTER FUNDAMENTAL & PROGRAMMING
C language statements that allows a programmer to select and execute specific
blocks of code while skipping other sections.
Include types such as:
•If-Else
•If-Else-If
•Switch structure
if – else Statement
4
COMPUTER FUNDAMENTAL & PROGRAMMING
"The if-else statement is used to
express decisions” -Dennis Ritchie.
In a simple if-else conditional statement,
there are two given choices the computer
could make.
However, the computer could only choose one
of them. It is like having two girlfriends, you
could only choose one to marry (if you are a
true Christian, not a fake one). The formal
syntax of if-else is:
if (condition)
statement;
else
statement;
Example
5
COMPUTER FUNDAMENTAL & PROGRAMMING
1. Write a program that determines if the input age
is qualified to vote or not. The qualifying age is 18
years old and above.
#include<stdio.h>
int main()
{
int age;
printf(“\nEnter Age : ");
scanf("%i",&age);
if(age>=18)
printf("%i is Qualify age to vote",age);
else
printf("%i is NOT Qualify age to vote",age);
return 0;
}
Example
6
COMPUTER FUNDAMENTAL & PROGRAMMING
2. Write a program that determines if the input
number is POSITIVE or NEGATIVE. Consider 0 as
positive (considering that it contains no negative sign).
#include<stdio.h>
int main()
{
int number;
printf("\n Enter number : ");
scanf("%i",&number);
if (number>=0)
printf("\n %i is a Positive Number",number);
else
printf("\n %i is a Positive Number",number);
return 0;
}
Example
7
COMPUTER FUNDAMENTAL & PROGRAMMING
3. Write program that will determine if the student is
passed or failed.
#include<stdio.h>
int main()
{
int grade;
printf("\n\n GRADE : ");
scanf("%i",&grade);
if(grade>=75)
printf("\n ***** PASSED *****");
else
printf("\n ***** FAILED *****");
return 0;
}
if Statement
8
COMPUTER FUNDAMENTAL & PROGRAMMING
if (condition)
statement;
if (condition)
statement;
if (condition)
statement;
if (condition)
statement
Example
9
COMPUTER FUNDAMENTAL & PROGRAMMING
#include <stdio.h>
int main()
{
int a;
printf("Enter number ");
scanf("%i",&a);
if (a==1)
printf("one");
if (a==2)
printf("TWO");
if (a==3)
printf("Three");
return 0;
}
if Compound
Statement
10
COMPUTER FUNDAMENTAL & PROGRAMMING
As in the if statement, compound statements may also be
used in if-else statements (provided they are grouped
together by braces).
if (expression)
{
statement1;
statement2;
…
}
else
{
statement3;
statement4;
…
}
If there is a need to
execute several statements
(compound statement),
then left and right {} braces
can group these
statements.
Syntax
11
COMPUTER FUNDAMENTAL & PROGRAMMING
If there is a need to
execute several statements
(compound statement),
then left and right {} braces
can group these
statements.
Every time C language encounters an if statement,
1.It determines whether expression is true or false.
2.If expression is true, then C language executes
statement.
3.If expression is false, then C language ignores or skips
the remainder of the if statement and proceeds to
the next statement.
if Compound
Statement
if – else if Statement
12
COMPUTER FUNDAMENTAL & PROGRAMMING
user can decide among multiple options.
The if statements are executed from the
top down. As soon as one of the
conditions controlling the if is true, the
statement associated with that if is
executed, and the rest of the ladder is
bypassed. If none of the conditions is
true, then the final else statement will be
executed.
if (condition)
statement;
else if (condition)
statement; . .
else statement;
if structure
13
COMPUTER FUNDAMENTAL & PROGRAMMING
If there is a need to
execute several statements
(compound statement),
then left and right {} braces
can group these
statements.
if (condition)
{
statement1;
statement2;
…
}
else if (condition)
{
statement1;
statement2;
…
}
else
{
statement1;
statement2;
}
Example
14
COMPUTER FUNDAMENTAL & PROGRAMMING 2. Write a program that determines if the input number is
POSITIVE, NEGATIVE or ZERO.
#include <stdio.h>
int main() {
int number;
// Input a number from the user
printf("Enter a number: ");
scanf("%d", &number);
// Check and display if the number is positive, negative, or zero
if (number > 0)
{ printf(“%d”,number);
printf("The number is positive.\n");
}
else if (number < 0)
{ printf(“%d”,number);
printf("The number is negative.\n"); }
else {
printf(“%d”,number); printf("The number is zero.\n");
}
return 0;
}
Example
15
COMPUTER FUNDAMENTAL & PROGRAMMING
3. Write program that will determine if the
student is passed or failed.
#include<stdio.h>
int main()
{
int n1,n2,choice;
float age;
printf("Enter Age : ");
scanf("%f",&age);
if(age<16)
{
printf("%.0f not Qualify",age);
}
else if((age>=16) && (age<=17))
{
printf(“You are %.0f, Eligible to vote for SK only",age);
}
else if((age>17) && (age<=25))
printf(“You are %.0f, Eligible to vote for SK & National",age);
else
printf("%.0f You are %.0f, Eligible to vote National only",age);
return 0;
}