Ad Code

Decision making statements.

Conditional statements/ Decision making statements/ control statements.

The execution of statements or program depends upon some condition. If the condition is true, then some block of statements will execute, if the condition is false, then other block of statements will execute.
Following are the decision making statements:-

1. if 
2. if else
3. nested if else
4. else if ladder
5. switch case

1. If Statements

 This is one way decision making statement in which the statement will execute only when the condition is true.

Syntax-

if(condition)
{
  statement1;
  statement2;
  . 
  .
  .
  statement(n);
}

Lets take an example to understand this.

WRITE A PROGRAM TO CHECK WHETHER THE FIRST NUMBER IS GREATER THAN SECOND NUMBER USING SIMPLE IF STATEMENT.


#inlcude<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“Enter the two numbers:”);
scanf(“%d%d”,&a,&b);
if(a>b)
{
printf(“First Number is Greater than Second Number”);
}
getch();
}

Output:-



2. If else statement:

This is the two way decision making statement in which if the condition is true, then the IF block statements will execute and give the output of the program. But in case the condition is false, then the ELSE block of statements will execute and give the output of the program.

SYNTAX-

if(condition)
{   
  statement1;
  statement2;     
}
else
{
  statement3;
  statement4;
}

Lets take an example to understand this.

WRITE A PROGRAM TO FIND THE GREATER AMONG TWO NUMBERS USING IF-ELSE STATEMENT.


#inlcude<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“Enter the two numbers:”);
scanf(“%d%d”,&a,&b);
if(a>b)
{
printf(“First number is greater than second number”);
}
else
{
printf(“Second number is greater than first number”);
}
getch();
}

Output:-

3. Nested if else:

In this type of decision making statements, the execution of the program is depend upon more than one conditions to be true or false.
 
Lets take an example to understand this.

WRITE A PROGRAM TO CHECK THE GREATEST NUMBER AMONG THREE NUMBERS USING NESTED IF-ELSE STATEMENT.


#inlcude<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“Enter the three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf(“Largest number is %d”,a);
}
else
{
printf(“Largest number is %d”,c);
}
}
else
{
if(b>c)
{
printf(“Largest number is %d”,b);
}
else
{
printf(“Largest number is %d”,c);
}
}
getch();
}

Output:-


4. Else if ladder


When the execution of statements depends upon multiple conditions in the way that only one condition can be true at one time.

Lets understand with an example.

WRITE A PROGRAM TO DISPLAY THE RESULT OF A STUDENT ON THE BASIS OF THEIR PERCENTAGE USING ELSE-IF LADDER.


#inlcude<stdio.h>
#include<conio.h>
void main()
{
float per;
clrscr();
printf(“Enter the percentage of the students:”);
scanf(“%f”,&per);

if(per<33)
{
printf(“Fail”);
}
else if(per>=33 && per<45)
{
printf(“Passed with third division”);
}
else if(per>=45 && per<60)
{
printf(“Passed with second division”);
}
else if(per>=60 && per<75)
{
printf(“Passed with first division”);
}
else if(per>=75 && per<100)
{
printf(“Passed with honour”);
}
else
{
printf(“Invalid result”);
}
getch();
}

Output:-


5. Switch case:

Switch case is a multi-way decision making statement in which there are multiple conditions are available and only one condition will be true at one time.
In case switch expression doesn't match to any condition, default statement will execute.

Let's understand with an example.

WRITE A PROGRAM TO DISPLAY THE WEEK PAGE ACCORDING TO ITS NUMBERS USING SWITCH CASE STATEMENTS.


#inlcude<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf(“Enter the choice:”);
scanf(“%d”,&day);
Switch(day)
{
Case 1:
printf(“Monday”);
break;
Case 2:
printf(“Tuesday”);
break;
Case 3:
printf(“Wednesday”);
break;
Case 4:
printf(“Thursday”);
break;
Case 5:
printf(“Friday”);
break;
Case 6:
printf(“Saturday”);
break;
Case 7:
printf(“Sunday”);
break;
default:
printf(“Invalid input”);
break;
}
getch();
}

Output:- 

Post a Comment

0 Comments

Ad Code