Hello, I know I'm asking a stupid question but I'm kinda confused. I know that the if statement is called every frame when the condition is true. Like this:
But I recently searched some information and I figured out that the while loop does the same:
And the for loop is just another way to write the while loop.
I know I'm wrong but could someone please clear this up for me?
The only reason if is running every frame is because you have it in a function that is called every frame, like Update() or FixedUpdate().
Simple example to prove they're different. Throw this in a script, and it will print "hello" once per frame:
But if you put this in a script, you'll cause unity to go into an infinite loop, freezing up (you'll have to force-quit, so if you actually try this, save first!), and your game will never get to the 2nd frame!
If you've got a while loop that seems to be acting like an if, then something must be happening inside the body that is causing the condition to stop being true immediately. A rough example...
Hope this clears it up!
answered Dec 06, 2011 at 08:46 PM
An if statement causes the code inside the if block to execute one time IF the condition is true.
A while loop causes the code inside the while block to execute over and over and over UNTIL the condition is NOT true.
A for loop is actually a while loop with a special (shortcut) syntax.
A for loop has three espressions separated by semi-colons. It executes the code in the for block over and over and over UNTIL the middle expression is NOT true.
The first expression is executed before the body of the loop. The last expression is executed at the end of each iteration of the loop.
Therefore, the following three pieces of code do the exact same thing:
If in the the Update() function an if or a while are called every frame, however that is not the case in a normal setting. The major difference being what OrangeLightning said, a while loop will constantly loop through itself checking the condition after each loop through the code within, whereas an if will check the condition once, react accordingly, and then move on, meanwhile a for loop, and a foreach loop function slightly differently than a while loop. Although it is true that you can use a for loop almost anywhere you can use a while loop they are still fundementally different
To illustrate the difference between a while loop and an if statement:
Will infinitely print the number 1 because the while loop has no way of exiting due to the "true" boolean, whereas the if statement:
Will simply print the number 1 once and leave the statement continuing on with its work for the function. Here is an okay explanation of the differences between all the loops, as well as a pretty good explanation of if statements, it is java specific but still not bad:
answered Dec 06, 2011 at 08:59 PM
"And the for loop is just another way to write the while loop."
Not true, you will use a for loop when you want a counter.
The loop will run a total of 10 times (0 - 9) and increment the variable every round.
answered Dec 06, 2011 at 08:58 PM