I am taking coding as a class and I am having troubles with a small piece of code that keeps freezing up, the idea of the code is to create a while loop and an imbedded while loop in order to count to one hundred, but the game keeps freezing every time I run it. I think that for some reason the loop is not ending and that is causing the problem but I rewrote the code several times and can't fix the problem at all. Where am I going wrong? int a = 0; int b = 0; int c = (b * 10); int d = (c + a); int e = 10; while (d < 100) { a++; if (a >= e) { b++; a -= e; } print (d); }
You're creating an infinite loop. Can you see that "d" is always less than 100 so it'll loop forever? BTW: Here's how to post code on the forums using code-tags.
The code inside the while loop should be adding to the values of A and B and since A and B are a part of C (and then a part of D) eventually the value of D should go up to 100. At least that's what I planned.
"d" won't change unless you change it explicitly. "d" is not a function. It won't always be "int d = (c + a);" as "c" and "a" change. In your above code befoer the loop begins: a is 0 b is 0 c is 0 * 10; d is 0 + 0; e is 10
This! ^ ^ ^ Unity will lock up 100% of the time EVERY millisecond your scripting code is running. Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update. Absolutely NOTHING will happen... until your code either: - returns from whatever function it is running - yields from whatever coroutine it is running As long as your code is looping, Unity isn't going to do even a single frame of change. Nothing. No exceptions. "Yield early, yield often, yield like your game depends on it... it does!" - Kurt Dekker
You need to learn the different C# data types. Int (and all other primitives like float, bool...etc) are called Value Types.
Also worth noting if you want d to always equal c + a, then you could make it into a property: Code (CSharp): public int D { get { return c + a; } }
What kind of coding class? Your code looks like you come from a functional programming language like Haskell where you do not define a sequence of operations but you just define relationships. This is not the case for most imperative programming languages. Functional programming languages are way more abstract and detached from how the hardware actually works. Most imperative languages could almost be seen as a machine language on steroids covered in tons of sugar Just to provide a working example for future readers Code (CSharp): int a = 0; int b = 0; int d = 0; int e = 10; while (d < 100) { a++; if (a >= e) { b++; a -= e; } int c = (b * 10); d = (c + a); print (d); } Just a few notes to the piece of code. Depending on what variable values are actually needed inside the loop, this is just unnecessarily complicated and hard to read. Currently "a" goes from 1 to 9 in the first iteration and after that from 0 to 9. The value of d can be simplifed to d = b*10 +a;. The loop would run up to b==10 and a == 0 at which point the loop would terminate. Here's a .NET fiddle that shows the output of a, b and d. Things like that are usually done with nested for loops. Though since we don't know the exact purpose of this, we can't really say much about what you may want to do instead. The value of d at the point of the print statement would simply go from 1 to 100 (inclusive). If that's all you need, this would be simpler Code (CSharp): for(int i = 0; i < 100; i++) { print(i+1) } The values of a and b are kinda strange since a is essentially offset by 1. Whenever possible you should stick to a 0 based counting system. This makes later calculations much easier.