Coupon Accepted Successfully!


The if-else Statement

In the if–else statement, if the expression/condition is true, the body of the if statement is executed; otherwise, the body of the else statement is executed. The else keyword is used when the expression is not true. The format/syntax of if–else statement is given in Figures (a), (b) and (c).

As shown in Figure (c), both if and else blocks contain statement/statements. When the expression is true, the if block is executed, otherwise, the else block is executed.

(a) The if-else statement


(b) The if-else statement


(c) The if-else statement

Flowchart for if-else is as shown in Figure.


Description: 2858.png

The if-else flow-chart

Consider the following programs for understanding the if-else statement.

5.6 Write a program to enter age and display a message whether the user is eligible for voting or not. Use if-else statement.


Explanation: In the above program, the user enters his/her age. The integer variable age is used to store the value. The if statement checks the value of age. If the age is greater than or equal to 18, then the if block of the statement is executed. Otherwise, the else block of statement is executed.

5.7 Write a program with simple if-else statement. If the entered cricket score is less than 50 display one message otherwise another message.


Explanation: The program prompts the user to enter the score of the batsman. If the score entered is greater than 50 or equal to 50, then it shows a particular message. Otherwise, for a score less than 50, the other message displayed.

Test Your Skills Now!
Take a Quiz now
Reviewer Name