CS-I Journal J1153: Repeat-While Loop

Name: Mukesh Kalikaya

Date: 27-January-2023

I received assistance from: No one

I assisted: No one

Navigation

BACK TO JOURNALS

CSS File

Previous Journal

Section 1

Compare this behavior to that of the while loop. What are the similarities? What are the differences?

The repeat-while loop executes the statements within the body of the loop which is something a while doesn't do. Instead, the condition, the Boolean test, is evaluated in a while loop. Both of them do test two conditionals and create a loop according to those conditionals.

What are the minimum number of times that this loop will execute?

The minimum number that this loop will be executed is 1.

What is the purpose of the Jump instruction after the consequent?

The purpose of the Jump instruction is so that the loop can go back to the beginning of the loop.

Section 2

Are you able to implement the same functionality found in a while loop using a repeat-while loop, without changing the condition or adding a conditional? Explain and provide an example.

Both while loop and repeat-while loop will execute the same number of times. In the below example, if the initial value of variable x=3 (meaning the condition is false), then while loop will give different result. But if the first value of x is 6, both executions will give same result.

Example:

Repeat-While Loop:

var x = 3
repeat {
print("something")
x -= 1
print(x)
} while (x > 4)
print("random")

The output would be:

something
2
random

While Loop:

var x = 3
while (x > 4) {
print("something")
x -= 1
print (x)
}
print ("random")

The output would be:

random

Based on the results, you can see that the repeat-while loop doesn't function the same way that a while loop does because it prints a statement first instead of running the condition first.

Are you able to implement the same functionality found in a repeat-while loop using a while loop, without changing the condition or adding a conditional? Explain and provide an example.

In the above example for question 1 in section 2, the while loop will not print a statement before checking the condition. Only if the initial value of a variable used in the condition is true. In this case both repeat-while loop and while loop execute the block of code the same number of times.

What did I learn? What was the "big idea"?

I learned about the functionality of repeat-while loops and the statements/format to run them.

What challenges did I encounter?

I didn't encounter any challenges because repeat-while loops are pretty similar to while loops which I was confidently able to do in the previous unit.

How could this experience be improved?

This experience is at it's best.

Free Reflection: How has what I've learned affected my thinking?

Now that I know what repeat-while loops are used for and how to use them, I can use this type of loop if I want to repeatedly execute a program and if that is interrupted, I can go to the next condition that I can mention to do something else.