Conditional Statements in C Programming
Conditional Statements in C Programming
1.If Statement in C
Definition
If Statement is a basic conditional statement in C Programming. If
Statement is used as a single statement in C program, then code inside
the if block will execute if a condition is true. It is also called a one-way
selection statement.
Syntax of If Statement
The general syntax of if Statement in c is given below
if(expression)
{
//code to be executed
}
If the condition is evaluated as true, then the block statement is
executed, but if the condition is evaluated as false, then control is passed
to the next Statement following it.
C Program for if Statement
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“enter the number”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“%d number in even”,n);
}
getch();
}
Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in the following picture.
2.if-else Statement in C
Definition
if else statement in c allows two-way selection. If the given condition is
true. Then program control goes inside the if block and execute the
statement.
The condition is false then program control goes inside the else block
and execute the corresponding statement.
Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in following picture.
First, it will check the given condition associated with the if statement; if
the result of the given condition is True, then the Statement inside the if
block is executed.
If the given condition is false, then it will check the first else if part and
the condition of is true then the statements related to else if is Executed
otherwise the pointer goes to next else if and this process is contained
And if the result of the all the else if a condition is false then the pointer
is automatically going to else part and the Statement of the else
automatically executed.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr();
printf(“Please Enter three number”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf(“a is greatest number”);
}
else
{
printf(“c is greatest number”);
}
}
else
{
if(b>c)
{
printf(“b is greatest number”);
}
else
{
printf(“c is greatest”);
}
}
getch();
}
Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in following picture.
Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in following picture.
Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in following picture.