Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Java Conditional Statements

Uploaded by

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

Java Conditional Statements

Uploaded by

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

Computer Science

Java Programming
(Java Conditional Statements)

20/12/2024
CONTENTS

 If

 if-else

 nested if

 Ladder

 switch
Introduction
 In Java, decision-making enables us to write decision-driven
statements and execute code based on given conditions. Java if
statement executes a block of a statement if a condition is true,
and if a condition is false, it will not execute the code.

 However, there might be a situation where we want to take a


different action when a condition is false. That is where the else
statement comes in. By using the if else statements in Java
together, we can execute a block of code even when a condition is
false.

 If-else together refer to the set of conditional statements in Java


that are executed when the condition is true.

 If-else in Java is used in program execution with pre-defined


rules. Moreover, it allows you to run a specific block of code when
the condition is true; else, it will check other conditions. The
What Are Conditional Statements in Java?

 Conditional statements in Java are programming constructs


that allow you to control the flow of your program based on
certain conditions.

 They enable you to make decisions and execute different


blocks of code depending on whether a condition evaluates
Types of Conditional Statements in Java
to true or false.

1. if statement

2. if-else statement

3. nested if-else statement

4. if-else-if ladder

5. switch statement
 There are five types of if-else statements in Java, which are
as follows:
1. if statement: The if statement is the most basic conditional
statement in Java. It evaluates a boolean expression and executes a
block of code if the condition is true.

2. if-else statement: The if-else statement provides an alternative block


of code to execute when the condition in the if statement evaluates
to false.

3. nested if-else statement: The nested if-else statement allows you to


have multiple levels of conditions. It consists of multiple if-else
statements within each other.

4. if-else-if ladder: It is an extension of the if-else statement in Java. It


allows you to check multiple conditions in a cascading manner and
execute different blocks of code based on the condition that
evaluates to true.
Java if Statement
 The if statement in Java is a rule used when a condition is
true or satisfied. It tests the condition and executes the
block of code only when a condition is true.

Syntax of if Statement

if (condition) {
// code to be executed if condition is true
}

 In the if statement, the condition is a boolean expression that is


evaluated. If the condition evaluates to true, the code block
enclosed within the curly braces {} is executed.

 If the condition evaluates to false, the code block is skipped,


and the program continues to the next statement after the if
Java if Statement Examples
Example 1:

int num = 10;

