0% found this document useful (0 votes)
2 views

Class_9_ICSE_Conditional_Statements_in_Java

Uploaded by

spam16543
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Class_9_ICSE_Conditional_Statements_in_Java

Uploaded by

spam16543
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Notes for Class 9 ICSE Students: Condi onal Statements in Java

Introduc on

 Condi onal statements in Java control the flow of execu on based on certain condi ons.

 These condi ons evaluate to either true or false.

 Based on the result, specific code blocks are executed or skipped.

Normal Flow of Control

 In Java, the program executes statements sequen ally by default.

 This is referred to as the Normal Flow of Control.

Condi onal Flow of Control

1. if Statement

o Executes a block of code only if the condi on is true.

o Syntax:

if (condi on) {

// Code to execute if condi on is true

o Example:

int age = 18;

if (age >= 18) {

System.out.println("You are eligible to vote.");

2. if-else Statement

o Executes one block if the condi on is true, another if it's false.

o Syntax:

if (condi on) {

// Code to execute if condi on is true

} else {

// Code to execute if condi on is false

}
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 Checks mul ple condi ons sequen ally.

o Syntax:

if (condi on1) {

// Code for condi on1

} else if (condi on2) {

// Code for condi on2

} else {

// Code if none of the above condi ons are true

o Example:

int marks = 85;

if (marks >= 90) {

System.out.println("Grade: A");

} else if (marks >= 75) {

System.out.println("Grade: B");

} else {

System.out.println("Grade: C");

4. Nested if Statements

o An if statement within another if.


o Syntax:

if (condi on1) {

if (condi on2) {

// Code to execute if both condi ons are true

o Example:

int age = 20;

int income = 30000;

if (age >= 18) {

if (income >= 25000) {

System.out.println("You are eligible for a loan.");

5. System.exit(0)

o Terminates the program immediately.

o Syntax:

System.exit(0);

o Example:

System.out.println("Exi ng program.");

System.exit(0);

Mul ple Branching of Control

1. switch-case Statement

o Selects one block of code to execute from mul ple op ons.

o Syntax:

switch (expression) {

case value1:

// Code for case value1

break;

case value2:
// Code for case value2

break;

default:

// Code if none of the cases match

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");

2. Fall Through in switch-case

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 Use switch-case to create programs that offer choices to the user.

o Example:

import java.u l.Scanner;

public class MenuExample {

public sta c void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Menu:");

System.out.println("1. Add");

System.out.println("2. Subtract");

System.out.println("3. Mul ply");

System.out.println("Enter your choice:");

int choice = sc.nextInt();

System.out.println("Enter two numbers:");

int num1 = sc.nextInt();

int num2 = sc.nextInt();

switch (choice) {

case 1:

System.out.println("Sum: " + (num1 + num2));

break;

case 2:

System.out.println("Difference: " + (num1 - num2));

break;

case 3:

System.out.println("Product: " + (num1 * num2));

break;

default:
System.out.println("Invalid choice");

Key Points to Remember

 if statements are used for simple condi ons.

 switch-case statements are used for mul ple, fixed values.

 Use break to prevent fall-through in switch-case.

 Nested if statements and if-else-if ladders allow handling complex condi ons.

 System.exit(0) terminates the program immediately.

You might also like