Skip to main content

Looping and Branching

The Aims​

This lab will introduce you to using the various flow control constructs of the Java language.

Part 1 Using if / else​

The main method from your previous lab exercise should look something like this.

public static void main(String[] args) {
String make = "BMW";
String model = "530D";
double engineSize = 3.0;
byte gear = 2;
short speed = (short)gear * 20;

System.out.println("The make is " + make);
System.out.println("The model is " + model);
System.out.println("The engine size is " + engineSize);
System.out.println("The speed is " + speed);
}
}
  1. Firstly, put in some logic to print out either that the car is a powerful car or a weak car based on the engine size, for example, if the size is less than or equal to 1.3.

  2. Now, using an if / else if construct, display to the user a suitable speed range for each gear, so for example, if the gear is 5, then display the speed should be over 45mph or something. If the gear is 1, then the speed should be less than 10mph etc.

Click here to see the sample solution

public class Main
{
public static void main(String[] args)
{
String make = "BMW";
String model = "530D";
double engineSize = 3.0;
byte gear = 2;
short speed = (short)gear * 20;

System.out.println("The make is " + make);
System.out.println("The model is " + model);
System.out.println("The engine size is " + engineSize);
System.out.println("The speed is " + speed);

if (engineSize <= 1.3) {
System.out.println("You have a weak car");
}
else {
System.out.println("You have a powerful car");
}

if (gear == 1) {
System.out.println("Suitable speed for first gear: up to 10mph");
} else if (gear == 2) {
System.out.println("Suitable speed for 2nd gear: 11 to 20mph");
} else if (gear == 3) {
System.out.println("Suitable speed for 3rd gear: 21 to 30mph");
} else if (gear == 4) {
System.out.println("Suitable speed for 4th gear: 31 to 45mph");
} else {
System.out.println("Suitable speed for 5th gear: over 45mph");
}
}
}

Part 2 Looping​

  1. We will now need to generate a loop which loops around all the years between 1900 and the year 2000 and print out all the leap years to the command console. You can use either a for or while loop to do this. Put the code at the end of the main method.

  2. Once you have done this, set it so that after 5 leap years have been displayed, it breaks out of the loop and prints 'finished'.

Click here to see the sample solution

public class Main
{
public static void main(String[] args)
{
...

int year = 1900;
int found = 0;
while (year <= 2000) {
if (year % 4 == 0) {
System.out.println(year + " is a leap year");
found++;
if (found == 5)
break;
}
year++;
}
}
}

Part 3 Using switch / case​

  1. Now re-write part 1 to use a switch / case construct. It should do exactly the same thing as the if / else if construct. Don't forget to use break!

Click here to see the sample solution

public class Main
{
public static void main(String[] args)
{
...

switch (gear) {
case 1:
System.out.println("Suitable speed for first gear: up to 10mph");
break;
case 2:
System.out.println("Suitable speed for 2nd gear: 11 to 20mph");
break;
case 3:
System.out.println("Suitable speed for 3rd gear: 21 to 30mph");
break;
case 4:
System.out.println("Suitable speed for 4th gear: 31 to 45mph");
break;
default:
System.out.println("Suitable speed for 5th gear: over 45mph");
break;
}

...
}
}

Part 4 (Optional)​

  1. This will possibly require some research by you to find out how to do this. Create an array capable of containing 10 ints.

  2. Now modify your loop above, so that it no longer displays the first five years, but stores the first 10 in your array instead.

  3. Now provide another loop to print out the values in the array. Use the length property of the array object to specify how many times to loop.

Click here to see the sample solution

public class Main
{
public static void main(String[] args)
{
...

int[] years = new int[10];

int year = 1900;
int found = 0;
while (year <= 2000) {
if (year % 4 == 0) {
years[found] = year;
found++;
if (found == 10)
break;
}
year++;
}

for (int i = 0; i < years.length; i++) {
System.out.println(years[i]);
}

}
}