HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

The else if Statement


In programming, an else if statement is a conditional statement that allows you to specify multiple conditions to be evaluated in a sequence.

Syntax

if (condition1) {

// block of code to be executed if condition1 is true

} else if (condition2) {

// block of code to be executed if the condition1 is false and condition2 is true

} else {

// block of code to be executed if the condition1 is false and condition2 is false

}



Example

#include <stdio.h>

int main() {

int time = 22;

if (time < 10) {

printf("Good morning.");

} else if (time < 20) {

printf("Good day.");

} else {

printf("Hello CodeLines.");

}

return 0;

}


Output

Hello CodeLines.

Example explained


In the above example, time(22) is greater than 10, so the first condition is false. The next condition, else in the if statement is also false, so we move on to the second condition because both condition 1 and condition 2 are false - and print "Hello CodeLines" on the screen.


However, if the time was 14, our program would print "Good day."