Skip to main content

The if-else Statement

The if-else Statement

As described in earlier post,the if statement will execute a single statement, or a group of statements, when the expression following if evaluates to true.But it does nothing when the expression evaluates to false.
In case, we want to execute one group of statements if the expression evaluates to true and another group of
statements if the expression evaluates to false,we need to use the If-else statement.
The syntax of If-else statement is as follows;
if(condition)
{

statement;

}
else
{

statement2;

}

If the condition 1 is evaluated true,then statement1 will execute else statement2 will be executed.
A simple example is;

if(a=2500 )
{
hra = 1200;
da = basic * 0.5 ;pt=150;
}
else
{
hra = basic*0.1 ;
da = basic * 25.01 / 100 ;
}
gs = basic + hra + da ;
net=gs-pt;
printf ( "Net salary = Rs. %f", net ) ;
}

Comments