CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 6 min · free

Conditional Constructs: if, if-else-if and switch

6 min5 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

How Java decides: if, if-else, the if-else-if ladder, nested if, switch with break and fall through, a menu program and System.exit(0).

Follows the syllabus of: CBSE Class 10 Information Technology (402), CBSE Class 11 Information Technology (802), CBSE Class 12 Information Technology (802), ICSE Class 9 Computer Applications

On screen in this lesson

Conditions and operators

OperatorAsksExample
==equal to?marks == 33
!=not equal to?ch != 'n'
> < >= <=bigger, smaller?age >= 18
&&both true?age >= 18 && card
=stores a valuemarks = 45

The switch statement

switch compares one value with many cases
Works with int, char and String, not double
break ends the switch
default runs when no case matches

if-else-if or switch?

Pointif-else-ifswitch
Checksany conditionequality only
Rangesyes, marks >= 50no
Typesanyint, char, String
Best forranges, logicfixed choices

Recap

if runs code only when the condition is true
Ladder: the first true condition wins
Nested if: inner if checked only if outer is true
switch: break stops, no break falls through
System.exit(0) ends the whole program

Quick answers

Why did my switch print three lines?

The break statements were missing, so it fell through into the next cases and default.

What is the difference between break and System.exit(0)?

break leaves only the switch; System.exit(0) ends the whole program.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. A switch was meant to print one day, but it printed Tue, Wed and a question mark. Why? Today we learn how Java makes decisions with if, if else, the if else if ladder, nested if, and switch. We will also build a menu driven program and learn System dot exit.

A condition is a question whose answer is true or false. Double equals asks whether two values are equal. Not equal to asks whether they are different. Greater than, less than, and their or equal to forms compare sizes. Double ampersand means and, so both conditions must be true. A single equals sign only stores a value, so never use it to compare.

An if statement runs its statement only when the condition is true. The condition always goes inside round brackets. An if else gives two paths, and exactly one of them runs. Here marks is twenty eight, and res will hold the result. Twenty eight is not thirty three or more, so the else part runs, and it prints Fail.

When there are more than two paths, we use an if else if ladder. The first diamond asks, marks seventy five or more? If yes, the grade is A, and no other check happens. If no, we go down, and the second diamond asks, marks fifty or more? If yes, the grade is B. If both answers are no, the final else gives grade C. Then we print the grade and stop.

Here is the same ladder in Java. Pause and predict, what prints when marks is exactly seventy five? Seventy five is greater than or equal to seventy five, so the first condition is true. Grade becomes A, and Java skips the rest of the ladder. If we had written greater than without equals, the answer would have been B.

A nested if is an if statement inside another if. The inner if is checked only when the outer condition is true. Here the outer if asks, is the age eighteen or more? Nineteen is, so we go inside. The inner if asks, does the voter have an identity card, stored in id? It is false, so it prints Get ID.

When one variable is compared with many fixed values, a switch is neater than a long ladder. It checks one value against a list of cases. It works with int, char and String values, but not with double or boolean. The keyword break jumps out of the switch once a case is done. The default part runs when no case matches, like an else.

Here the switch value is two, and d will hold the day. Java jumps straight to case two and stores Tue, for Tuesday. Then break takes it out of the switch, so nothing else runs, and it prints Tue. If the value were nine, no case would match, and default would store a question mark.

Now every break is missing, and here is our title question. Java jumps to case two and adds Tue. With no break, it does not stop. It falls through into case three and adds Wed, and then into default and adds a question mark. So it prints Tue, Wed and a question mark. This is called fall through, and board exams often ask for this output.

So which one should we use? An if else if ladder can check any condition, but a switch only checks whether values are equal. A ladder can test ranges like marks fifty or more, but a switch cannot. A ladder works with any type, while a switch works with int, char and String. Use a ladder for ranges and a switch for a fixed list of choices.

A menu driven program shows a list of choices, and the user picks one by typing its number. Here ch is the choice, l is the length and b is the breadth of a rectangle. If the user types one, ans stores the area, length times breadth. If the user types two, ans stores the perimeter, two times length plus breadth. Any other number reaches default, which ends the program with System dot exit.

System dot exit ends the whole program at once. The zero is a status code, and zero means the program ended normally. Here it prints Start, then exit stops everything, so Never is never printed. In a menu, we often use it for an exit choice, or to stop when the input is wrong. Remember, break leaves only the switch, but exit ends the program.

Let us recap. An if runs its code only when the condition is true, and else handles the other case. In a ladder, the first true condition wins and the rest are skipped. A nested if is checked only when the outer if is true. In a switch, break stops it, and without break the cases fall through. And System dot exit of zero ends the whole program.

Courses that teach this

CourseUnit
CBSE Class 10 Information Technology (402)Electronic Spreadsheet (Advanced) using LibreOffice Calc
CBSE Class 11 Information Technology (802)Part B, Unit 5: Fundamentals of Java Programming
CBSE Class 12 Information Technology (802)Fundamentals of Java Programming
ICSE Class 9 Computer ApplicationsConditional Constructs in Java
ISC Class 11 Computer Science (868)Statements, Control Flow and Functions
GSEB Std 12 Computer StudiesObject-Oriented Concepts and Java Basics
NIOS Senior Secondary Data Entry Operations (336)Lesson 8: Formulas, Functions and Charts
Programming All levels JavaControl Flow and Iteration

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →