Loops 1

Level 1 - Basics

Copy this fragment to the run() method of an exercise in IntelliJ.

SaxionApp.printLine("Before the while-loop starts");
int i = 0;
while (i < 10) {
    SaxionApp.printLine("In the loop.");
    SaxionApp.sleep(0.2);

    i++;
}

SaxionApp.printLine("The while-loop has finished");

Run the code. You should see appearing the texts. How many times does "In the loop" appear? Change the code so the message "In the loop" appears 20 times.

SaxionApp.printLine("Before the while-loop starts"); int i = 0; while (i < 20) { // 10 was changed to 20 here SaxionApp.printLine("In the loop."); SaxionApp.sleep(0.2); i++; } SaxionApp.printLine("The while-loop has finished");

Change the code so the value of the variable i appears instead of the text "In the loop".

SaxionApp.printLine("Before the while-loop starts"); int i = 0; while (i < 20) { SaxionApp.printLine(i); // the text was changed to i here. SaxionApp.sleep(0.2); i++; } SaxionApp.printLine("The while-loop has finished");

Change the code so i takes steps of two instead of only one at the time.

SaxionApp.printLine("Before the while-loop starts"); int i = 0; while (i < 20) { SaxionApp.printLine(i); SaxionApp.sleep(0.2); i = i + 2; // change this so i is incremented by 2 in each loop } SaxionApp.printLine("The while-loop has finished");

Change the code so i takes steps of five.

SaxionApp.printLine("Before the while-loop starts"); int i = 0; while (i < 20) { SaxionApp.printLine(i); SaxionApp.sleep(0.2); i = i + 5; // this shouldn't be hard now } SaxionApp.printLine("The while-loop has finished");

Change the code so the loop is counting backwards. Do this by subtracting 1 from i instead of adding 5. What happens when you start the program? Why? To stop your program, click the red stop button in IntelliJ.

SaxionApp.printLine("Before the while-loop starts"); int i = 0; while (i < 20) { SaxionApp.printLine(i); SaxionApp.sleep(0.2); i = i - 1; // this line was changed. Alternative: i-- or i -= 1. // the code is now in an endless loop, because i only // gets smaller. Look at the condition: i always will // be less than 20. } SaxionApp.printLine("The while-loop has finished");

To stop the loop from running, we need to change the condition. Change it so the loop runs as long as i is more than -10.

SaxionApp.printLine("Before the while-loop starts"); int i = 0; while (i > -10) { // condition was changed from <20 to >-10. SaxionApp.printLine(i); SaxionApp.sleep(0.2); i = i - 1; } SaxionApp.printLine("The while-loop has finished");

Level 2 - Counting

Now create a new loop that counts from 10 down to 1.

int i = 10; // loop variable starts at 10 while (i > 0) { // run as long as it is more than 0 SaxionApp.printLine(i); i--; // decrement loop variable }

Create a new loop that counts from 0 to 9

int i = 0; // loop variable starts at 0 while (i < 10) { // run as long as it is less than 10 SaxionApp.printLine(i); i++; // increment loop variable }

Change the previous loop so it counts from 1 to 10

int i = 1; // loop variable starts at 1 this time while (i <= 10) { // run as long as it is less than or equal to 10 SaxionApp.printLine(i); i++; } // Alternative solution: int i = 0; while (i < 10) { SaxionApp.printLine(i + 1); // just changing what is printed is also a solution. i++; }

Instead of counting to 10, ask a number from the user, and count from 1 to that number.

int number = SaxionApp.readInt(); int i = 1; while (i <= number) { // use number here instead of 10 SaxionApp.printLine(i); i++; }

Change the program, so it not only counts to that number, but at the same time also shows a countdown to 1. For example, if the user enters "5", it should show:

up: 1 down: 5
up: 2 down: 4
up: 3 down: 3
up: 4 down: 2
up: 5 down: 1
// there are several ways of doing this. One of the ways is // to use i for both the up and down counting. int number = SaxionApp.readInt(); int i = 1; while (i <= number) { SaxionApp.print("up: " + i); // changed to "print" SaxionApp.printLine(" down: " + (number - i + 1)); i++; } // Alternative solution: // another way would be to introduce a second loop variable // that counts down int number = SaxionApp.readInt(); int countUp = 1; // changed i to countUp for clarity int countDown = number; // new countDown variable while (countUp <= number) { SaxionApp.print("up: " + countUp); SaxionApp.printLine(" down: " + countDown); countUp++; countDown--; }

Level 3 - Endless loops

Copy this fragment to the run() method of an exercise in IntelliJ. Run it. What happens? To stop your program, click the red stop button in IntelliJ.

SaxionApp.printLine("Before the while-loop starts");
while (true) {
    SaxionApp.printLine("In the loop.");
    SaxionApp.sleep(0.2);
}

Instead of printing "in the loop" each time, ask a number from the user each time, and print that number on the screen. (You can remove the sleep line)

SaxionApp.printLine("Before the while-loop starts"); while (true) { int number = SaxionApp.readInt(); SaxionApp.printLine("The number was: " + number); }

If we want to make a small calculator out of this, that adds all numbers together, we have to introduce a "sum" variable that keeps track of the sum during the loop. Use the following snippet to start with. The code does not compile, you have to change it yourself, see below.

SaxionApp.printLine("Before the while-loop starts");

int sum = 0; 

while (true) {
    int number = SaxionApp.readInt();

    sum =    // change this line

    SaxionApp.printLine("The total of all numbers is now: " + sum);
}

Change the code, so the sum variable will increase with the number that the user entered. The output should be something like this:

10
The total of all numbers is now: 10
20
The total of all numbers is now: 30
5
The total of all numbers is now: 35
SaxionApp.printLine("Before the while-loop starts"); int sum = 0; while (true) { int number = SaxionApp.readInt(); sum = sum + number; // each time the new number is added to the // previous value of sum. Alternative: sum += number SaxionApp.printLine("The total of all numbers is now: " + sum); }

If the user enters the number 0, we want to stop the loop. Add an if-statement to check if the number is 0. If that is the case, use the "break" statement to stop the loop, like this:

break;
SaxionApp.printLine("Before the while-loop starts"); int sum = 0; while (true) { int number = SaxionApp.readInt(); if (number == 0) { // check for 0 break; // jump to the end of the loop } sum = sum + number; SaxionApp.printLine("The total of all numbers is now: " + sum); } SaxionApp.printLine("The while-loop has finished");

We also want to calculate the average of the total of numbers. The average is calculated by dividing the sum by the count of the numbers entered. So we should keep track of how many numbers the user enters. Add a new variable to the program called "count". Add 1 to this variable each time a new number is entered. Show the average to the user.

SaxionApp.printLine("Before the while-loop starts"); int sum = 0; int count = 0; while (true) { int number = SaxionApp.readInt(); if (number == 0) { // check for 0 break; // jump to the end of the loop } sum = sum + number; count++; SaxionApp.printLine("The total of all numbers is now: " + sum); SaxionApp.printLine("The average of all numbers is now: " + ((double)sum / count)); } SaxionApp.printLine("The while-loop has finished");

Level 4 - Loops within loops

Ask the user for a number once. Then show the character "#" that many times. For example:

How many? 10
#########
SaxionApp.print("How many? "); int number = SaxionApp.readInt(); int i = 0; while (i < number) { SaxionApp.print("#"); i++; }

Now change the program. The program should still only once ask for user input, but after that show the row of #'s again and again endlessly with a sleep of 0.2 inbetween. Example

How many? 10
#########
#########
#########
#########
#########
...
SaxionApp.print("How many? "); int number = SaxionApp.readInt(); while (true) { // we enclose the other loop in an endless loop int i = 0; while (i < number) { SaxionApp.print("#"); i++; } SaxionApp.printLine(); // add this, otherwise all #'s will be on one line SaxionApp.sleep(0.2); }

Also ask the user how many rows he/she wants. Change the endless loop to be a loop that will make that amount of rows.

SaxionApp.print("How many #? "); int number = SaxionApp.readInt(); SaxionApp.print("How many rows? "); int rows = SaxionApp.readInt(); int rowIndex = 0; // loop variable for the row loop while (rowIndex < rows) { // changed the condition to stop after the number of rows int columnIndex = 0; // changed i to columnIndex for clarity while (columnIndex < number) { SaxionApp.print("#"); columnIndex++; } SaxionApp.printLine(); rowIndex++; // don't forget this }