Function of "for"

Can someone tell me what is the function of "for" in this program? I guess the "for" is just to give to the block the value of "d". The program is the solution of the exercise "while(user==gullible)". I did it knowing that there had to be the "for" i just want to know why.
Please help.
Thanks in advance.(sorry the bad English);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;
int main ()
{
	int d, c;

	cout << "Please enter any number other than 0: ";
	cin >> c;

	for (d=1;d<99;d++)
	{
		getchar();
		if (c!=0)
		{
			cout << "Please enter any number other than " << d << ": ";
			cin >> c;

			if (c == d )
			{
			cout << "OTHER THAN " << d << "!!";
			getchar ();
			goto Exit;
			}
		}
	}
	Exit:
	getchar ();
	return 0;
}
The for loop
1
2
3
4
for (d=1;d<99;d++)
{
	...
}
would look something like this if it was written as while loop.
1
2
3
4
5
6
d=1;
while (d<99)
{
	...
	d++;
}


d is only used in the loop so you can make it local to the for loop by removing the definition of d on line 5 and change the loop to
1
2
3
4
for (int d=1;d<99;d++)
{
	...
}
Hmm. Is there any other way to increase the d without using while loop nor for loop?
Increasing the value of d is easy. The more interesting question is how to repeat the same block of code more than once without using a while or for loop.
Hmm ok.
Then the for (d=1;d<99;d++) will only affect his block, and if i want i can do another loop but increasing 2 in the same main function?
another loop but increasing 2 in the same main function?
Sorry, I don't understand "increasing 2" here.
d+2for (d=1;d<99;d+2) isn't this possible?
I mean add a second for where d have other value
Last edited on
Do d = d + 2 or d += 2 to increase d by 2.
Topic archived. No new replies allowed.