Advertisement

What is loop

Started by March 18, 2002 03:40 PM
6 comments, last by Mage_gr 22 years, 9 months ago
Can you explain me what loop is?I tried to read it from my book but I didn''t understand it.
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
A loop contains code that is repeated a given number of times. As an example,

int i = 0;
while ( i < 100 )
{
// stuff to repeat 100 times
i++;
}

If you don''t have one, I suggest that you get yourself a compiler system, here are two free systems: lccwin32 or Dev-C++

Having a compiler system with a debugger will hopefully make it easier for you to learn from your book.


"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Advertisement
This is what a loop is:
first off: pouya: come on, the guys making an honest effort!

now, do get yourself a compiler as suggested by LessBread. If you find that explanation lacking and can''t understand the one in your book you could try this tutorial. The top of the page is about if statements, but farther down it gets to loops. You could also try looking at the rest of the tutorial that that page comes from.

Finally, I don''t know about lccwin32, but Dev-C++ is an ide, not compiler, but you will most likely want the ide as well as the compiler. However, you could also get away with just mingw (i''m assuming windows) and a text editor.
pouya, that''s a nasty thing to give a beginner; you''ll confuse the hell out of him!

Mage_gr, a loop in programming is a sequence of instructions/operations that are pefromed several times in order, until a specific exit condition has been reached. There are two basic looping constructs in programming, which are functionally equivalent - for loops and while loops.

Assuming your programming language to be C/C++ or any syntactically similar language (Java, Perl to an extent), for loops look like this:
for( expr1; expr2; expr3 )  statement_block 


expr1 is typically a statement that initializes a loop counter, expr2 is often a conditional statement that tests for the exit condition and expr3 is a post execution statement, usually counter increment:
for( int n = 0; n < MAX_N; ++n)  statement_block 


It''s important to note that none of these statements is mandatory in C-like languages. You may come across perfectly valid loops that look like this:
for( ; ; )  statement_block 


In such cases statement_block will perform the loop management.

while loops execute just like for loops, except that they have a little less syntactic sugar:
while( condition )  statement_block 


Once condition becomes false, the loop terminates. You can therefore think of a for loop as being equivalent to the following while loop:
expr1;while( expr2 ){  statement_block;  expr3;} 


The final piece of the puzzle - statement_block. In C-like languages, the body of a for or while loop (or an if statement) can only be a single statement. To get around this, however, statements can be blocked together using the curly braces {} and appear as a single statement to code at higher scopes. That allows us to write the following:
while( true ){  CallSomeFunction();  if( SomeConditionTest() == SomeVal )    break;} 


Wait a sec... what''s that break thingie? The break keyword exits the smallest enclosing for, while or switch statement. That means that in the code above, the break statement will exit the loop if SomeConditionTest returns SomeVal. Look, Ma! No counters!

The corollary to the break statement is the continue statement, which advances to the next iteration of a loop construct. Here''s an example:
while( true ){  SomeVar = CallSomeInitFunction();  if( SomeConditionTest( SomeVar ) == SomeVal )    continue;  if( SomeOtherConditionTest( SomeVar == SomeOtherVal )    break;  // the code following only executes for values that "pass" both tests  SomeFunction();} 


Hopefully, that explained loops in a generic, robust and accessible way.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
There is also the do...while() loop. This type of loop is executed at least once. Here is an example of using this loop:

            #include <iostream>using namespace std;int main()int nCountdown = 7;do{cout << nCountdown << " seconds till liftoff.\n";nCountdown--;}while(nCountdown != 0);cout << "LAUNCH!!!\n";return (0);  }  


There is also one other loop. It's more of a control statement, but it can be used as a loop. It's goto.

Using the same source as before without the #include, main(), return statements

        int nCountdown = 7;loop:cout << nCountdown << " seconds till liftoff\n";nCountdown--;if(nCountdown != 0){goto loop;}            


Here loop: is an identifier, and if the goto statement is invoked the program goes back to the identifier following the
goto keyword. You can have as many identifiers as you want,
"loop2, loop3, thisstatement" they are all identifiers, but many people would consider 1 identifier to be more than enough.
A lot of poeple don't recommend using goto cause it can make reading the program harder. But it does have some useful uses.



[edited by - Brian Jones on March 18, 2002 7:31:45 PM]
I don't have a signature
Advertisement
I can''t believe that I had to edit that 4 times. haha
I don't have a signature
quote: Original post by echeslack
Finally, I don''t know about lccwin32, but Dev-C++ is an ide, not compiler, but you will most likely want the ide as well as the compiler. However, you could also get away with just mingw (i''m assuming windows) and a text editor.


lccwin32 is a dev system - compiler, debugger and a few other toys for win32 - but it''s straight C only - some c99 - but anyway, if this fellow is having troubles with loops, then maybe he should just play around with C first before digging into C++.

Dev-C++ is an IDE, but the "easy to grab and install" package includes mingw, and bloodshed also provides a link to the insight debugger which works better (easier) than the default dev-C++ debugger.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement