0% found this document useful (0 votes)
3 views15 pages

Java Conditions and If Statements

The document provides an overview of Java conditional statements, including 'if', 'else', and 'else if', along with their syntax and usage. It explains how to perform logical comparisons and execute different blocks of code based on specified conditions. Additionally, it includes examples and activities to practice using these statements in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Java Conditions and If Statements

The document provides an overview of Java conditional statements, including 'if', 'else', and 'else if', along with their syntax and usage. It explains how to perform logical comparisons and execute different blocks of code based on specified conditions. Additionally, it includes examples and activities to practice using these statements in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Java Java Conditions and If

Java If ... Else Statements


You already know that Java supports the
usual logical conditions from mathematics:

Java Conditions
•Less than: a < b
and If Statements
•Less than or equal to: a <= b
•Greater than: a > b
•Greater than or equal to: a >= b
•Equal to a == b
•Not Equal to: a != b
Java Java Conditions and If
Java If ... Else Statements
Java has the following conditional
You can use these statements:
conditions to
perform different  Use if to specify a block of code to be
actions for different executed, if a specified condition is true
 Use else to specify a block of code to be
decisions. executed, if the same condition is false
 Use else if to specify a new condition to
test, if the first condition is false
 Use switch to specify many alternative
blocks of code to be executed
Java
The if Statement
Java If ... Else
Use the if statement to Syntax
specify a block of Java
code to be executed if a if (condition) {
condition is true.
// block of code to be executed if the
condition is true

Note that if is in lowercase letters.


Uppercase letters (If or IF) will generate an
error.
Java
The if Statement
Java If ... Else
Use the if statement to Example 1
specify a block of Java
code to be executed if a
condition is true.
Java
The if Statement
Java If ... Else
Use the if statement to Activity 1
specify a block of Java
code to be executed if a
Write a program that compares two numbers of
condition is true.
your choice and print if these numbers are
equal or one is greater or less than the other.

Do not use the same numbers as used in


Example 1.
Java
The if Statement
Java If ... Else
Use the if statement to Example 2
specify a block of Java We can also test variables:
code to be executed if a
condition is true.
Java
The if Statement
Java If ... Else
Use the if statement to
Example 2 – Explained
specify a block of Java
code to be executed if a In the example above we use two variables, x and y,
condition is true. to test whether x is greater than y (using the >
operator).

As x is 20, and y is 18, and we know that 20 is


greater than 18, we print to the screen that "x is
greater than y".
Java
The if Statement
Java If ... Else
Use the if statement to Activity 2:
specify a block of Java
code to be executed if a
Write a program that we use two variables, a
condition is true.
and b, to test whether a is greater than or
equal to b (using an appropriate operator).

As a is greater than or equals to b then print to


the screen that “a is greater than or equals to
b".
Java
The if Statement
Java If ... Else
Use the if statement to
Example 2 – Explained
specify a block of Java
code to be executed if a In the example above we use two variables, x and y,
condition is true. to test whether x is greater than y (using the >
operator).

As x is 20, and y is 18, and we know that 20 is


greater than 18, we print to the screen that "x is
greater than y".
Java
The else Statement
Java If ... Else
Syntax
Use the else statement to
specify a block of code to
if (condition) {
be executed if the
condition is false. // block of code to be executed if the
condition is true
} else {
// block of code to be executed if the
condition is false
}
Java
The else Statement
Java If ... Else
Example 3
Use the else statement to
specify a block of code to
be executed if the
condition is false.
Java
The else Statement
Java If ... Else
Example 3 – Explained
Use the else statement to
specify a block of code to
be executed if the  In the example above, time (2000) is greater
condition is false. than 1800, so the condition is false.
 Because of this, we move on to the else
condition and print to the screen "Good
evening".
 If the time was less than 1800, the program
would print "Good day".
Java
The else Statement
Java If ... Else
Activity 3
Use the else statement to
specify a block of code to
be executed if the Write a program that will print to the screen
condition is false. "Good Afternoon“ if the time (timeNow) is
greater than time (1200).

Else will print to the screen “Good Morning” if


the time (timeNow) is less than time (1200). This
program will use two int variables, timeNow
and time
Java Java If ... Else
Java If ... Else Real-Life Examples
Real-Life Examples Real-Life Example 1
This example shows how int doorCode = 98798;
you can use if..else to
"open a door" if the user // Add appropriate Boolean expression.
enters the correct code: if (doorCode _________) {
Copy and complete the System.out.println("Correct code. The door
code. is now open.");
} else { System.out.println("Wrong code.
The door remains closed.");
}
Java Java If ... Else
Java If ... Else Real-Life Examples
Real-Life Examples Real-Life Example 2
This example shows how
int myAge = ______;
you can use if..else to int votingAge = ______;
find out if a person is old if (myAge _________) {
enough to vote: System.out.println("Old enough to vote!");
} else {
Copy and complete the System.out.println("Not old enough to vote.");
code. }
// Add appropriate Boolean expression.
// Initialized the two variables.

You might also like