If
if (condition) { first statements to be executed ;
second statements to be executed ; }
Above is the syntax of the if statement. The statements to be executed are enclosed within curly brackets.
Example
---------
Write a function called message( ) that accepts an integer as an argument. The message ( ) function should print one of the following, depending on the value of its argument.
Argument Value Message
0 File not found
1 File full
2 File not found
3 Not enough memory
4 Illegal data
The following C program will be the solution
int i;
main ( ) ;
{
scanf ( " %d", &i);
message ( );
}
message( )
{
if ( i = = 0) printf ( " file not opened " );
else
if ( i = = 1) printf ( " file full " );
else
if ( i = = 2) printf ( " file not found " );
else
if ( i = = 3) printf ( " not enough memory " );
else
if ( i = = 4) printf ( " illegal data " );
}
Now when this program is compiled and run
if you input 0, output is file not opened
if you input 1, output is file full
if you input 2, output is file not found
if you input 3, output is not enough memory
if you input 4, output is illegal data