Te cycles

Discussion on C, C++ and C++11 at the COJ. This is the place to clear your doubts about these languages, and to share with the community the new things you learn about them.
Post Reply
Cubanito91
Posts: 1
Joined: 6 years ago
Gender: None specified

Te cycles

Post by Cubanito91 » 6 years ago

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 ...



User avatar
codemonkey
Posts: 18
Joined: 6 years ago
Gender: None specified

Re: Te cycles

Post by codemonkey » 6 years ago

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.

User avatar
Robbin
Posts: 38
Joined: 6 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Te cycles

Post by Robbin » 6 years ago

Well, we got another way to make a cycle, it's a little annoying, and a little slowly, sometimes,
but it can help you in some occations.. :geek:
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 ;)

User avatar
Robbin
Posts: 38
Joined: 6 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Te cycles

Post by Robbin » 6 years ago

Sorry!

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

User avatar
Phantom
Posts: 58
Joined: 6 years ago
Location: Cuba
Gender: None specified

Re: Te cycles

Post by Phantom » 6 years ago

Robbin wrote:Esto me recuerda el Lenguaje Ensamblador ;)
Of course!! C++ is more like Assembler. :lol:

User avatar
Robbin
Posts: 38
Joined: 6 years ago
Location: Santiago de Cuba
Gender: None specified

Re: Te cycles

Post by Robbin » 6 years ago

Ok.. this part is like assembly, but other parts aren´t.. :!:

User avatar
jelara
Posts: 37
Joined: 6 years ago
Location: Universidad de las Ciencias Informáticas (UCi)
Gender: None specified

Re: Te cycles

Post by jelara » 6 years ago

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.

Spartan
Posts: 11
Joined: 6 years ago
Gender: None specified

Re: Te cycles

Post by Spartan » 5 years ago

Would you give us a link or the document? i haven't heard about it, but it should be really interesting.

Post Reply

Return to “C/C++/C++11”