You are on page 1of 4

3.

Nesting of if else statements:-


One if else statement may be present within the other if else statement, this condition is referred to as
nesting of if else statements.

Syntax:
if (condition){
……..do this;
……..do this;
}
else
{
if(condion)
…… do this;
else
……..do this; <--------- nesting of if else.
}

4. Break statement
- We often come across situations where we want to jump out of a loop instantly, without
waiting to get back to the conditional test.
- break is a keyword in C.
- break is used to terminate loops or conditions.
- When compiler read the break statement inside loop, then control automatically passes to
the first statement after the loop.
- A break is usually associated with an if.
Syntax:
if(condition)
{
do this;
do this;
break;
do this;
do this;
}
do this;
P1 - Write a program to obtain division of the student of your class

#include<stdio.h>
#include<conio.h>
int main()
{
int n1,n2,n3,n4,n5,n6, sum=0;
float ave;
clrscr();
printf("Enter the marks of five subjects one by one \n");
scanf("%d%d%d%d%d",&n1,&n2,&n3,&n4,&n5);
sum=n1+n2+n3+n4+n5;
ave=sum/5.0;
if(ave>=60&&ave<=100)
printf("First Division: \t Average Marks =%f",ave);
else
{
if(ave>=45&&ave<60)
{
printf("Second Division: \t Average Marks =%f",ave);
}
else
{
if(ave>=33&&ave<45)
printf("Third Division: \t Average Marks =%f",ave);
else
{
if(ave<33)
printf("Fail: \t Average Marks =%f",ave);
else
printf("Condition does not match");
}
}
}
getch();
return 0;
}
------------------------------------------------------------------------------------------------------------------------
Enter the marks of five subjects one by one <-------------------- INPUT
65
65
37
97
98
------------------------------------------------------------------------------------------------------------------------
First division, Average marks =72.400002 <-------------------- OUTPUT
------------------------------------------------------------------------------------------------------------------------
// Write a program whether a given number by user is a prime number or not prime number
using break statement.
#include<stdio.h>
#include<conio.h>
int main()
{
int num, i ;
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
i=2;
while ( i <= num - 1 )
{
if( num % i == 0 )
{
printf ( "Not a prime number" ) ;
break ;
}
i++ ;
}
if ( i == num )
printf ( "Prime number" ) ;
getch();
return 0;
}
7 <--------------------INPUT

Prime number <------------------OUTPUT


//Find output of the following program.
#include<stdio.h>
#include<conio.h>
main()
{
int x=1,y=6;
//clrscr();
while(x<=y)
{
if(x==y)
break;
else
printf("x=%d and y=%d \n",x,y);
x++;
y--;
}
getch();
}
x=1 and y=6 < -------------- OUTPUT
x=2 and y=5
x=3 and y=4

You might also like