Java If…Else
Choose the path that matches the condition.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind If … Else.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
An if/else chain checks conditions in order. The first true branch runs, and the rest of that chain is skipped. Put more specific or stricter thresholds before broader ones when the conditions overlap.
விரிவான விளக்கங்கள்
Java Conditions மற்றும் If Statements
Java-ல different conditions-க்கு different actions execute பண்ண if statements use பண்றோம்.
Java-ல இருக்குற conditional statements:
if— condition true-ஆ இருந்தா execute ஆகும்else— condition false-ஆ இருந்தா execute ஆகும்else if— முந்தைய condition false-ஆ இருந்தா new condition test பண்றதுக்குswitch— நிறைய possible values test பண்றதுக்கு
if Statement
Condition true-ஆ இருந்தா மட்டும் code execute ஆகும்:
else Statement
Condition false-ஆ இருந்தா இந்த block execute ஆகும்:
else if Statement
Multiple conditions test பண்றதுக்கு:
Short Hand If…Else (Ternary Operator)
If-else-ஐ ஒரே line-ல எழுதலாம்:
int score = 75;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 70) {
System.out.println("C");
} else {
System.out.println("Below C");
}
// Output: C
// Ternary shorthand
String result = (score >= 50) ? "Pass" : "Fail";
System.out.println(result); // PassTernary operator-ன் syntax என்ன?
- if(condition) ? value1 : value2
- condition ? value1 : value2
- condition then value1 else value2
- (condition) { value1 } else { value2 }
Reveal answer / விடை
condition ? value1 : value2
Ternary operator: `condition ? valueIfTrue : valueIfFalse`. Example: `String result = age >= 18 ? "Adult" : "Minor";` — simple if-else-ஐ one line-ல் எழுதலாம்.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int score = 75;
if (score >= 90) {
System.out.println("Excellent");
} else if (score >= 60) {
System.out.println("Pass");
} else {
System.out.println("Try again");
}
}
}Predict first. Then reveal the expected output / வெளியீடு
Pass
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Test 59, 60, 89, and 90. Which branch handles each boundary?
Reveal the answer and reasoning / விடை
59 → Try again; 60 → Pass; 89 → Pass; 90 → Excellent.
தவிர்க்க வேண்டிய தவறு: Writing score >= 60 before score >= 90 would make the later excellent branch unreachable for these values.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.