CONDITIONAL STATEMENTS:
1] if statement
syntax:
if(condition)
{
statement;
}
EXAMPLE:
#include<stdio.h>
main()
{
int marks;
if(marks>40)
{
printf("passed");
}
}
NOTE: for executing a single statement we can omit the curly braces.......
2] if......else statement
syntax:
if(condition)
{
statement-1;
}
else
{
statement-2;
}
EXAMPLE:
#include<stdio.h>
main()
{
int marks;
if(marks>40)
{
printf("passed");
}
else
{
printf("failed");
}
}
3] else if statement
syntax:
if(condition-1)
{
statement-1;
statement-2;
.........;
}
else if(condition-2)
{
statement-1;
statement-2;
.........;
}
else
{
statement-1;
statement-2;
.........;
}
EXAMPLE:
#include<stdio.h>
main()
{
int a,b,c;
printf("enter 3 values:");
scanf("%d %d %d",&a,&b,&c);
if(a<b&&a<c)
{
printf("%d is greater",a);
}
else if(b<a&&b<c)
{
printf("b is greater");
}
else if(c<a&&c<b)
{
printf("c is greater");
}
else
{
printf("all are equal");
}
}
4] nested if statement
syntax:
if(expression-1)
{
statements;
if(expression-2)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
EXAMPLE:
#include<stdio.h>
main()
{
int a,b,c;
printf("enter 3 values:");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("\n%d is greater",a);
else
printf("\n%d is greater",c);
}
else
{
if(b>c)
printf("\n%d is greater",b);
else
printf("\n%d is greater",c);
}
}
5] switch statement
syntax
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
.........
.........
default:
default-block;
break;
}
EXAMPLE:
#include<stdio.h>
main()
{
int n;
printf("enter your choice:");
scanf("%d",n);
switch(n)
{
case 1:
printf("TELUGU");
break;
case 2:
printf("ENGLISH");
break;
case 3:
printf("HINDI");
break;
default:
printf("invalid key");
}
}
0 Comments