IB Computer Science Unit 1
IB Computer Science Unit 2
IB Computer Science Unit 3
IB Computer Science Unit 4
IB Computer Science Unit 5
IB Computer Science Unit 6

IB Computer Science Repetition Structures

IB Computer Science Learning Goals

In this IB Computer Science lesson you will be learning about:

  • Understanding how computer programs can repeat tasks
  • Writing programs that use while loops and for loops

What is a Loop?

So far you have written programs that have executed in sequential order and have used some simple selection structures to execute certain blocks of code but not others. Sometimes it is also necessary that certain blocks of statements be repeated over and over again inside a program a specific # of times.

  • This is accomplished using a repetition structure, commonly known as a loop
  • Loops, like if statements, execute blocks of statements when a comparison evaluates to true
  • However, unlike if statements, a block of code will repeatedly be executed until the comparison becomes false
IB Computer Science Loop Flow Chart Java

Examples

In the following programs lets figure out how many times the word “Hello” is printed to the screen

Counter
Comparison (counter < 10)
Result


0
TRUE
HELLO


1
TRUE
HELLO


2
TRUE
HELLO


3
TRUE
HELLO


4
TRUE
HELLO


5
TRUE
HELLO


6
TRUE
HELLO


7
TRUE
HELLO


8
TRUE
HELLO

9
TRUE
HELLO


10
FALSE

 

HELLO IS PRINTED 10 TIMES ONTO THE SCREEN

Examples

In the following program lets figure out how many times the word “Hello” is printed to the screen

Counter
Comparison (counter < 7)
Result

0
TRUE
HELLO

2
TRUE
HELLO

4
TRUE
HELLO

6
TRUE
HELLO

8
FALSE

HELLO IS PRINTED 4 TIMES ONTO THE SCREEN

Examples

In the following program lets figure out how many times the word “Hello” is printed to the screen

IB Computer Science Loop Flow chart Example

Counter
Comparison (counter >= 0)
Result

15
TRUE
HELLO

10
TRUE
HELLO

5
TRUE
HELLO

0
TRUE
HELL0

-5
FALSE

HELLO IS PRINTED 4 TIMES ONTO THE SCREEN

Examples

IB Computer Science Flow Chart Example Counted

Counter
Comparison (counter < 23)
Result

2
TRUE
HELLO

4
TRUE
HELLO

8
TRUE
HELLO


16
TRUE
HELLO

32
FALSE

HELLO IS PRINTED 4 TIMES ONTO THE SCREEN

Examples

What is the value of the variable Sum after the program has executed?

IB Computer Science Flow chart sum of 1 to 10

Counter
Comparison (counter < 10)
sum = sum + counter

0
TRUE
0 + 0 = 0

1
TRUE
0 + 1 = 1

2
TRUE
1 + 2 = 3

3
TRUE
3 + 3 = 6

4
TRUE
6 + 4 = 10

5
TRUE
10 + 5 = 15

6
TRUE
15 + 6 = 21

7
TRUE
21 + 7 = 28

8
TRUE
28 + 8 = 36

9
TRUE
36 + 9 = 45

10
FALSE

 

THE VALUE OF THE VARIABLE SUM WILL BE 45 AFTER THE LOOP FINISHES (THIS LOOP ADDED UP THE NUMBERS FROM 1 TO 10)

Examples

What is the value of the variable SumO and SumE after the program has executed?

IB Computer Science Flow chart Java Sums

Counter
Loop Comparison (counter < 10)
If Statement Comparison (Counter divisible by 2)
SumO = SumO + counter
SumE = SumE + counter

0
TRUE
TRUE
0
0 + 0 = 0

1
TRUE
FALSE
0 + 1 = 1
0

2
TRUE
TRUE
1
0 + 2 = 2

3
TRUE
FALSE
1 + 3 = 4
2

4
TRUE
TRUE
4
2 + 4 = 6

5
TRUE
FALSE
4 + 5 = 9
6

6
TRUE
TRUE
9
6 + 6 = 12

7
TRUE
FALSE
9 + 7 = 16
12

8
TRUE
TRUE
16
12 + 8 = 20

9
TRUE
FALSE
16 + 9 = 25
20

10
FALSE

 

AFTER THE LOOP IS FINISHED SUMO = 25 AND SUME = 20 (THE LOOP BASICALLY ADDED UP ALL THE ODD NUMBER LESS THAN 10 AND ALL THE EVEN NUMBERS LESS THAN 10)

Java - While Loop

As long as the comparison inside the brackets is true, the statements inside the while loop will repeatedly execute

The following example uses a while loop to print “Java is Fun” to the screen 10 times. I used the variable ctr to represent the loop counter

IB Computer Science Java While Loop

Java - For Loop

IB Computer Science Java For Loop Syntax