if (num > 0) {

System.out.println("Number is positive");

Explanation:

 In this example, the if statement checks if the value of


the variable num is greater than 0. If the condition is
true, it executes the System.out.println statement,
which prints "Number is positive" to the console. Since
num is indeed greater than 0 (10 > 0), the message will
 The if statement can also be used with an optional else
statement to provide an alternative block of code to execute
when the condition is false. Here's an example:
Example 2:

int num = -5;


if (num > 0) {
System.out.println("Number is positive");
}
else {
System.out.println("Number is non-
positive");
}
Explanation:
 In this example, if the condition num > 0 evaluates to false
because num is -5, the code within the else block is executed,
which prints "Number is non-positive" to the console.
Java if-else Statement
 If else in Java is also a set of rules or statements performing a set of
conditions. Based on the rules, it executes the if block if the
condition is true; otherwise, it executes the else block.

Syntax of if-else Statement

if (condition) {
// code to be executed if condition is true
}
else {
// code to be executed if condition is false
}

 In the if-else statement, the condition is a boolean expression that


is evaluated. If the condition evaluates to true, the code block
enclosed within the curly braces {} after the if statement is
executed.

 If the condition evaluates to false, the code block enclosed within


Example of if else in Java
Example :

int num = 10;


if (num > 0) {
System.out.println("Number is positive");
}
else {
System.out.println("Number is non-positive");
}

Explanation:
 In this example, the if statement checks if the value of the variable
num is greater than 0. If the condition is true, it executes the code
block within the if statement, which prints "Number is positive" to
the console.

 If the condition is false, it executes the code block within the else
statement, which prints "Number is non-positive" to the console.
Java Nested if statement
 Nested if statement in Java represents the if-else statement within
another if-else statement. The inner if block executes only when the
outer if condition is true.
Syntax of Nested if Statement

if (condition1) {
// code to be executed if condition1 is true
if (condition2) {
// code to be executed if both condition1 and condition2 are
true
}
else {
// code to be executed if condition1 is true and condition2
is false
}
}
else {
 In the nested if statement, the outer if statement is
evaluated first. If its condition is true, the code block within
its body is executed.

 Inside the body of the outer if statement, there is another if


statement (the inner if statement) that can have its own
condition and corresponding code blocks.

 If the condition of the inner if statement is true, the code


block within its body is executed. If the condition of the
inner if statement is false, the code block within the else
statement (inside the outer if statement) is executed.
Example of nested if in Java

Example :

int num1 = 10;


int num2 = 5;
if (num1 > 0) {
System.out.println("Number 1 is positive");
if (num2 > 0) {
System.out.println("Number 2 is positive");
}
else {
System.out.println("Number 2 is non-positive");
}
}
else {
System.out.println("Number 1 is non-positive");
}
Explanation:

 In this example, the outer if statement checks if num1 is


greater than 0. If it is true, the code block within its body
is executed, which prints "Number 1 is positive" to the
console.

 Inside the body of the outer if statement, there is an inner


if statement that checks if num2 is greater than 0. If it is
true, it prints "Number 2 is positive" to the console.

 If the condition of the inner if statement is false, it prints


"Number 2 is non-positive" to the console. Since num1 is
10 and num2 is 5, both conditions are true, and the
corresponding messages will be printed.
Java if-else-if Ladder Statement
 The if else if ladder statement in Java executes one condition from
multiple statements. This set of rules is based on nested if condition
and performs the expression from top to down. If the condition is
true, the same statement is executed, and it skips the balance ladder
Syntax
rules. of if-else-if ladder Statement
if (condition1) {

// code to be executed if condition1 is true

} else if (condition2) {

// code to be executed if condition1 is false and condition2 is true

} else if (condition3) {

// code to be executed if condition1 and condition2 are false and


condition3 is true

} else {

// code to be executed if all conditions are false

}
 The if-else-if ladder starts with an initial if statement,
followed by one or more else if statements.

 Each else if statement allows you to specify a new condition


to check. If the condition in an if or else if statement
evaluates to true, the corresponding block of code is
executed.

 If none of the conditions are true, the code within the else
block is executed.
Example of if-else-if in Java

Example :

int num = 5;
if (num > 0) {
System.out.println("Number is positive");
}
else if (num < 0) {
System.out.println("Number is negative");
}
else {
System.out.println("Number is zero");
}
Explanation:

 In this example, the if-else-if ladder checks the value of


the variable num and prints a message based on its
value. If num is greater than 0, it prints "Number is
positive."

 If num is less than 0, it prints "Number is negative."


Otherwise, if none of the conditions are true, it prints
"Number is zero."
Switch Statement in Java
 In Java, the switch statement is a control flow statement that allows
you to select one of many code blocks to execute based on the value
of a variable or an expression. It provides a concise way to handle
multiple cases and avoid long chains of if-else statements.

Syntax of Switch Statement


switch (expression) {
case value1:
// code to be executed if expression matches value1
break;
case value2:
// code to be executed if expression matches value2
break;
// more cases
default:
// code to be executed if expression doesn't match any case
break;
}
 In the switch statement, the expression is evaluated, and
its value is compared against each case value.

 If a case value matches the value of the expression, the


corresponding code block is executed. The break statement
is used to terminate the switch block and exit the
statement.

 If a case doesn't have a break statement, the execution


continues to the next case until a break is encountered or
the switch block ends.

 The default case is optional and represents the code block


to be executed when none of the case values match the
Example of switch statement
Example :
int day = 3;
String dayName;

switch (day) {

case 1:
dayName = "Monday";
break;

case 2:
dayName = "Tuesday";
break;

case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;

case 5:
dayName = "Friday";
break;

default:
dayName = "Invalid day";
break;

}
System.out.println("The day is: " + dayName);
Explanation:

 In this example, the switch statement checks the value of


the variable day and assigns the corresponding day name
to the dayName variable.

 Since day is 3, the case 3 matches, and the code block


within that case is executed, assigning "Wednesday" to
dayName. The message "The day is: Wednesday" is then
printed to the console.

 The switch statement provides a more structured and


readable way to handle multiple cases compared to long
chains of if-else statements.

 It is commonly used when you have a fixed set of values or


constants to compare against and need to perform
If Else Programs in Java
1. Checking if a number is even or odd

int num = 10;

if (num % 2 == 0) {

System.out.println("The number is even.");

else {

System.out.println("The number is odd.");

 In this example, the program checks if the value of num is


divisible by 2. If the condition is true, it prints "The number is
even." Otherwise, it prints "The number is odd."
2. Finding the maximum of two numbers

int num1 = 10;

int num2 = 20;

if (num1 > num2) {

System.out.println("The maximum number is: " +

num1);

else {

System.out.println("The maximum number is: " +

num2);
In this example, the program compares the values of num1
and num2. If num1 is greater than num2, it prints "The
}
maximum number is: 10." Otherwise, it prints "The maximum
3. Checking if a year is a leap year

int year = 2024;

if ((year % 4 == 0 && year % 100 != 0) || year % 400


== 0) {
System.out.println(year + " is a leap year.");
}
else {
System.out.println(year + " is not a leap year.");
}

 In this example, the program checks if the value of year is a leap


year. It uses the condition (year % 4 == 0 && year % 100 != 0) ||
year % 400 == 0 to determine if it is divisible by 4 but not by 100,
or if it is divisible by 400. If the condition is true, it prints "2024 is
a leap year." Otherwise, it prints "2024 is not a leap year."
THANK YOU

You might also like