0% found this document useful (0 votes)
5 views9 pages

Conditional Statements Smaller Font

Conditional statements in programming enable decision-making based on conditions, controlling the flow of execution. Java examples illustrate the use of `if`, `if-else`, `if-else-if`, nested statements, logical operators, and the `switch` statement. These constructs allow for complex logic and structured checks of multiple values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Conditional Statements Smaller Font

Conditional statements in programming enable decision-making based on conditions, controlling the flow of execution. Java examples illustrate the use of `if`, `if-else`, `if-else-if`, nested statements, logical operators, and the `switch` statement. These constructs allow for complex logic and structured checks of multiple values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Conditional Statements in

Programming
Understanding Conditional
Statements with Java Examples
What Are Conditional Statements?

• Conditional statements allow programs to make decisions based on conditions.

They control the flow of execution depending on whether a condition is True or


False.
The if Statement

• Example in Java:

int age = 20;


if (age >= 18) {
System.out.println("You are an adult");
}
The if-else Statement

• Example in Java:

int age = 16;


if (age >= 18) {
System.out.println("You are an adult");
} else {
System.out.println("You are a minor");
}
The if-else-if Ladder

• Example in Java:

int score = 85;


if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
Nested Conditional Statements

• Example in Java:

int age = 20;


boolean hasLicense = true;
if (age >= 18) {
if (hasLicense) {
System.out.println("You can drive");
} else {
System.out.println("You need a license");
}
} else {
System.out.println("You are too young to drive");
}
Logical Operators in Conditions

• Logical operators help combine multiple conditions:

- `&&`: Both conditions must be True


- `||`: At least one condition must be True
- `!`: Reverses the condition

Example:
if (age >= 18 && hasLicense) {
System.out.println("You can drive");
}
The Switch Statement

• Example in Java:

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");
}
Summary of Conditional
Statements
• - Conditional statements control program flow.
- `if`, `if-else`, and `if-elif-else` handle different conditions.
- Nested conditions allow complex logic.
- Logical operators (`&&`, `||`, `!`) help combine conditions.
- `switch` provides a structured way to check multiple exact values.

You might also like