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.
Cubanito91
Posts: 1
Joined: Mon Nov 21, 2011 1:39 pm
Gender: None specified

Te cycles

Postby Cubanito91 » 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 ...



User avatar
codemonkey
Posts: 18
Joined: Thu Nov 17, 2011 3:42 pm
Gender: None specified

Re: Te cycles

Postby codemonkey » 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.

User avatar
Robbin
Posts: 38
Joined: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Te cycles

Postby Robbin » 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,
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: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Te cycles

Postby Robbin » Tue May 15, 2012 9:35 pm

Sorry!

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

User avatar
Phantom
Posts: 58
Joined: Thu Nov 17, 2011 11:21 am
Location: Cuba
Gender: None specified

Re: Te cycles

Postby Phantom » Sun Jun 03, 2012 11:35 pm

Robbin wrote:Esto me recuerda el Lenguaje Ensamblador ;)


Of course!! C++ is more like Assembler. :lol:

User avatar
Robbin
Posts: 38
Joined: Mon Nov 21, 2011 11:42 am
Location: Santiago de Cuba
Gender: None specified

Re: Te cycles

Postby Robbin » Tue Jun 05, 2012 6:47 pm

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

User avatar
jelara
Posts: 37
Joined: Mon Nov 07, 2011 9:00 pm
Location: Universidad de las Ciencias Informáticas (UCi)
Gender: None specified

Re: Te cycles

Postby jelara » 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.

Spartan
Posts: 11
Joined: Thu Apr 05, 2012 9:36 pm
Gender: None specified

Re: Te cycles

Postby Spartan » 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.


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

Who is online

Users browsing this forum: No registered users and 1 guest