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.
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);
}
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.
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.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.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.
The if…else Statement
Do raaste, aur exactly ek chalega. Yaad rakho: else ka apna koi condition nahi hota — woh “baaki sab kuch” hai.
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.
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.
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 EndYahan 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.
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.
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.
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.
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.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.
marks >= 40 sabse upar rakh diya, toh 95 marks wale ko bhi wahi mil jayega.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.
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.
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.
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.
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.
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.
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.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);
}
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.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.
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.
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.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.
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.
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.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.
break na dikhe, neeche padhte raho. Har saal koi na koi student “Tuesday” likh kar teen lines ke marks gawa deta hai.Menu-Driven Programs and System.exit(0)
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”.
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.
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.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.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.
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.
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.
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.
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”.
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).
How This Chapter Is Examined
ICSE Computer Applications (subject code 86) mein Class X ka structure yeh hai, seedha CISCE ke syllabus document se:
Internal Assessment — 100 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.
Practice Worksheet (10 Questions)
Pehle khud likho, phir answer kholo. Har answer verified hai.
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.
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.
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.
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.
if (m >= 33) System.out.println("Pass"); else System.out.println("Fail");Show Answer
System.out.println(m >= 33 ? "Pass" : "Fail");
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.
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.
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.
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.
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.
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. 🌱
