In this IB Computer Science lesson you will be learning about:
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.
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
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
In the following program lets figure out how many times the word “Hello” is printed to the screen
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
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
What is the value of the variable Sum after the program has executed?
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)
What is the value of the variable SumO and SumE after the program has executed?
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)
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
The loop works as follows:
This sounds complicated, but interpret it as follows:
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
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
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:
Loop finished:
Print out the sum
Divide the sum by 5 to get the average
Print out the average
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
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.
Solution 2:
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 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
Example:
The following program asks the user to enter a number. The program then displays a wedge of stars as follows:
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
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.
7. Generate a multiplication table using nested loops. The output should look like
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
Check out our programing in python courses that focus on high school level coding.
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