★ India’s Student Guidance Platform

Conditional Constructs in Java — ICSE Class 10 Computer Applications

ICSE Class 10 Computer Applications chapter card: Conditional Constructs in Java

Program ka kaam sirf calculate karna nahi hai — decide karna bhi hai. Marks 33 se zyada hain ya nahi, number even hai ya odd, user ne menu mein 1 dabaya ya 4 — yeh saare faisle Java mein conditional constructs se hote hain. ICSE Class 10 Computer Applications mein yeh Unit 1 (Revision of Class IX) ka hissa hai, aur paper mein output questions se lekar full programs tak har jagah dikhta hai. Chaliye, ek-ek karke, aaram se.

Key Rule — The Condition Must Be boolean
Java mein if ke bracket ke andar jo bhi likho, uska result boolean (true/false) hona chahiye — int nahi. C/C++ mein if(5) chalta hai; Java mein nahi chalta. Yeh Java ki sabse badi safety feature hai aur exam ka favourite 1-mark question bhi.

The Simple if Statement

Sabse chhota decision: “agar yeh sach hai, toh yeh karo.” Agar sach nahi hai, kuch mat karo — program aage badh jaata hai. Syntax:

if (condition)
{
    statement(s);
}
Example 1 — Pass ka message
int marks = 41;
if (marks >= 33)
{
    System.out.println("Pass");
}
System.out.println("Result declared");

Output:

Pass
Result declared

“Result declared” hamesha chhapega — woh if block ke bahar hai. Sirf “Pass” conditional hai.

Common Mistake
if (x = 5) likhna. Yeh assignment hai, comparison nahi — aur Java isse compile hi nahi karta. Compiler bolta hai: incompatible types: int cannot be converted to boolean. Comparison ke liye hamesha == (do barabar ke nishaan) lagao.
Common Mistake — The Stray Semicolon
if (a > 5); — bracket ke turant baad semicolon lagane se if ka body khaali ho jaata hai. Neeche wali line if ke bahar chali jaati hai aur hamesha chalti hai.
Example 2 — Semicolon ka jaal
int a = 3;
if (a > 5);
    System.out.println("Inside");
System.out.println("Outside");

Output:

Inside
Outside

a = 3 hai, phir bhi “Inside” chhapa — kyunki semicolon ne if ka body khatam kar diya. Indentation compiler ke liye kuch matlab nahi rakhta.

↑ Back to top

The if…else Statement

Do raaste, aur exactly ek chalega. Yaad rakho: else ka apna koi condition nahi hota — woh “baaki sab kuch” hai.

Example 1 — Even ya odd (negative number ke saath)
int n = -7;
if (n % 2 == 0)
    System.out.println(n + " is even");
else
    System.out.println(n + " is odd");

Output:

-7 is odd

Dhyaan do: −7 % 2 ka answer −1 hai, 1 nahi. Isliye even test ke liye hamesha n % 2 == 0 likho — n % 2 == 1 negative numbers par fail ho jaata hai.

Example 2 — Leap year
int year = 2024;
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
    System.out.println(year + " is a leap year");
else
    System.out.println(year + " is not a leap year");

Output:

2024 is a leap year

Yeh ek hi condition mein && aur || dono use karta hai — brackets lagana yahan optional nahi, zaroori hai, warna logic badal jaata hai.

Common Mistake — Braces bhool jaana
if ke baad braces na lagao toh sirf agli EK line if ka hissa banti hai. Dekho:
int a = 10;
if (a > 5)
    System.out.println("Big");
    System.out.println("Always");
System.out.println("End");

Output:

Big
Always
End
Yahan a > 5 sach tha isliye teeno chhape. Par agar a = 2 hota, toh “Always” phir bhi chhapta — woh if ke andar hai hi nahi. Hamesha braces lagao.
Exam Tip
Output-prediction questions mein sabse pehle braces dhoondho, indentation nahi. Examiner jaan-boojh kar galat indentation deta hai. Compiler indentation nahi padhta — tum bhi mat padho.

↑ Back to top

The Ternary (Conditional) Operator

Java ka ekmatra teen operands wala operator: condition ? valueIfTrue : valueIfFalse. Yeh ek expression hai, statement nahi — iska matlab woh apni ek value deta hai, jise tum store ya print kar sakte ho.

Example 1 — Bada number aur pass/fail
int a = 17, b = 25;
int big = (a > b) ? a : b;
System.out.println("Bigger = " + big);

int m = 33;
System.out.println(m >= 33 ? "Pass" : "Fail");

