Control Flow
What we’ll cover
- Block Scope
- Conditional Statements
- Loops
- Switch Statements
Block Scope
- A block or compound statement consists of a number of statements, surrounded by a pair of braces.
- Blocks define the scope of your variables.
- Blocks can be nested
public static void main(String[] args)
{
{ // this is the scope within which `someUnreachableValue` is defined
String someUnreachableValue = "This value cannot be reached outside the scope";
} // `someUnreachableValue` is not accessible beyond this line
System.out.println(someUnreachableValue); // compile-time exception
}
Naming Conflicts
- You cannot declare identically named variables in two nested blocks
public static void main(String[] args)
{ // this is the scope within which `uniqueVariableName` is defined
String uniqueVariableName = "We cannot re-declare this variable name";
{ // cannot re-declare variable `uniqueVariableName` in inner scope
String uniqueVariableName; // compile-time exception
}
}
Conditional Statements
If Statements; General Syntax
- The argument of the
ifclause must be abooleanvalue.- the value of
conditionin the example below must be of typeboolean.
- the value of
- Execution of an
ifcondition is called a check or evaluation. - The body of an
ifstatement only executes if the value of theconditionhas a value oftrue. - Control structure code creates branches, or different paths that the code can take.
public static void main(String[] args)
{
Scanner userInputGrabber = new Scanner(System.in);
String userInputPrompt = "Enter your age";
String userInput = userInputGrabber.nextLine();
Integer userAge = Integer.parseInt(userInput);
Integer requiredAge = 18;
if(userAge >= requiredAge) {
System.out.println("You are old enough to use the application");
}
}
If Statements; Example
public static void main(String[] args) {
Scanner userInputGrabber = new Scanner(System.in);
String userInputPrompt = "Enter your age";
String userInput = userInputGrabber.nextLine();
Integer userAge = Integer.parseInt(userInput);
Integer requiredAge = 18;
if(userAge >= requiredAge) {
System.out.println("You are old enough to use the application");
}
}
if/else Statements; General Syntax
If/Elsestatements are used when you would like to execute one block or the other, but never both blocks.Ifstatements do not have to be followed with anelsestatementElsestatements have to be preceded with anifstatement.
public static void main(String[] args) {
Integer randomValue = java.util.concurrent.ThreadLocalRandom.current().nextInt(0, 2);
Boolean condition = randomValue == 1;
if(condition) {
System.out.println("The value was 1");
} else {
System.out.println("The value was 0");
}
}
if/else if Statements; Example
if/else ifstatements are used when you would like to execute one of several blocks, given that at least one condition prior to theelse ifcheck isfalse.ifstatements do not have to be paired with anelse ifstatemenstelse ifstatements have to be preceded by anifstatements orelse ifstatements.else ifblocks can only execute if the value of allconditions have a value offalse.
public static void main(String[] args) {
Scanner userInputGrabber = new Scanner(System.in);
String userInputPrompt = "Enter your age";
String userInput = userInputGrabber.nextLine();
Integer userAge = Integer.parseInt(userInput);
Integer requiredAge = 18;
if(userAge > requiredAge) {
System.out.println("Your age is greater than the required age");
} else if (userAge == requiredAge) {
System.out.println("Your age is equal than the required age");
}
}
if/else if/else Statements; Example
public static void main(String[] args) {
Scanner userInputGrabber = new Scanner(System.in);
String userInputPrompt = "Enter your age";
String userInput = userInputGrabber.nextLine();
Integer userAge = Integer.parseInt(userInput);
Integer requiredAge = 18;
if(userAge > requiredAge) {
System.out.println("Your age is greater than the required age");
} else if (userAge == requiredAge) {
System.out.println("Your age is equal than the required age");
} else {
System.out.println("Your age is less than the required age");
}
}
Multiple Selections - The switch Statement
public static void main(String[] args) {
Scanner userInputGrabber = new Scanner(System.in);
String userInputPrompt = "Enter your age";
String userInput = userInputGrabber.nextLine();
Integer userAge = Integer.parseInt(userInput);
switch(userAge) {
case 15:
System.out.println("Teenagers cannot use this application!");
break;
case 12:
System.out.println("Preteens cannot use this application!");
break;
case 4:
System.out.println("Children cannot use this application!");
break;
case 0:
System.out.println("Babies cannot use this application!");
break;
default:
System.out.println("Welcome to my application!");
}
}
Cascading switch conditions
casestatements without breaks can cascade functions to the nextcase.
public static void main(String[] args) {
Scanner userInputGrabber = new Scanner(System.in);
String userInputPrompt = "Enter your age";
String userInput = userInputGrabber.nextLine();
Integer userAge = Integer.parseInt(userInput);
switch(userAge) {
case 15:
System.out.println("Teenagers cannot use this application!");
case 12:
System.out.println("Preteens cannot use this application!");
case 4:
System.out.println("Children cannot use this application!");
case 0:
System.out.println("Babies cannot use this application!");
break;
default:
System.out.println("Welcome to my application!");
}
}
Case labels
- A case label can be used to gain better control over a
switch/caseorloopcontrol-structure.