Lecture 6 - Program Control Structures - Decisions
Lecture 6 - Program Control Structures - Decisions
Methodologies
The if statement is used to check some given condition and perform some
operations depending upon the correctness of that condition.
It is mostly used in the scenario where we need to perform the different
operations for the different conditions.
The syntax of the if statement is given below.
if(expression)
{
//code to be executed
}
Flowchart of if statement in C
4
#include<stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
return 0;
}
Program to find the largest number of the
three.
5
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
Program to check whether a person is
eligible to vote or not.
8
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
If else-if ladder Statement
9
Syntax
if(condition1)
{
//code to be executed if condition1 is true
}else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
Else
{
//code to be executed if all the conditions are false
}
Flowchart of else-if ladder statement in C
11
12
{
printf("number is equal to 50");
}
Program to calculate the grade of the
student according to the specified marks.
13
If there is no break statement found in the case, all the cases will be
executed present after the matched case.
It is known as fall through the state of C switch statement.
16
17
break; }
Difference between if-else & switch
18