Would the output of the first code batch beIn post 62, Cheetory6 wrote:Spoiler: While Loops
For the other two control structures, we will be using a similar concept of using boolean expressions to guide the movement of our program, but we’ll adding the concept of Loops/Looping on top. A loop is essentially a section of code that gets repeated in the flow of program’s operation. We will first consider the While loop:
Code: Select all
int i = 0; while(i < 4){ Debug.Log(i); i++; } Debug.Log(“Escaped!!”);
The basic idea of the while loop, is that while the condition within the brackets is true the code within the curly brackets will repeat.
In this case, the while loop will repeat the code within its curly brackets until the variable i is no longer less than 4.
OUTPUT:
1 - "1"
2 - "2"
3 - "3"
4 - "4"
Code: Select all
int i = 0; while(i < 4) { Debug.Log(i); i++;} Debug.Log(“Escaped!!”);
You have to be really careful when using while loops! There is the potential to create a set of conditions where a while loop will never have its condition become true and then the code within will repeat indefinitely and lock up your program. If you remove the contents of the code within the while loop above as follows, you will never get the “Escaped!!” message.
"0"
"1"
"2"
"3"
“Escaped!!”
Since when
i
hits 4 it pops it from the loop?