if else if Ladder In C Programming Language

Rumman Ansari   Software Engineer   2023-03-27   20500 Share
☰ Table of Contents

Table of Content:


A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder.

You have seen how the else statement comes into play when you have a test against two possible conditions—either the number is even or it is odd; either the year is a leap year or it is not.However,programming decisions you have to make are not always so black and white. Consider the task of writing a program that displays –1 if a number the user types is less than zero, 0 if the number is equal to zero,and 1 if the number is greater than zero.(This is actually an implementation of what is commonly called the sign function.) Obviously, you must make three tests in this case to determine whether the number that is keyed in is negative, zero, or positive. The simple if-else construct will not work. Of course, in this case, you can always resort to three separate if statements,but this solution does not always work—especially if the tests are not mutually exclusive.

You can handle the situation just described by adding an if statement to your else clause.We mentioned that the statement that follows an else could be any valid Objective-C program statement,so why not another if ? Thus,in the general case,you could write the following:

The syntax for a if else if ladder as follows ?

if(condition)
 statement;
 else if(condition)
      statement;
      else if(condition)
        statement;
       .
        .
         .
         else
         statement;

Practical Approach of if else if Ladder

// Program to implement the sign function
#include
int main()
{
 
int number, sign;
printf("Please type in a number: ");
scanf ("%i", &number);
if ( number< 0 )
	{
	sign = -1;
	}
   else if ( number == 0 ){
	 sign = 0;
   }
   else // Must be positive
	{ 
	sign = 1;
	}
	
printf("Sign = %i \n", sign);
 
return 0;
}
Output
 
 Output 1: 
Please type in a number: 10
Sign = 1
Press any key to continue . . .

 Output 2: 
Please type in a number: 0
Sign = 0
Press any key to continue . . .

 Output 3: 
Please type in a number: -10
Sign = -1
Press any key to continue . . .

Another Approach of if else if Ladder

if statement in java

The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. The final else acts as a default condition; that is, if all other conditional tests fail, then the last else statement is performed. If there is no final else and all other conditions are false, then no action will take place.

Practical Approach of if else if Ladder

#include
    void main(){ 
	double score = 55;

	if (score >= 90.0)
	 printf("A \n");
	else if (score >= 80.0)
	 printf("B \n");
	else if (score >= 70.0)
	 printf("C \n");
	else if (score >= 60.0)
	 printf("D \n");
	else
     printf("F \n");
  }
 
Output
 
F
Press any key to continue . . . 

Another Example

The next program analyzes a character that is typed in from the terminal and classifies it as either an alphabetic character ( a – z or A – Z ), a digit ( 0 – 9 ), or a special character (any- thing else).To read a single character from the terminal,the format characters %c are used in the scanf call.

// Program to implement the sign function
#include
int main()
{
 char c;
printf("Enter a single character:  ");
scanf (" %c", &c);
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ){
	printf("It's an alphabetic character \n");
    }
	else if ( c >= '0' && c <= '9' ){
  	  printf("It's a digit. \n");
	}
	 else{
		printf("It's a special character. \n");
		}
 
return 0;
}
Output
 
 Output 1: 
Enter a single character:  %
It's a special character.
Press any key to continue . . .

 Output 2: 
Enter a single character:  a
It's an alphabetic character
Press any key to continue . . .


 Output 3: 
Enter a single character:  A
It's an alphabetic character
Press any key to continue . . .


 Output 4: 
Enter a single character:  10
It's a digit.
Press any key to continue . . . 

As shown in the program example, it’s best to put a space before the %c in the scanf for- mat string (as in " %c" ). This will cause scanf to "skip" over any so-called whitespace char- acters (e.g., newlines, returns, tabs, line feeds) in the input. Omitting that space can cause scanf to read in a character that you don’t expect. While that might not be a problem in this example, it’s good to keep this in mind when working on other examples in this chapter (including the exercises) whenever you want to read a single character.