if()..if else..else if…Statement

Computer Education, Training & Tutorial Resources - ComputerEducationWorld.com
Home » Free Educations » Free Programming Training » C Programming » C For Beginners »

if ( 2 <  4 )
    printf( “two is  less than four, that’s not a surprise” );

this syntax can be used with your program by simply including the header file and few  statements .If you have more than one statements,use braces, like we did with the body of the main function. Anything inside braces is called a compound statement. When using if statements, the code that depends on the if statement is called the “body” of the if statement.So the right syntax will be

if ( TRUE ) {
  /* between the braces is the body of the if statement */
  Execute all statements inside the body
}

It is recommended for putting braces following if statements. If you do this, you never have to remember to put them in when you want more than one statement to be executed, and you make the body of the if statement more visually clear.

If we go ahead ,and dealing with if statement evaluates to false ,we have to use else statement with if. And this code goes here..

if ( TRUE ) {
  /* Execute these statements if TRUE */
}
else {
  /* Execute these statements if FALSE */
}

When the statement to solve a program is more complex it then solve with more if else and else if statement.You can use an “else if” statement following an if statement and its body; that way, if the first statement is true, the “else if” will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked hence only one statement(either if or else if or else ) will execute.

#include <stdio.h>

int main()                           

{
    int salary;                          /*declaring a variable… */
 
    printf( “Please enter your salary” );  /* Asks for salary */
    scanf( “%d”, &salary );                 /* The input is put in salary */
    if ( salary < 10,000) {                  /* If the salary is less than 10,000 */
     printf (”Oh! Your salary is low\n” ); /* Just to show you it works… */
  }
  else if ( salary== 10,000 ) {            /* I use else just to show an example */
     printf( “Yeh! You have enough money \n” );      
  }
  else {
    printf( “You are a rich man\n” );     /* Executed if no other statement is */
  }
  return 0;
}
 
 


• • •
 



captcha PHP Script Free PHP captcha script free php
Website Design by WebWalas.com