- Home /
Difference between if statement, while and for loop?
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:
if(condition is true)
{
//action
}
But I recently searched some information and I figured out that the while loop does the same:
while(condition is true)
{
//action
}
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?
Answer by WillTAtl · Dec 06, 2011 at 08:46 PM
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:
function Update()
{
if(true)
print("hello!");
}
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!
function Update()
{
while(true)
print("hello!");
}
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...
function Update()
{
//condition initially true every update...
var simulatedIf=true;
while(simulatedIf)
{
print("hello!");
//make it false now, so it won't repeat
simulatedIf=false;
}
}
Hope this clears it up!
Answer by jahroy · Dec 06, 2011 at 08:57 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:
for ( var i = 0; i < 10; i ++ ) { print(i); }
/ ------ /
var i = 0;
while ( i < 10 ) { print(i); i ++; }
/ ------ /
var i = 0;
for ( ; i < 10; ) { print(i); i ++; }
Answer by TheCodeMonkey · Dec 06, 2011 at 08:59 PM
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:
While(true)
{
//some code for printing the number 1
}
Will infinitely print the number 1 because the while loop has no way of exiting due to the "true" boolean, whereas the if statement:
If(true)
{
//some code for printing the number 1
}
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:
Answer by J3-Gaming · Dec 06, 2011 at 08:58 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.
for (int i = 0; i < 10; i++)
{
Debug.Log("The current number is: " + i);
}
The loop will run a total of 10 times (0 - 9) and increment the variable every round.
Incorrect. A for loop IS just another way to write a while loop.
A for loop is just a while loop that has a convenient syntax as a shortcut.
Indeed, there's nothing stopping you having a counter in a while loop.
Or using the for loop with a linked list ins$$anonymous$$d of a counter:
// C#
for (Node current = first; current != null;current = current.next)
{
//
}
;)
Your answer
Follow this Question
Related Questions
Cycle Through All GameObjects in the Scene 2 Answers
Assign Default For Large Array 1 Answer
Beginner: Box moving up and down. Loop? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
JS Loop question 2 Answers