Conditional statements in c programming
Conditional statements are used to check the condition.whaether condition is true or not.if condition is true it runs one statement.there are three conditional statements in c programming language.
These are :
-
1.if statement
-
2.if-else Statement
-
3.Nested if else statement
1.if statement:
First we are discussing if statement .In c program we are using if statement for checking condition.if condition is true it gives output.if condition does not satisfy it does not print anything..
Syntax for if statement:
if(condition)
{
_________
_________
_________
}
Program using if statement:
#include<stdio.h>
void main()
{
int a;
Printf(“enter a value:”);
Scanf (“%d”,&a);
if(a<5)
{
Printf(“%d\n”,a);
}
}
Output:
enter a value:4
4
Explanation:👇👇👇
E program lo a value ani ravataniki printf function vadamu.manam values read cheyataniki scanf function use chesamu.nenu value vachesi 4 ani enter chesanu.Aa tharwatha if statement vadi condition check chesamu.value 4 anedi 5kante thakkuva kabatti 4 ani value print ayyindi.\n is anedi next line lo output print avvataniki a backspace c program lo use chestam.
2.if-else statement:
In c language if else is also called as conditional statements.it checks the condition in if statement if it is true it print the inner program of if .if statement is false it goes to else part of a program.
Syntax for if else statement:
if(condition)
{
________
________
________
}
else
{
_______
________
________
}
Program for if else statement:
#include<stdio.h>
void main()
{
int a,b;
Printf(“enter a value”);
printf(“enter b value”);
scanf(“%d”,&a);
scanf(“%d”,&b);
if(a>b)
{
printf(“a is bigger”);
}
else
{
printf(“b is bigger”);
}
}
output:
enter a value 5
enter b value 6
b is bigger
Explanation:👇👇👇
Ikkada program lo two values manam read chesam a,b anevi.a>b ane condition ni check chesindi 6nedi peddadhi 5 kante kabatti b is bigger ani vachindi.
3.Nested if else statement:
Neste if else statement nothing but if statement inside another if statement.it checks the if condition if it is true inner program executes.if it is false it goes to inner if statement it checks another condition if it is also false then it goes to else statement.
Syntax for nested if else statement:
if(condition)
{
______
______
else if(condition)
{
______
_______
}
else
{
_____
_____
}
}
Program:👇👇👇
#include<studio.h>
void main()
{
int a,b,c;
printf(“enter a value:”);
printf(“enter b value:”);
Printf (“enter c value:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
printf(“a is bigger”);
}
else if(b>c)
{
printf(“b is bigger”);
}
else
{
printf (“c is bigger”);
}
}
output:👇👇👇
enter a value:4
enter b value:5
enter c value:7
c is bigger.
Explanation:👇👇👇
It takes three values a,b,c.first a>b ane condition ni check chestundi .a condition false aithe else if check chestadi adhi kooda false aithe else statement print avuthadi.