Conditional statements and conditional operation
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
April 14, 2025
else
and else if
statementsOutput
if
followed by parentheses.{
and closing }
curly brackets.Note
An if
-statement is not followed by a semicolon since the statement doesn’t end after the conditional.
Sample Output 1
Sample Output 2
public class
, which is followed by the name of the program and the opening curly bracket of the block.public static void main(String[] args)
in the programs begins a block, and the source code within it is executed when the program is run – this snippet is, in fact, the starting point of all programs.if
command that started the conditional statement.}
character, the indentation also ends.}
character is at the same level of indentation as the if
-command that started the conditional statement.The example below is correctly indented.
Automatic code identation
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Give a number: ");
int first = Integer.valueOf(scan.nextLine());
System.out.println("Give another number: ");
int second = Integer.valueOf(scan.nextLine());
if (first == second) { System.out.println("Same!"); }
else if (first > second) { System.out.println("The first was larger than the second!"); }
else {
System.out.println("The second was larger than the first!");
}
}
}
>
greater than>=
greater than or equal to<
less than<=
less than or equal to==
equal to!=
not equal toint number = 55;
if (number != 0) {
System.out.println("The number is not equal to 0");
}
if (number >= 1000) {
System.out.println("The number is at least 1000");
}
Output
Sample Output 1
Sample Output 2
Sample Output 1
Sample Output 2
else
command, which is used together with the if
command.int number = 4;
if (number > 5) {
System.out.println("Your number is greater than five!");
} else {
System.out.println("Your number is five or less!");
}
Output
else
branch has been specified for a conditional statement, the block defined by the else branch is run in the case that the condition of the conditional statement is false.else
-command is placed on the same line as the closing bracket of the block defined by the if
-command.Sample Output 1
Sample Output 2
Sample Output 1
Sample Output 2
else if
else if
command.else if
is like else
, but with an additional condition.else if
follows the if
-condition, and they may be multiple.Let’s read out the example below to our duck partner:
Output
Sample Output 1
Sample Output 2
int number = 5;
if (number == 0) {
System.out.println("The number is zero.");
} else if (number > 0) {
System.out.println("The number is greater than zero.");
} else if (number > 2) {
System.out.println("The number is greater than two.");
} else {
System.out.println("The number is less than zero.");
}
Output
number > 2
is also true.points | grade |
---|---|
< 0 | impossible! |
0-49 | failed |
50-59 | 1 |
60-69 | 2 |
70-79 | 3 |
80-89 | 4 |
90-100 | 5 |
> 100 | incredible! |
boolean
after the evaluation. boolean
type variables are either true or false.Output
Output
isGreater
now contains the boolean value false.isItLessThan
has true
as its value.if (isItLessThan)
– the value for the variable isItLessThan
is found in its container, and the program finally prints:%
.Scanner reader = new Scanner(System.in);
int number = Integer.valueOf(reader.nextLine());
int remainder = number % 400;
if (remainder == 0) {
System.out.println("The number " + number + " is divisible by four hundred.");
} else {
System.out.println("The number " + number + " is not divisible by four hundred.");
}
%
in conditional statementsSample Output 1
Sample Output 2
%
operator.System.out.println( 1%2 );
System.out.println( 2%2 );
System.out.println( 3%2 );
System.out.println( 4%2 );
System.out.println( 5%2 );
int ocho = 8
System.out.println( ocho%2 );
variable1 == variable2
), we cannot compare the equality of strings using two equals signs.Scanner reader = new Scanner(System.in);
System.out.println("Enter the first string");
String first = reader.nextLine();
System.out.println("Enter the second string");
String second = reader.nextLine();
if (first == second) {
System.out.println("The strings were the same!");
} else {
System.out.println("The strings were different!");
}
==
operator?equals
command, which is related to string variables.equals
commandequals
command is written after a string by attaching it to the string to be compared with a dot.equals
Scanner reader = new Scanner(System.in);
System.out.println("Input two strings");
String first = reader.nextLine();
String second = reader.nextLine();
if (first.equals(second)) {
System.out.println("The strings were the same!");
} else {
System.out.println("The strings were different!");
}
if (first.equals("two strings")) {
System.out.println("Clever!");
}
if (second.equals("two strings")) {
System.out.println("Sneaky!");
}
Sample otuput 1
Sample otuput 2
Sample otuput 3
Sample Output 1
Sample Output 2
Sample Output 1
Sample Output 2
&&
, or ||
, and not !
are used.
&&
, i.e., the and-operator.||
||
, i.e., the or-operator: is the number less than zero or greater than 100.System.out.println("Is the number less than 0 or greater than 100");
int number = 145;
if (number < 0 || number > 100) {
System.out.println("It is! :)");
} else {
System.out.println("It is not :(")
}
Output
!
number > 4
using !
, i.e., the not-operator.int number = 7;
if (!(number > 4)) {
System.out.println("The number is not greater than 4.");
} else {
System.out.println("The number is greater than 4.")
}
Output
Below is a table showing the operation of expressions containing logical operators.
number | number > 0 | number < 10 | number > 0 && number < 10 | !(number > 0 && number < 10) | number > 0 || number < 10 |
---|---|---|---|---|---|
-1 | false | true | false | true | true |
0 | false | true | false | true | true |
1 | true | true | true | false | true |
9 | true | true | true | false | true |
10 | true | false | false | true | true |
Let’s familiarize ourselves with the execution order of conditional statements through a classic programming exercise.
‘Write a program that prompts the user for a number between one and one hundred, and prints that number. If the number is divisible by three, then print “Fizz” instead of the number. If the number is divisible by five, then print “Buzz” instead of the number. If the number is divisible by both three and five, then print “FizzBuzz” instead of the number.’_
The programmer begins solving the exercise by reading the exercise description and by writing code according to the description.
The conditions for execution are presented in a given order by the description, and the initial structure for the program is formed based on that order.
The structure is formed based on the following steps:
if
type conditions are easy to implement using if - else if - else
-conditional statements.Proposed solution
Scanner reader = new Scanner(System.in);
int number = Integer.valueOf(reader.nextLine());
if (number % 3 == 0) {
System.out.println("Fizz");
} else if (number % 5 == 0) {
System.out.println("Buzz");
} else if (number % 3 == 0 && number % 5 == 0) {
System.out.println("FizzBuzz");
} else {
System.out.println(number);
}
Proposed solution
Scanner reader = new Scanner(System.in);
int number = Integer.valueOf(reader.nextLine());
if (number % 3 == 0 && number % 5 == 0) {
System.out.println("FizzBuzz");
} else if (number % 3 == 0) {
System.out.println("Fizz");
} else if (number % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(number);
}
%
, in the following way.int number = 5;
if (number % 5 == 0) {
System.out.println("The number is divisible by five!");
}
if (number % 6 != 0) {
System.out.println("The number is not divisible by six!")
}
Output
if
, else if
, else if
, … comparisons, and start building the program from a situation in which you can be certain that the year is not a leap year.Computer Language