do while and While loop
Objective: Understand Do while loop and while loop.
do while
Do while loop is the type loop which run code when specified Boolean expression evaluate to true.
Syntax
It start with do open and closing curly bracket ,then while keyword followed by double parentheses end with semicolon.
Inside do while we increment to avoid program to freeze or to run to the infinity loop.
do
{
//increment or decrement
}while(Condition);
Do while run code at least one time even if condition is false. Because it run first then check condition.
While Loop.
While loop execute code when condition is true.
Syntax
It start with while keyword ,open and closing parentheses and open and closing curly bracket.
while(Condition)
{
}
How to avoid this loop from freezing, easy is to increment the value ,this means forgot to increment or decrement while checking the true condition the loop run infinity.
Infinity loop
int number = 0;
while(number <0)
{
Debug.Log("Hello");
}
Note: This logic is always be true, so this will print hello forever until the app freeze.
Solution
We need to increment number inside the while loop ,so it will be able to breath.
Other way is to use coroutine ,but we didn't cover this I assume you don't know what is coroutine.
int number = 0;
while(number <0)
{
Debug.Log("Hello");
number++;
}
See you in the next one.