Output:

Bigger = 25
Pass

33 >= 33 sach hai — boundary value par students aksar “Fail” likh dete hain. >= aur > ka farak dhyaan se padho.

Example 2 — Nested ternary
int p = 4, q = 9;
String s = (p > q) ? "P" : (q % 2 == 0) ? "Q even" : "Q odd";
System.out.println(s);

Output:

Q odd

Pehla test (4 > 9) false, isliye colon ke baad wala poora hissa evaluate hua. 9 % 2 = 1, toh woh bhi false — answer “Q odd”. Ternary right se left group hota hai, isliye doosra ternary pehle wale ke else-part ke andar baith jaata hai.

Exam Tip
“Convert the following if…else into a ternary statement” ek pakka 2-mark question hai. Formula yaad rakho: if (C) x = A; else x = B;x = C ? A : B; Bas condition, question mark, sach wali value, colon, jhooth wali value.

↑ Back to top

The if…else if Ladder

Jab do se zyada raaste hon — grade nikaalna, bill slab lagana, category batana — tab ladder use hoti hai. Conditions upar se neeche check hoti hain aur pehli sachchi condition chalte hi baaki poori ladder chhod di jaati hai.

Key Rule — Order Matters
Ladder mein conditions ka order answer badal deta hai. Ranges ke liye hamesha sabse tight (sabse badi/chhoti) condition pehle likho. Agar tumne marks >= 40 sabse upar rakh diya, toh 95 marks wale ko bhi wahi mil jayega.
Example 1 — Grade calculator
int marks = 72;
char grade;
if (marks >= 90) grade = 'A';
else if (marks >= 75) grade = 'B';
else if (marks >= 60) grade = 'C';
else if (marks >= 40) grade = 'D';
else grade = 'E';
System.out.println("Marks = " + marks + ", Grade = " + grade);

Output:

Marks = 72, Grade = C

72 >= 90 → false. 72 >= 75 → false. 72 >= 60 → true, so grade = ‘C’ aur ladder wahin khatam. Neeche wali conditions check hi nahi hui.

Example 2 — Slab-based electricity bill
double units = 310, bill;
if (units <= 100) bill = units * 3.0;
else if (units <= 300) bill = 100 * 3.0 + (units - 100) * 4.5;
else bill = 100 * 3.0 + 200 * 4.5 + (units - 300) * 6.0;
System.out.println("Units = " + units + ", Bill = " + bill);

Output:

Units = 310.0, Bill = 1260.0

300 + 900 + 60 = 1260. Aur dhyaan do — units double hai, isliye output 310.0 chhapa, 310 nahi. Slab programs mein yeh chhoti si baat marks kha jaati hai.

Example 3 — Positive, negative ya zero
int n = 0;
if (n > 0) System.out.println("Positive");
else if (n < 0) System.out.println("Negative");
else System.out.println("Zero");

Output:

Zero

Zero ke liye alag condition likhne ki zaroorat nahi — else khud “na positive, na negative” ko pakad leta hai. Yeh ladder ka sabse saaf use hai.

↑ Back to top

Nested if

Ek if ke andar doosra if. Ladder aur nested if alag cheezein hain: ladder mein conditions bhai-behen hain (ek hi level par), nested if mein ek condition doosri ke andar hai — andar wali tabhi check hoti hai jab bahar wali sach ho.

Example 1 — Teen numbers mein sabse bada (ties ke saath)
int a = 12, b = 45, c = 45;
if (a > b)
{
    if (a > c) System.out.println("a is greatest");
    else System.out.println("c is greatest");
}
else
{
    if (b > c) System.out.println("b is greatest");
    else if (c > b) System.out.println("c is greatest");
    else System.out.println("b and c are equal and greatest");
}

Output:

b and c are equal and greatest

Bahar wali condition (12 > 45) false thi, isliye seedha else block mein gaye. Wahan b aur c barabar nikle. Zyadatar students tie ka case likhna bhool jaate hain — examiner exactly wahi test karta hai.

Example 2 — Triangle ka type
int a = 5, b = 5, c = 5;
if (a == b && b == c) System.out.println("Equilateral");
else if (a == b || b == c || a == c) System.out.println("Isosceles");
else System.out.println("Scalene");

Output:

Equilateral

Equilateral ka test pehle hona chahiye. Agar Isosceles wali line upar hoti, toh 5-5-5 bhi “Isosceles” chhapta — kyunki a == b sach hai.

