It is a controlled statement which is used to control the
flow of execution of a program. The if() statement first test the conditions,
if conditions are true than the particular block of statements are executed. If
conditions are false than the particular block of statements will not executed.
Syntax:
if ( condition)
{
statement 1;
statement 2;
…………………………..
statement n;
}
example:
if ( a > b)
{
printf(“A is greater than B”);
}
There are 4 forms of if() statement.
- simple if statement
- if - else statement
- nested if – else statement
- if – else if – else statement
Simple if statement:
In
simple if statement, when condition
is true than a block of statement is executed other wise execution jump to next
statement and skip the if statement
block.
Syntax:
if(condition)
{
……………….
}
if – else statement:
It is the second form of if
statements. In if – else statement,
when a condition is true than if statement
is executed other wise else statement
is executed.
Syntax:
if(condition)
{
…………….
}
else
{
…………….
}
Nested if – else statement:
In this type of statements one if statement
contain one or more than one if statements.
Syntax:
if(condition)
{
……………………
if(condition)
{
…………….
}
……………….
}
else
{
…………………..
}
if – else if – else :
To check more than one condition we uses else if ladder. In which if first condition is false than we check
second condition and if second condition is false than we check third condition
and so on till the else statement.
syntax:
if(condition)
{
……………….
}
else if (condition)
{
………………………..
}
else
{
……………….
}
No comments:
Post a Comment