For loops are looping me.

If anyone remembers, I am the same person who said the way of teaching C++ in my country is outdated and uninteresting.

Now for loops are confusing me. The way they work in my textbook is incompletely given.

Line 12 and line 28 don't matchup.

I am trying to set a counter variable for counting the number of steps to 1.

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
30
 #include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;

int main ()
{
	long int n;
	cout<<"Enter a natural number: "<<setw(5);
	cin>>n;
	cout<<endl<<n<<setw(10);
	for(long i=1;i>1;i=i+1)
	{
		if(n==1)
		{
	    	break;
    	}
		else if((n%2)==0)
		{
			n=n/2;
		}
		else
		{
			n=(3*n)+1;
		}
		cout<<n<<setw(10);
	}
	cout<<endl<<endl<<"It took "<<i<<" steps to reach 1.";
	getch();
}


I can get the code working for

 
for(;;)


in step 12.

I get all the numbers from initial to 1. I need only a counter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
	long n;
	cout << "Enter a natural number: ";
	cin >> n;
	cout << '\n' << n;
	long i = 1;
	for ( ; n > 1; ++i)
	{
		if (n % 2 == 0)
			n /= 2;
		else
			n = 3 * n + 1;
		cout << setw(10) << n;
	}
	cout << "\n\nIt took " << i << " steps to reach 1.\n";
}

Thanks, but I didn't want an answer. I could have figured that one out. I wanted to know how for loops work. And what did I do wrong.

Why

 
long i = 1;


is outside loop?
Last edited on
Scope and lifetime.

The variable(s) declared in the initialization part of the for loop exists only for the duration of the loop. The variable is out of scope, when the loop is over.

The variable in the function scope exists to the end of the function.

http://www.cplusplus.com/doc/tutorial/control/
Good question. Your textbook technically ISN'T wrong believe it or not. Try compiling that program in your lab and you will realize that it works.

But most textbooks will declare all variables at the beginning. Which you have not done and which is giving you your confusion.

You're probably using Turbo C++ right? Or some old compiler.

These compilers most likely will NOT have block specific scopes (I learnt from testing). They only have function specific variables and global variables.

This means that EVERY SINGLE variable that is declared inside the main function, and that includes variables inside for-loops, are local to the function. So if you declare a variable inside a for-loop then you can use it outside the for-loop, but within the same function.

Meaning that for Turbo C++'s standard, your program is correct.

~to answer your question~


But in modern C++ there are block specific variables that means identifiers cannot be reached outside their block.

for-loop variables cannot be accessed outside the for-loop.
So how to combat this? Declare the variable outside the for-loop like dutch did.
Otherwise you would declare a variable both outside and inside the for-loop and assign the variable declared outside the value of inside variable through every iteration.

Yes it might be confusing because what we're taught. I share your sorrows when it comes to our education system.
It is wierd when I have to study Turbo C++ in school, but can't get a reliable source of Downloading it at home. You know any?

Thanks.

P.S.

The program is the Collatz Conjecture.
Don't download turbo c++. Why on earth would you? Not like we get homework that you need to have a compiler at home.. After these one/two years you will never see that compiler again.
Topic archived. No new replies allowed.