[Help] Loop statement

What does the underlined statement mean?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int x = 1;
	while (x <=10)
	{
		cout<<"Hey"<<endl;	
		x = x+1;
	}

	

	return 0;
}
So basically the code is first off initializing x = 1. So now variable x is stored as 1 in memory. It then executes the while statement which is saying, while x is less than or equal to 10, print "Hey" on the screen and then add +1 to x's value. It will keep repeating this till x's value has reached 10, then the program will end. What you will see on your screen is "hey" a bunch of times rather than seeing what is happening with 1.

Try building and running this code to help you understand. It will show you what is happening with x. I suggest typing in different integer values and see what happens. It's good to learn what will happen to a program when you enter different values. That's how you'll learn more. Goodluck.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
using namespace std;

void math()
{
	int x = 0;
	cout << "Enter the number 1:" << endl;
	cin >> x;
	while (x <= 10)
	{
		cout << x << endl;
		x = x + 1;
	}
}
int main()
{
	math();

	return math();

	return 0;
}
Last edited on
Topic archived. No new replies allowed.