Confused about while loops in C#

so I'm rather new to while loops with C#...I have the following code:

if (boostCharge > 0 && !bikeEngine.boostIsOn) {
   boostCharge--;
}
/*      
while (boostCharge > 0 && !bikeEngine.boostIsOn) 
{
   boostCharge--;
}
*/

using "if", the boostCharge variable slowly goes down on every frame. But using "while", boostCharge goes straight to 0, why is that? Do while loop have to be in a Coroutine in C#?

Thanks!

You are seeing the if method in the wrong angle.

For a moment, forget about your Update funcion, lets assume a generic function.

If is only a comparison function, in other words, it will just evaluate if the condition between parenthesis is true, and if so, will execute the code between brackets. If is not a loop function.

While, in the other hand, is a loop function. It will execute while the condition between the parenthesis is true, and will only exit its loop when the condition becomes false.

The impression you have that IF is actually doing a per-frame loop is solely because Update is being called every frame, and thus, it will evaluate your condition in the IF once per frame. If is not being responsible for the loop, but your update function is.

With the while, every frame Update is called, and within Update, it'll loop internally until your condition in the WHILE is false, and in your example, this happens when boostcharge <= 0. So, every frame, you start boostcharge with, lets say, 10, and it'll exit with 0. In other words, you have a loop (the WHILE command) within another "loop" (the Update function).

The if method works because your boostcharge variable probably is class-scoped, so it'll persist the value of the variable between Update calls.

The FOR command will work exactly as WHILE, giving that your condition is met in the for loop as well.

Hope you now understand the difference between your IF and your WHILE inside the Update method.

Like all code within a function even loops are executed immediately. Your Update() function is called every frame by Unity, but the code inside is executed "as fast as possible". A while loop is a loop inside your function it will run until the condition is false. So in your example if boostCharge is 10 and your code is executed, right after the while block boostCharge will be 0, because the while won't exit until boostChange is <= 0.

That's why you have to be carefully with loops, you can make a loop that runs forever and will block everything (in layman's terms it's called: crash)

You can try this (but be careful if executed within Unity the whole editor will crash):

// !!!!
// USE AT YOUR OWN RISK !
// !!!!

void Update()
{
   while(true)
   {
   }
}

As I said: be careful and save everything before you test something like that.


Some further information:

Unity is a program. It offers you the possibillity to extend it's function. In Unity you can create a GameObject and add a components to it. A special component is the MonoBehaviour component. MonoBehaviour is a class that offers Unity some functions. Such a class is also called interface. Because the class have some explicit functions that are known to unity so unity is able to use them. When you write you own script it's "based" (or to be correct derived from) MonoBehavior.

A program is like a single function. In a lot of programming languages (like c++, java, c#,...) it is actual a function that is named "main()". This function is called by your operating system when you start your program. When you leave this function the program is done and exits. To keep your program running you have to make a loop. Since you're not alone on this PC you have to call some operating system functions that handles the mouse and keyboard input.

In case of Unity the actual program is hidden from you. The Unityplayer (web or standalone...) is also just a program with a main function. Inside this function unity handles the input and also processes the GameObjects you've created.

// That's just a simplified pseudo main function

bool IamStillRunning = true;
void main()
{
   while(IamStillRunning)
   {
      CallSomeOSFunctions();
      foreach(GameObject O in scene)
      {
         O.Update();
      }
      // when all GameObject updates are finished call LateUpdate();
      foreach(GameObject O in scene)
      {
         O.LateUpdate();
      }
      // There will much more like, rendering, physics calculations, ...
   }
}

Now look at this, if only one of your Update function doesn't return the whole program will stuck inside your Update function. I don't want to give a whole lesson in application developing but maybe this helps a bit to understand what's happening "under the hood".

Assuming that 'if' is in an Update, that statement will execute once per frame. The while statement will execute IN ONE FRAME.

What are you trying to achieve? Because even once per frame is pretty fast.