Exam Tip
Nested if likhte waqt braces pehle lagao, code baad mein bharo. Har opening brace ke saamne uska closing brace turant likh do — ICSE ke long programs mein “missing brace” sabse aam silly mistake hai, aur woh poore program ko compile hone se rok deti hai.
Common Mistake — Dangling else
Braces ke bina, ek akela else hamesha sabse nazdeek wale unmatched if se judta hai — us if se nahi jiske saamne tumne usse indent kiya hai. Yeh galti dry-run questions mein bahut marks kha jaati hai.

↑ Back to top

The switch…case Statement

Jab ek hi variable ki alag-alag fixed values par decision lena ho — day number, menu choice, ek character — tab switch ladder se saaf aur padhne mein aasan hota hai.

switch (expression)
{
    case value1 : statement(s);
                  break;
    case value2 : statement(s);
                  break;
    default     : statement(s);
}
Key Rule — What switch Accepts
Switch ki expression byte, short, int, char (aur String) ho sakti hai — float, double aur boolean nahi chalte. Aur har case ki value ek constant honi chahiye, variable nahi. Range ya condition (jaise case x > 5) switch mein likhi hi nahi ja sakti — uske liye ladder chahiye.
Example 1 — Din ka naam
int day = 3;
switch (day)
{
    case 1: System.out.println("Monday"); break;
    case 2: System.out.println("Tuesday"); break;
    case 3: System.out.println("Wednesday"); break;
    case 4: System.out.println("Thursday"); break;
    default: System.out.println("Some other day");
}
System.out.println("Done");

Output:

Wednesday
Done

Har case ke baad break laga hai, isliye sirf matching case chala aur switch se bahar aa gaye.

Example 2 — Grouped cases (vowel check)
char ch = 'u';
switch (ch)
{
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        System.out.println(ch + " is a vowel");
        break;
    default:
        System.out.println(ch + " is a consonant");
}

Output:

u is a vowel

Yeh fall-through ka useful istemaal hai: paanchon vowel cases khaali hain, isliye control neeche behta hua ek hi statement tak pahunchta hai. Ek hi kaam, paanch entry points.

Exam Tip
default block optional hai, par ICSE ke answers mein hamesha likho — “invalid input” handle karne ke liye. Aur default switch ke beech mein bhi rakha ja sakta hai; woh tabhi chalta hai jab koi case match na ho, chahe woh kahin bhi likha ho. Yeh ek classic 2-mark theory question hai.

↑ Back to top

Fall-Through and the Role of break

Yeh chapter ka sabse zyada exam-worthy hissa hai. Switch ek matching case par koodta hai — aur phir rukta nahi. Jab tak break na mile ya switch khatam na ho jaye, neeche ke saare cases chalte rehte hain, unki value match ho ya na ho. Isi ko fall-through kehte hain.

Example 1 — Wahi program, bina break ke
int day = 2;
switch (day)
{
    case 1: System.out.println("Monday");
    case 2: System.out.println("Tuesday");
    case 3: System.out.println("Wednesday");
    case 4: System.out.println("Thursday");
    default: System.out.println("Some other day");
}

Output:

Tuesday
Wednesday
Thursday
Some other day

case 2 par entry hui, aur phir sab kuch chal gaya — default tak. “Monday” nahi chhapa kyunki woh entry point se upar tha.

Key Rule — Entry Point, Not a Box
case ek darwaza hai, dabba nahi. Switch us darwaze se andar ghusta hai aur seedha aage bhaagta hai. Sirf break (ya return/System.exit(0)) hi use rok sakta hai.
Example 2 — Switch ki expression pehle solve hoti hai
int k = 2;
switch (k + 1)
{
    case 2: System.out.print("Two ");
    case 3: System.out.print("Three ");
    case 4: System.out.print("Four "); break;
    case 5: System.out.print("Five ");
}
System.out.println("| over");

Output:

Three Four | over

k + 1 = 3, toh entry case 3 par. “Three ” chhapa, break nahi tha, isliye “Four ” bhi chhapa — aur wahan break mil gaya. “Five ” nahi chhapa.

Common Mistake
Fall-through wale output questions mein sirf matching case ka answer likh dena. Jab tak tumhe break na dikhe, neeche padhte raho. Har saal koi na koi student “Tuesday” likh kar teen lines ke marks gawa deta hai.

↑ Back to top

ICSE Section B mein “write a menu-driven program” ek regular question hai. Structure hamesha ek jaisa hota hai: user ek choice deta hai, switch us choice ko handle karta hai, aur default galat input ko pakadta hai. Program ko beech mein hi band karna ho toh System.exit(0) use karo — 0 ka matlab “normal termination”.