The loop works as follows:

  • First statement1 is executed.
  • Then the comparison is evaluated to true or false.
  • If the comparison is true, the statements inside the loop are executed.
  • Once all of the statements inside the loop have been executed, statement2 is executed and the comparison is evaluated again to true or false.
  • This process continues until the comparison evaluates to false

This sounds complicated, but interpret it as follows:

  • statement1 initializes the loop.
  • The comparison tests if the loop should be ended.
  • statement3 is the step statement that goes to the next loop evaluation

The following example uses a for loop to print “Java is Fun” to the screen 10 times. Again the variable ctr was used as a loop counter

IB Computer Science Java for Loop Example

There is always more than 1 way to set up a loop statement. Sometimes I will use the variable ctr as a “counter” variable, but its probably easier just to use “i” or “j” as your counter variables

More Counted Loop Examples

A program that asks the user to enter 5 numbers and then prints out the sum and average of those numbers using a for loop


Problem Solving:

Define 1 variable for the user input, 1 for the sum, 1 for the average

Notice that the same group of statements in the previous solution repeats itself

Loop:

  • Write a print statement asking for a number
  • Read in the value into a variable
  • Add that value to the previous value of the sum
  • repeat 5 times

Loop finished:
Print out the sum
Divide the sum by 5 to get the average
Print out the average

IB Computer Science Java Sum of Numbers Entered

More Counted Loop Examples

A program that asks the user for a number and prints the sum from 1 to that number using a while loop

Problem Solving:
Define a variable for the sum, and the number entered by the user
Ask the user for a number
Read in the number from the keyboard
Loop: from 1 to the number entered by the user
• Add the loop counter to the previous value of the sum
Loop Finished
Print out the sum

IB Computer Science Java Sum 1 to X

Conditional Loop Examples

So far you have been using loops to execute certain statements a predetermined amount of times. In other words, you have been using a counted loop. A conditional loop can be used in a program where it is not known how many times the loop needs to execute. There are many different ways to accomplish these tasks, so your problem solving ability will be needed to think of the best solutions. Some of the common ways are shown below.
Example Problem:
Write a program to calculate the area of a circle by inputting the radius from the user. Keep going until the user says he wants to stop.


Solution 1:

In this solution, you can have the program ask the user to enter “yes” if he wants to continue.

IB Computer Science Conditional Loop 1

Solution 2:

  • In this solution, you can have the program continue to loop until the user enters an invalid value (called a sentinel).
  • In this case since the radius of a circle can only be positive, the user can indicate he wishes to stop by entering a negative value for the radius
  • This solution uses an infinite loop and makes use of the “break” statement
IB Computer Science Conditional Java Loop

Solution 3:

In this solution, we can again use a sentinel value to indicate when we want the program to stop, but instead of using a “break” statement, we can make use of a Boolean variable

Nested Loop Examples

Nested loops are loops inside of loops and are a very powerful programming tool. Here are some examples in Java

Example:
The following program will print a table 5 x 3 table full of random numbers


Problem Solving:
• The inside loop executes its two statements 5 times creating a line of 5 random numbers on the screen
• When that loop is finished the println(“”) is executed moving the cursor to the next line
• Steps 1 and two are repeated 3 times because of the outside loop creating 3 rows of numbers on the screen

IB Computer Science Nested Loop Example 1

Example:
The following program asks the user to enter a number. The program then displays a wedge of stars as follows:

IB Computer Science Java Wedge Output

Look at the code carefully and you will see the outside loop counter used in the inside loop counter to decrease how many times the inner loop executes

IB Computer Science Java Wedge of Stars

IB Computer Science Practice Questions

1. Write a program that prints the numbers 1 to 15 on the screen
• Write one program that prints the numbers on separate lines
• Write another program that prints them with spaces in between.

2. Write a program that asks the user to enter two numbers and prints all the numbers in between those numbers on a separate line
• Write one program that prints them including the end points
• Write another program that prints them without including the end points

3. Write a program that calculates the hypotenuse of a right angle triangle.
• Write one program that repeats itself 5 times
• Write another program that only stops when the user enters a negative input.

4. A variation of the game of 21 is played as follows
• The computer generates two random numbers between 6 and 24
• If the sum is between 21 and 30 then you win
• If the sum is less than 21 you lose
• If the sum is anything else the game is a draw

5. Asks the user to enter 5 numbers and displays the biggest number and the smallest number

6. Write a program that asks the user for a positive number and then prints the nth triangle number.

IB Computer Science Triangle Number

7. Generate a multiplication table using nested loops. The output should look like

IB Computer Science Java Loop 2D Table

8. Write a program that lets you enter a number between 1 and 20 and that prints a diagonal line made up of those n stars

IB Computer Science Java Diagonal Line of Stars

Looking to Learn More about Computer Science and Coding?

Check out our programing in python courses that focus on high school level coding.  

  • Grade 11 Computer Science ICS3U
  • Grade 12 Computer Science ICS4U

These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions

Return To International Baccalaureate Computer Science Main Page