x


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?

more ▼

asked Dec 06, 2011 at 08:32 PM

BarkShark gravatar image

BarkShark
311 32 34 45

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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!

more ▼

answered Dec 06, 2011 at 08:46 PM

WillTAtl gravatar image

WillTAtl
822 7 9 18

Thank you, you cleared it up!

Dec 07, 2011 at 11:49 AM BarkShark
(comments are locked)
10|3000 characters needed characters left

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 ++; }

more ▼

answered Dec 06, 2011 at 08:57 PM

jahroy gravatar image

jahroy
3.8k 37 46 84

Thank you for the information!

Dec 07, 2011 at 11:51 AM BarkShark
(comments are locked)
10|3000 characters needed characters left

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:

For and While

Ifs

more ▼

answered Dec 06, 2011 at 08:59 PM

TheCodeMonkey gravatar image

TheCodeMonkey
236 5 6 8

Thanks for the help

Dec 07, 2011 at 11:51 AM BarkShark
(comments are locked)
10|3000 characters needed characters left

"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.

more ▼

answered Dec 06, 2011 at 08:58 PM

J3 Gaming gravatar image

J3 Gaming
269 55 45 53

Indeed, there's nothing stopping you having a counter in a while loop.

Dec 07, 2011 at 12:27 AM Eric5h5

Or using the for loop with a linked list instead of a counter:

 // C#
 for (Node current = first; current != null;current = current.next)
 {
     //
 }

;)

Dec 07, 2011 at 02:39 AM Bunny83

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.

Dec 06, 2011 at 09:02 PM jahroy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9408
x1057
x545
x161

asked: Dec 06, 2011 at 08:32 PM

Seen: 13192 times

Last Updated: Dec 07, 2011 at 11:51 AM