Execution Flow Statements define which part of the code should be executed next
● Branch Statements execute its Body single time if Condition is TRUE.
● Loop Statements execute its Body multiple times while Condition is TRUE (or while iterating through Elements)
● Jump Statements unconditionally skip execution of subsequent code
Branch Statements execute its Body single time if Condition is TRUE
● if ... else Statement executes single section for which condition is TRUE
● switch...case Statement is used instead if ... else Statement when all conditions evaluate the same variable
● ? TRUE : FALSE Statement executes first section if Condition is TRUE. Otherwise it executes second section.
if ... else
int value = 10;
String text = "bird";
if (value == 10 ) { System.out.println("Value = 10" ); }
else if (text.equals("bird")) { System.out.println("text equals bird"); }
else if (text.equals("dog" )) { System.out.println("text equals dog" ); }
else { System.out.println("No match found" ); }
switch...case
//TEST VARIABLES.
int a = 5;
//SWITCH STATEMENT.
switch (a) {
case 1:
System.out.println("Value is 1. Exit from switch.");
break;
case 5:
System.out.println("Value is 5. Continue to next case or default.");
default:
System.out.println("Default value is selected if no case was true.");
break;
}
VARIABLE = CONDITION ? TRUE : FALSE
//TEST VARIABLES.
int a = 10;
int b = 5;
//VARIABLE = CONDITION ? TRUE : FALSE
int max = (a > b) ? a : b;