The if-else Statement in programming in C.

The if-else Statement

The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Now we can execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false with the else statement.

Syntax :
if ( expression )
{
/* Statement list if the condition is true */
}
else
{
/* Statement list if the condition is false */
}

Example :

main()
{
int a;
printf ( " Enter a number " ) ;
scanf ( " %d ", &a) ;
if ( a > 0 )
{
printf ( " Number is positive " ) ;
}
else
{
printf ( " Number is zero or negetive." ) ;
}
}

On the execution of this program if you type any positive number (e.g. 5, 9, 12, 888) then the true part of if-else block will be executed and print the result "Number is positive", the else part will be skip. If you enter any non-positive number (e.g. 0, -45, -1, -68) then the true part of if-else block will be skip, the else part will be executed and print the result "Number is zero or negetive".

Flowchart :

flowchart of if-else example

Share with

Comments 0

Add your comment