Class_9_ICSE_Conditional_Statements_in_Java
Class_9_ICSE_Conditional_Statements_in_Java
Introduc on
Condi onal statements in Java control the flow of execu on based on certain condi ons.
1. if Statement
o Syntax:
if (condi on) {
o Example:
2. if-else Statement
o Syntax:
if (condi on) {
} else {
}
o Example:
int num = 5;
if (num % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
3. if-else-if Ladder
o Syntax:
if (condi on1) {
} else {
o Example:
System.out.println("Grade: A");
System.out.println("Grade: B");
} else {
System.out.println("Grade: C");
4. Nested if Statements
if (condi on1) {
if (condi on2) {
o Example:
5. System.exit(0)
o Syntax:
System.exit(0);
o Example:
System.out.println("Exi ng program.");
System.exit(0);
1. switch-case Statement
o Syntax:
switch (expression) {
case value1:
break;
case value2:
// Code for case value2
break;
default:
o Example:
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;
default:
System.out.println("Invalid day");
o If the break statement is omi ed, control falls through to subsequent cases.
o Example:
int num = 2;
switch (num) {
case 1:
System.out.println("Case 1");
case 2:
System.out.println("Case 2");
case 3:
System.out.println("Case 3");
}
// Output:
// Case 2
// Case 3
3. Menu-Driven Programs
o Example:
System.out.println("Menu:");
System.out.println("1. Add");
System.out.println("2. Subtract");
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
System.out.println("Invalid choice");
Nested if statements and if-else-if ladders allow handling complex condi ons.