Control Statements | IF Statements | Switch Statement | Examples
Control Statements
These are those statement which controls the flow of program control. They transfer the control of program to another part of program conditionally or unconditionally.
There are several control statements in the C Programming which are being discussed below -
If Statement
If statement evaluates the condition inside the parentheses. if the condition is true then statement inside the if body are executed and if the condition is evaluated to false then statements inside the body of if are skipped.
Syntax :
if(condition)
{
Statements;
}
Flow Chart :
If-If Statement
This is used to check multiple conditions for a particular problem.
Syntax :
if(condition)
{
statement 1;
}
if(condition)
{
statement 2;
}
Flow Chart :
If-Else Statement
This statement is used with the keyword else. if condition evaluates to be true then the code inside the body of if will execute otherwise the code inside the body of else will be executed.
Syntax :
if(condition)
{
statements;
}
else
{
statements;
}
Flow Chart :
Nested If Statement
This uses multiple if statements one inside another and a optional else statement.
Syntax :
if(condition)
{
if(condition)
{
if(condition)
{
statements;
}
statements;
}
}
else
{
statements;
}
Flow Chart :
Else-If Ladder
This statement consist of combination of if and else statements in a ladder fashion which is used to evaluate condition and execute a given specific code.
Syntax :
if(condition)
{
statements;
}
else if(condition)
{
........
........
........
}
else
{
statements;
}
Flow Chart :
Switch Statement
This statement is an alternative to if-else ladder statement and mostly used in menu driven program where a given condition or a variable is matched with the different cases and depending upon the case a given case code gets executed.
Syntax :
switch(condition/variable)
{
case 1:
case 2:
default :
}
Note : We use break statement in order to solve the problem of fall through. Fall Through is a condition in which more than one cases code is executed.
Flow Chart :
Example To illustrate Different If Statements
WAP(Write a program) to find greatest of three numbers ?
Using Nested IF..
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
} else {
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
return 0
- }
Using If-Else Ladder
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
return 0;
}
Using If Statement
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
Thank You So Much.. I will cover different iterative statements in my next post.
For Technology related stuff's visit my youtube channel and subscribe that channel
for getting technology related updates and to learn different technology.
Comments
Post a Comment