samuraiseoul
samuraiseoul

Reputation: 2967

For and While loops: differences, question, and reasons

I'm trying to figure out some answers to some questions and some difference between While and For loops in C++ and also the reasons? Here's what I've come up with so far. According to http://www.cplusplus.com/doc/tutorial/control/

While is: while (expression) statement

and For is: for (initialization; condition; increase) statement;

so in a for loop, the initialization is where we declare the variable to be used in the condition statement right?

The condition is under what conditions, will it loop.

Then increase is where we decide how much to add or subtract to the variable.

In a while loop the expression is also a condition right? Or are they completely different terms in this case?

I noticed that with the for loop I can move the increase part to the statement if I want to but I can't in a While loop put an increase in parenthesis or declare a variable in the parenthesis (that initialization thing in a for loop). I was kind of curious what the reason is?

Anyways, I'm kind of teaching myself with the help of google, and advice from people, I'm pretty much completely new to programming so please take it easy on me, I'm not up to date with Jargon or complicated answers yet. :) If you need more information or anything please tell me.

Upvotes: 2

Views: 9524

Answers (3)

Basheer AL-MOMANI
Basheer AL-MOMANI

Reputation: 15327

Typically, for statements are used for counter-controlled repetition and while statements for sentinel-controlled repetition.

• Most for statements can be represented with equivalent while statements as follows:

References: Java™ How To Program (Early Objects), Tenth Edition

Upvotes: 0

jay.lee
jay.lee

Reputation: 19837

for loops are more of a convenience that a true language construct. For example, a for loop can easily be expanded into a while loop.

for ( c=0; c<10; c++ ) is equivalent to

c=0;
while ( c<10 ) {
  // some statements
  c++;
}

Also, for loops aren't limited to simple numeric operations, you can do more complex things like this (C syntax):

// a very basic linked list node
struct node {
  struct node *next;
};
struct node; //declare our node

// iterate over all nodes from 'start' node (not declared in this sample)
for ( node=start; node; node=node->next ) {}

which will iterate over a simple linked list.

You can also have multiple initializers, conditions, and statements (depending on the language) as such: for ( c=0, d=5; c<10, d<20; c++, d++ ). But I'd advise against crazy for loops like this as they get rather messy.

Upvotes: 6

Benoit
Benoit

Reputation: 79185

in a for loop, initialization may declare variables or assign a value to them, or both.

Depending on your compiler the scope of those variables will vary. For example, VC++6.0 will extend the scope of a variable outside of the loop, and a common way to circumvent it is horrific:

#define for if(0){} else for

actually, the for syntax is:

for(initialization; loop condition expr; statements to be run when looping)

All of them could be left empty.

Upvotes: 0

Related Questions