Example — Interest menu with exit
int choice = 9;
double p = 8000, r = 5, t = 2;
switch (choice)
{
    case 1:
        System.out.println("Simple Interest = " + (p * r * t / 100));
        break;
    case 2:
        System.out.println("Amount = " + (p + p * r * t / 100));
        break;
    default:
        System.out.println("Invalid choice. Program terminated.");
        System.exit(0);
}
System.out.println("This line never runs for an invalid choice.");

Output:

Invalid choice. Program terminated.

Aakhri line chhapi hi nahi — System.exit(0) ne poora JVM band kar diya. break sirf switch se bahar nikalta hai; System.exit(0) program hi khatam kar deta hai. Yeh farak likhwana examiner ko bahut pasand hai.

Exam Tip
Real menu-driven answer mein input Scanner class se lena hota hai (Scanner sc = new Scanner(System.in); phir sc.nextInt()), aur upar import java.util.Scanner; likhna zaroori hai. Yahan hum sirf logic dikha rahe hain, isliye value directly de di gayi hai — par paper mein import line bhoolna 1 mark ka nuksaan hai.
Common Mistake — System.exit(0) in BlueJ
System.exit(0) syllabus mein hai aur theory questions mein poocha jaata hai — par apne BlueJ lab assignments mein isse aadat mat banao. BlueJ ek hi virtual machine mein sab chalata hai, isliye System.exit(0) poora BlueJ hi band kar deta hai aur tumhe app dobara kholna padta hai. Lab mein program band karne ke liye break, ek boolean flag, ya method se return use karo. Written paper mein System.exit(0) likhna bilkul sahi hai.

↑ Back to top

Dry-Run Bank — Output Questions

ICSE Section A ka signature move: “What will be the output of the following?” Neeche ke har snippet ko pehle khud kaagaz par solve karo, phir answer kholo. Har program yahan compile karke chalaya gaya hai — output bilkul wahi hai jo tumhe milega.

Dry Run 1
int k = 2;
switch (k + 1)
{
    case 2: System.out.print("Two ");
    case 3: System.out.print("Three ");
    case 4: System.out.print("Four "); break;
    case 5: System.out.print("Five ");
}
System.out.println("| over");
Show Answer

Output:

Three Four | over

Expression pehle solve hoti hai: 2 + 1 = 3. Entry case 3 par, phir fall-through case 4 tak, jahan break mil gaya.

Dry Run 2
int a = 3;
if (a > 5);
    System.out.println("Inside");
System.out.println("Outside");
Show Answer

Output:

Inside
Outside

Bracket ke baad semicolon = khaali if body. Dono println if ke bahar hain, isliye dono chale.

Dry Run 3
int a = 10;
if (a > 5)
    System.out.println("Big");
    System.out.println("Always");
System.out.println("End");
Show Answer

Output:

Big
Always
End

Yahan a = 10 tha isliye teeno chhape. Follow-up socho: agar a = 2 hota toh output sirf “Always” aur “End” hota — “Always” if ke andar hai hi nahi.

Dry Run 4
int p = 4, q = 9;
String s = (p > q) ? "P" : (q % 2 == 0) ? "Q even" : "Q odd";
System.out.println(s);
Show Answer

Output:

Q odd

4 > 9 false → doosra ternary chala. 9 % 2 = 1, toh woh bhi false → “Q odd”.

Dry Run 5
int x = 5;
if (x = 5)
{
    System.out.println("Equal");
}
Show Answer

Output:

error: incompatible types: int cannot be converted to boolean

Yeh program chalta hi nahi — compile-time error hai. x = 5 ek int deta hai, aur if ko boolean chahiye. Sahi code: if (x == 5).

↑ Back to top

How This Chapter Is Examined

ICSE Computer Applications (subject code 86) mein Class X ka structure yeh hai, seedha CISCE ke syllabus document se:

Theory paper — ek written paper, do ghante, 100 marks.
Internal Assessment100 marks, poora practical: saal bhar mein kam se kam 20 laboratory assignments.
Paper ka pattern — Section A 40 marks (saare compulsory), Section B 60 marks (kisi bhi chaar questions ke answer do).
Is chapter ki jagah — Class X ka Unit 1 hai “Revision of Class IX Syllabus”, jismein Conditional constructs in Java saaf-saaf listed hai. Yaani yeh purana chapter nahi hai — yeh Class 10 ke paper mein poora examinable hai.

