Page 1 of 1

Te cycles

Posted: Mon Dec 12, 2011 12:09 pm
Well, I think this topic to promote discussion about different ways to program in this programming language. I am interested in someone enlighten me about the various ways to create cycles, as the loop "for" the "while" or "DoWhile." I hope answers ...

Re: Te cycles

Posted: Wed Dec 14, 2011 6:41 pm
Cubanito91
As you say, there are 3 ways to create cycles. The way more used is without question, 'for' loop. The theory about this loop you can found it in any book about c++.
The general structure of for statement is the following:
for ( init_stament; condition; expression){
statement;
}

Here I let you some ways I use 'for' loop:

1st: When a problem has many test case and first it's entry how many will be.
for ( cin >> t; t; t-- ){
// the code for each test case here
}

2nd: To traverse a container as a vector, a map, etc.
for (map<int,int>::iterator it = m.begin(); it != m.end(); it++){
cout << it->first << " " << it->second << endl;
}

3rd: A naive algorithm to know if a number is prime can be:
bool is_prime(int n){
if( !(n & 1) && n != 2 ) return false; // n is even and isn't 2.
for( int i = 3; i * i <= n; i += 2 ){ // only check odd numbers.
if( !(n % i) ) return false; // n is divisible for i.
}
return true;
}

The 'while' loop is similar to 'for', look this:
for( init_statement; condition; expression){
statement;
}

is equivalent to

init_statement;
while( condition ){
statement;
expression;
}

So, is your choise when to use each of these loops.

I use the do-while rarely because I always choose among 'for' or 'while'.

I hope I had been useful for you.

Re: Te cycles

Posted: Tue May 15, 2012 9:34 pm
Well, we got another way to make a cycle, it's a little annoying, and a little slowly, sometimes,
it's the goto:

you put a tag, and at the end of your code, if the condition for the loop isn't the one that you want you say: goto <etiqueta>

Example:

while( condition ){
statement;
expression;
}

it would be like this:

etiqueta:
//......
//code of the cycle
//
if(!condition) goto etiqueta;

Esto me recuerda el Lenguaje Ensamblador

Re: Te cycles

Posted: Tue May 15, 2012 9:35 pm
Sorry!

I forgot I must write in English only.. sorry for the last line

Re: Te cycles

Posted: Sun Jun 03, 2012 11:35 pm
Robbin wrote:Esto me recuerda el Lenguaje Ensamblador
Of course!! C++ is more like Assembler.

Re: Te cycles

Posted: Tue Jun 05, 2012 6:47 pm
Ok.. this part is like assembly, but other parts arenĀ“t..

Re: Te cycles

Posted: Tue Oct 09, 2012 1:58 pm
Guys, you should read the document that made Dijkstra famous (no, it wasn't the algorithm): it was a letter called "Goto statement considered harmful". That document actually set the basis of what we now know as structured programming.

BTW, the difference between a while loop and a do/while loop is that while checks the condition before each iteration, whereas do/while checks it after each iteration. The only practical difference is that do/while always makes the first iteration, and while may not iterate at all if the condition is false at the beggining.

Re: Te cycles

Posted: Mon Jul 08, 2013 6:15 pm
Would you give us a link or the document? i haven't heard about it, but it should be really interesting.