views:

73

answers:

2

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

A: 

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.

Benoit
+6  A: 

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.

jay.lee
Okay, this helped, could you give another example of the for ( node=start; node; node=node->next ) {}I'm not sure what a node is. T_T So I didn't really get the example.Also on the initialization, could I dofor (int i; i=0; i<10; i++){}or does it have to be for (int i=0; i<10; i++){}If not why?
Samuraisoulification
I edited for elaboration and added some extra stuff. Being able declare x in the loop like for (int x;;) depends on the language. In C it's not allowed, but in C++ it is.
jay.lee
Okay, I meant more could I do though (int i; i = 0; .....) or must I do (int i = 0; ...)?
Samuraisoulification
use for (int i=0;;). for (int i; i=0;) means the i=0 is the conditional statement.
jay.lee
Oh! That makes sense, thanks!
Samuraisoulification