CodeGym /Java Course /Module 1. Java Syntax /Conditional statement

Conditional statement

Module 1. Java Syntax
Level 4 , Lesson 0
Available

1. The if-else statement

Programs wouldn't be very useful if they always did the same thing, regardless of how external circumstances change. A program needs to be able to adapt to different situations and take certain actions in some situations, and to act differently in others.

In Java, this is done with a conditional statement, which uses a special keyword that lets you execute different blocks of commands depending on the truth value of a condition.

A conditional statement consists of three parts: condition, statement 1 and statement 2. If the condition is true, then statement 1 is executed. Otherwise statement 2 is executed. Both commands are never executed. Here's the general appearance of this kind of statement:

if (condition)
   statement 1;
else
   statement 2;
The if-else conditional statement

It is quite understandable when written in plain English like this:

If condition is true, then
   execute statement 1;
otherwise
   execute statement 2;
The if-else statement in plain language

Examples:

Code Explanation
int age = 17;
if (age < 18)
   System.out.println("You are still a child");
else
   System.out.println("You are now an adult");
The screen output will be:
You are still a child
int temperature = 5;
if (temperature < 0)
   System.out.println("It's freezing outside");
else
   System.out.println("It's warm");
The screen output will be:
It's warm
int age = 18;
if (age == 18)
   System.out.println("You've been drafted for military service");
else
   System.out.println("Report for duty anyway");
The screen output will be:
You've been drafted for military service


2. Block of statements

If the condition is satisfied (or not) and you want your program to execute several commands, you can combine them into a block.

To combine commands into a block, you "wrap" them in curly braces. Here's how it looks in general:

{
   statement 1;
   statement 2;
   statement 3;
}

You can have as many statements as you want in a block. Or even none.

Examples of an if-else statement combined with a block of statements:

Code Explanation
int age = 17;
if (age < 18)
{
   System.out.println("You are still a child");
   System.out.println("Don't talk back to adults");
}
else
{
   System.out.println("You are now an adult");
   System.out.println("And thus ends your youth");
}
The screen output will be:
You are still a child
Don't talk back to adults
int temperature = 5;
if (temperature < 0)
{
   System.out.println("It's freezing outside");
   System.out.println("Put on a hat");
}
else
   System.out.println("It's warm");
The screen output will be:
It's warm
int age = 21;
if (age == 18)
   System.out.println("You've been drafted for military service");
else
{
}
The empty block will be executed.
The code will run fine, but nothing will be displayed.

3. Abbreviated form of the if statement

Sometimes it you need to execute one or statements if the condition is true but nothing should be done if it is false.

For example, we can specify this command: If Bus No. 62 has arrived, then get aboard, but don't react if the bus isn't here. In Java, this scenario lets us use an abbreviated form: an if statement without an else block.

In other words, if statements(s) needs to be executed only if the condition is true and there are no commands to be executed when the condition is false, then you should use the if statement, which is concise and omits the else block. It looks like this:

if (condition)
   statement 1;
The if conditional statement

Below are three examples of equivalent code:

Code Explanation
int age = 18;
if (age == 18)
{
   System.out.println("You've been drafted for military service");
}
else
{
}
The screen output will be:
You've been drafted for military service

The program has an else block, but it is empty (there are no statements between the curly braces). You can simply remove it. Nothing will change in the program.

Code Explanation
int age = 18;
if (age == 18)
{
   System.out.println("You've been drafted for military service");
}
The screen output will be:
You've been drafted for military service
int age = 18;
if (age == 18)
   System.out.println("You've been drafted for military service");
The screen output will be:
You've been drafted for military service

Comments (27)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
haalk3n Level 6, Romania
29 January 2024
Hello 2024!
Anar Hasanov Level 23, Baku, Azerbaijan Expert
4 November 2023
Both commands are never executed at once*
Anonymous #11416402 Level 24, China
4 November 2023
It is really confusing if I havn't watched your correction :) Thanks
Anonymous #11416402 Level 24, China
4 November 2023
Sometimes it you need to execute one or more statements if the condition is true but nothing should be done if it is false.*
Abhishek Tripathi Level 72, Rewa, India Expert
29 June 2023
you know This is quite easy as compared with objects and class
Youssef Aissa Level 3, Morocco
16 June 2023
🤓🤓
MAYA Level 4, Bangalore, India
26 December 2022
pls explain about scanner
Parsa Level 62, Bangalore, India Expert
12 October 2023
Vinay Kulkarni Level 4, CodeGym University in India, India Expert
9 December 2022
package CodeGymProject; import java.util.Scanner; public class militaryCommisar { public static void main(String[] args) { String militaryCommissar = ", you've been drafted for military service"; Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int age = scanner.nextInt(); if (age >= 18 && age <= 28) { System.out.println(name + militaryCommissar); } } } NOT RUNNING
Anonymous #11315796 Level 4, Franklin, United States
17 March 2023
Why did you rename your class?
Nisha Kumari Level 4, India
3 July 2023
Can't we use if (18<age<28) ??
David Muñoz Level 4, Chicago, United States
28 July 2023
i tried that too and it would not work. searching up on google i found this other way if (edad >= 18) { if (edad <= 28){ System.out.println(nombre + militaryCommissar); } else{ } }
Parsa Level 62, Bangalore, India Expert
12 October 2023
Assuming you did it on the website's IDE, you changed both the package name and the class name. The class name is Solution, which is the file name. But if you did it on intellij, I have no idea, because the code is fine. P.S. Class name should be LikeThis. The first letter should be capital too. Not important, but just convention. But make sure it's the same as your file name.
Anar Hasanov Level 23, Baku, Azerbaijan Expert
4 November 2023
Can't we use if (18<age<28) ?? No you can't. Java defines this (18<age) as Boolean(True or False), so you can't compare Boolean to Integer.
Anonymous #11161389 Level 3, Trollfjord, Norway
17 November 2022
Have we learnt the command of getting an condition of 18-28 in this course before the task?
Evan Level 33, United States of America, USA
5 December 2022
Try using the logical AND operator, if you are still stuck!
Vinayak Ghadigaokar Level 7, Mumbai, India
19 October 2022
I wrote below code and it shows me error for no reason someone needs to check this. Scanner input = new Scanner(System.in); String name = input.nextLine(); int age = input.nextInt(); if(age>18 && age<28){ System.out.println(name + militaryCommissar); }
Maksim Chystabaeu Level 7, Belarus
7 November 2022
you should do this ">=" and "<="
Anonymous #11140964 Level 3, United States of America, United States
15 November 2022
Yes, the task was for the range 18 to 28 to be inclusive which means we needed to check that the age was greater than or equal to 18 and less than or equal 28. Your if statement was exclusive and only returned true if the age were to be 19 to 27.
Anonymous #11127758 Level 9, San Antonio, United States
7 October 2022
Can someone explain why this doesn't meet the fourth requirement, by not printing out anything if not in 18-28 range? I checked the "correct answer" and it has a similar style. Scanner scan=new Scanner(System.in); String name=scan.nextLine(); int age=scan.nextInt(); if (age<=28&&age>=18);{ //&&or|| System.out.println(name+militaryCommissar); }
Anonymous #11140964 Level 3, United States of America, United States
15 November 2022
You do not place a semicolon after the condition to be checked of an if statement. if(condition) { things to be done if true; } You code has a semicolon after the condition of the if statement making it unable to compile. if(condition); { things to be done if true; }
Oege Hiddema Level 32, Groningen, Netherlands
20 June 2022
Because the if-else statement uses a condition, I think it would be better to start with booleans and logical operators first and after that, treat the if-else statement.