Practically iska matlab: Section A mein short output-prediction aur “difference between” questions (switch vs if-else if, break vs System.exit(0)), aur Section B mein poore programs — grade calculator, bill slabs, menu-driven programs. Aur haan, bitwise aur shift operators syllabus mein nahi hain, isliye unpar based conditions ki chinta mat karo.

↑ Back to top

Practice Worksheet (10 Questions)

Pehle khud likho, phir answer kholo. Har answer verified hai.

Q1. Java mein if ki condition kis data type ki honi chahiye, aur if (5) kyun galat hai?
Show Answer

Condition boolean honi chahiye. if (5) galat hai kyunki 5 ek int hai aur Java int ko automatically boolean mein convert nahi karta — compiler error deta hai.

Q2. if…else if ladder aur switch mein do farak likho.
Show Answer

(i) Ladder range aur kisi bhi boolean condition par kaam karti hai; switch sirf ek expression ki exact constant values par. (ii) Ladder float/double par bhi chalti hai; switch sirf byte, short, int, char aur String accept karta hai.

Q3. switch mein break ka kya kaam hai? Na lagane par kya hota hai?
Show Answer

break control ko switch se bahar nikaalta hai. Na lagane par fall-through hota hai — matching case ke baad ke saare cases (default samet) bhi chal jaate hain, unki value match ho ya na ho.

Q4. Output batao:
int day = 2;
switch (day)
{
    case 1: System.out.println("Monday");
    case 2: System.out.println("Tuesday");
    case 3: System.out.println("Wednesday");
    case 4: System.out.println("Thursday");
    default: System.out.println("Some other day");
}
Show Answer
Tuesday
Wednesday
Thursday
Some other day

Ek bhi break nahi hai, isliye case 2 se shuru hokar switch ke ant tak sab chala.

Q5. Ise ternary operator se likho: if (m >= 33) System.out.println("Pass"); else System.out.println("Fail");
Show Answer

System.out.println(m >= 33 ? "Pass" : "Fail");

Q6. break aur System.exit(0) mein kya farak hai?
Show Answer

break sirf apne switch ya loop se bahar nikaalta hai; program aage chalta rehta hai. System.exit(0) poora program (JVM) hi band kar deta hai — uske baad ki koi line nahi chalti. 0 normal termination darshata hai.

Q7. Kya switch (marks) mein case marks > 90: likha ja sakta hai? Kyun ya kyun nahi?
Show Answer

Nahi. case ke saamne ek constant value honi chahiye, koi condition ya range nahi. Ranges ke liye if…else if ladder use karo.

Q8. Output batao aur samjhao:
int a = 12, b = 45, c = 45;
if (a > b)
{
    if (a > c) System.out.println("a is greatest");
    else System.out.println("c is greatest");
}
else
{
    if (b > c) System.out.println("b is greatest");
    else if (c > b) System.out.println("c is greatest");
    else System.out.println("b and c are equal and greatest");
}
Show Answer
b and c are equal and greatest

12 > 45 false → else block. Wahan 45 > 45 false, 45 > 45 phir false → aakhri else chala.

Q9. Ek program likho jo char ch = 'K' ke liye switch use karke bataye ki woh vowel hai ya consonant — chahe capital ho ya small.
Show Answer
char ch = 'K';
switch (Character.toLowerCase(ch))
{
    case 'a': case 'e': case 'i': case 'o': case 'u':
        System.out.println("Vowel"); break;
    default:
        System.out.println("Consonant");
}

Output:

Consonant

Case ko lowercase mein badalne se dus alag cases likhne ki zaroorat nahi padti.

Q10. Even-odd check ke liye n % 2 == 1 likhna galat kyun hai? Sahi kya hai?
Show Answer

Java mein % ka result left operand ka sign leta hai. −7 % 2 → −1, jo 1 ke barabar nahi — toh −7 ko galti se “even” bata diya jaayega. Sahi test: n % 2 == 0 se even check karo, warna odd.

↑ Back to top

Aage kya? Conditionals se decisions banti hain, par kaam dohraane ke liye loops chahiye — agla chapter wahi hai. Peeche jaana ho toh Operators & Expressions, Values & Data Types aur Java Basics & OOP Concepts revise kar lo. Poora section: ICSE Class 10 hub.

Kaizen: Roz sirf ek switch-case snippet uthao aur break hata kar output dubara predict karo. Do hafte mein fall-through tumhare liye trap nahi, free marks ban jayega. 小さな改善, har din. 🌱

Written & reviewed by Team Principal Saab — Meet the team →