Section 02 Conditionals
Section 02 Conditionals
© luv2code LLC
Conditionals: if/else
• Conditional statements can make decisions based on conditions
} else
{
int minVotingAge = 18
} else
{
} else
{
scanner.close();
int minVotingAge = 18
if (eligible)
{
} else
{
scanner.close();
|| logical OR
! logical NOT
int maxClassCount = 5
userCount=
maxClassCount=
System.out.println("minClassCount=" + minClassCount)
System.out.println("maxClassCount=" + maxClassCount)
System.out.println() and
;
} else
{
minClassCount=
maxClassCount=
int minClassCount = 2 Check if userCount is an Your class count is in the recommended rang
int maxClassCount = 5
even number or odd number You have selected an odd number of classes.
int userCount = 3
;
System.out.println("Your class count is in the recommended range") Returns the remainder of division
} else
{
} else
{
System.out.println("Low grade. You did not score in the top two tiers.")
fi
Ternary operator
String message = (age >= minVotingAge) ? "You are eligible to vote" : "You are NOT eligible to vote";
Traditional
} else
Reduces amount of code …
{
Ternary
String message = (age >= minVotingAge) ? "You are eligible to vote" : "You are NOT eligible to vote"
System.out.println(message)
;
© luv2code LLC
Comparing Strings
• Comparing Strings in Java can be very trick
Best Practice
String s2 = "Hello"
s1: Hell
What???
String s3 = new String("Hello")
;
== operator:
s1: Hell
System.out.println("s1: " + s1) compares the
s1 == s3: false
;
• If the String literal already exists in the String pool, it will reuse the String referenc
• Hence, they have the same memory address … point to same location
String s1 = "Hello"
;
String s2 = "Hello"
;
compares the
s2: Hell
;
s1 == s2: true
;
• Hence, they do NOT have the same memory address … point to different locations s2
s3 Hello
String s1 = "Hello"
;
compares the
System.out.println("s1: " + s1) s3: Hell memory address
String s1 = "Hello";
String s3 = new String("Hello")
s1: Hell
s4: hell
© luv2code LLC
switch Statement
• switch statement can make decisions based on a value(s
switch (variable)
case value1:
// execute this block of code
break;
case value2:
// execute this block of code
break;
default:
// default block of code
}
case value1:
// execute this block of code
break;
• The switch statement has a variable case value2:
// execute this block of code
break;
default:
Discussed later in
the course
• break: exits the case block to prevent executing subsequent case blocks
switch (computerType)
{
case "smartphone"
:
break
;
case "tablet"
:
break
;
}
Tablets are lightweight for browsing and light tasks.
switch (computerType)
case "smartphone"
break
to avoid inadvertently falling through
;
case "tablet"
:
break
;
case "laptop"
:
break
;
case "desktop"
:
break
;
default
:
• …
Determine if it is:
switch (theMonth)
Quarter 1 - Q1
case 1
Quarter 2 - Q2
:
case 2
:
case 3 Quarter 3 - Q3
:
System.out.println("Q1") Quarter 4 - Q4
break
;
case 4
:
case 5
:
case 6
:
System.out.println("Q2")
break
;
case 8
:
case 9
:
System.out.println("Q3")
;
break
;
case 10
:
case 11
:
case 12 Month:
:
System.out.println("Q4")
Q3
;
break
;
default
:
System.out.println("Invalid month")
;
© luv2code LLC
Regular switch Statement
• While regular switch statement meets basic requirement
case value1:
// execute this block of code
break;
case value2:
// execute this block of code
break;
default:
// default block of code
}
• Advantage s
switch (variable)
break
;
case "tablet"
:
break
Modern Switch
;
(condensed)
…
switch (computerType)
{
• primitive type: byte, short, char and int and associated Wrapper classe
switch (variable)
case "tablet" -> "Tablets are lightweight for browsing and light tasks."
case "laptop" -> "Laptops are portable for work on the go."
case "desktop" -> "Desktops excel in gaming and work related tasks."
}
;
System.out.println(description)
}
Tablets are lightweight for browsing and light tasks.
© luv2code LLC
Enums
• Enum is short for enumeration
case ComputerType.TABLET -> "Tablets are lightweight for browsing and light tasks."
case ComputerType.LAPTOP -> "Laptops are portable for work on the go."
case ComputerType.DESKTOP -> "Desktops excel in gaming and work related tasks."
}
;
System.out.println(description)
;