Journey to Java: Episode 5 “Control Flow”


Using the if then statement is fine with a simple example but if we need to have many else ifs inside our if else statement the code can get repetitive and hard to read. This is where the switch statement comes in.

int value = 1;switch(value) {
case 1:
System,out.println("value was 1");


For statement or for loop is used to loop a certain amount of times. This executes a block a certain amount of times until a condition is met.

for(init; termination; increment) {}
for(int i = 0; i < 5; i++) {
System.out.println("i = " + i);


A for loop executes a set amount of times. A while loop executes until a condition evaluates to false. This is good when you don’t know how many times you will have to loop.

int count = 0;
while(count != 5) {
System.out.println("count is " + count);

Do While

The do while loop is useful because it will always execute the loop at least once or more times depending on how the condition is set. This can cause an infinite loop if not using break keyword.

int count = 0;
do {
} while (count != 5);



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store