CodeGym /Java Course /Java Syntax /Conditional operators

Conditional operators

Java Syntax
Level 4 , Lesson 4
Available

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


"Hi, Amigo. Today we'll talk about if/else statements."

"Programs would be of little use if they didn't respond to changing external circumstances. A program needs to know how to adapt to circumstances and perform one action in one case and other actions in other cases. In Java, this is achieved using the 'if/else statement' – a special construct that makes it possible to perform different code blocks if a condition is satisfied."

"It consists of three parts: 'condition', 'command 1' and 'command 2'. If the condition is true, then 'command 1' is executed, otherwise 'command 2' is executed. These commands are never both executed. The statement looks more or less like this:"

Code for an if/else statement
if (condition)
    command_1;
else
    command_2;

"How exciting! I think that statement will make programming much more interesting!"

"Yep. Here are a couple of examples for you:"

Code Explanation
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
If a is less than b, the first command will be executed. Otherwise the second command will be executed. The commands are never both executed.
2
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
You can replace one command with a code block. The rest is the same.
3
if (a < b)
{
    a = 0;
}
else
{
}
You can omit the else block if it's empty.
These three examples are entirely equivalent.
You can omit the curly brackets if you only need to execute one command. If you have more than one command, you need to keep the brackets.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

"Diego just asked me to give you a few tasks."

4
Task
New Java Syntax, level 4, lesson 4
Locked
10 numbers
Display the numbers from 1 to 10 using a while loop. Each value should be on a new line.
4
Task
New Java Syntax, level 4, lesson 4
Locked
From 10 to 1
Display the numbers from 10 to 1 using a while loop. Each value should be on a new line.
4
Task
New Java Syntax, level 4, lesson 4
Locked
You can't have too much of a good thing
Use the keyboard to enter a string and a number N greater than 0. Use a while loop to display the string N times. Each value should be on a new line. Example input: abc 2 Example output: abc abc
4
Task
New Java Syntax, level 4, lesson 4
Locked
Seeing dollars in your future
Use a while loop to display a 10x10 square of dollar signs. Don't separate the symbols in each line. Example output: $$$$$$ $$$$$ $$$$$ $$$$$ $$$$$ $$$$$ $$$$$ $$$$$ $$$$$ $$$$$

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


Comments (143)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
nealgoogs Level 21, Eliot, United States
15 August 2024
last one, switch statement is better
rags 3 Level 5, Wichita, United States Expert
26 April 2024
On the "day of the week one I used an array[] work perfect but didnt like spelling for some reason.
Anonymous #11394127 Level 3, Ethiopia
13 September 2023
how can open the next lesson
John Squirrels Level 41, San Francisco, Poland
14 September 2023
Just click the Unlock button at the end of the lesson like you did before.
Anonymous #11263161 Level 3, Dearborn, United States
24 June 2023
Also, for
Anonymous #11263161 Level 3, Dearborn, United States
24 June 2023
On "Good or Bad," the sentence should really be "The number is equal TO or greater than 5." The grammar is not as it usually is in programming or even math. Also another minor gripe, why not just have periods end both strings?
qaz_gangnam Level 16, Korea, Republic of
6 May 2023
Omg! I'm so proud of myself! I spent almost an hour on "Crossing the road blindly", but I solved it!! and it's beautiful *u*
Lunita Level 4, Dominican Republic
5 February 2023
In these tasks we practice if-then-else, Logical Operators, keyboard input, operations, etc. We got this! 😘
Robbie_Robot Level 4, California, United States
4 October 2022
@johnsquirrels, So in the first lecture I see the slide the speaker is referencing is titled, someusers/directory/codegym/Conditionalstatement_US_.pdf Where do we access these .pdf learning materials??
John Squirrels Level 41, San Francisco, Poland
7 October 2022
It is a part of our CodeGym University. Please apply here if you want to enroll.
Omar Pérez Pacheco Level 6, Mexico
2 September 2022
Hice el código de la siguiente manera y si me arroja los resultados que solicita. Intenté con Netbeans para probarlo y poner distintos números y si me sale como piden la solución. Me podrías ayudar en que estoy mal, porfa
Ozkin Level 20, Colombia Expert
4 November 2022
Simple estas bien solo cambia el orden, primero revisa que la condicion de las 3 sena iguales antes de tofo, quita el println y solo deja el print
calibers16 Level 6, Knowhere, United States
13 August 2022
Now we're getting to the fun stuff! Love me some else-if statements!