New to C++

I am lost and very new to this, what do I, x, and y equal and how do you work it out?

int I = 0;
int x = 0;
int y;

while(i<3){
x = x+5;
i++;
}
So the difference between y and the other two integers (i and x) is that you have instantiated them with the value 0. One thing to note here is that the int type in C++ is a built in data type and they do not have implicit initialization.

What that means is, what you did here:
int y;

y doesn't have a value. In fact, whatever is in y can be garbage and it would be illegal to do anything with y other than to overwrite it.

For a type that's not built in and part of the standard library, like string, there is implicit initialization. That means if you don't initialize the value yourself, the object of said data type assumes a value. For instance:

string b;

b here is actually the empty string and is because it's defined this way in the class.

For your values, x = 15 and i = 3 at the end of the loop. This is because you run through the while loop three times. When i - 0, you first check the condition (i < 3) before executing any code inside the statement block. Since it is true, we execute and increment i. i is checked 4 times and the code inside is executed 3 times.
1
2
3
4
5
6
7
8
int I = 0; // I is 0
int x = 0; // x is 0
int y; // You declare this, but you don't initialize it. It has no value.

while(i<3){ // This is invalid, since i is undefined. C++ is case sensitive, so something like int I and int i are different
x = x+5; // This redefines x as itself plus 5.
i++; // Again, this i is undefined. (You probably meant I)
}
Topic archived. No new replies allowed.