I just started learning C++ today, can anyone give me an example of a For Loop (Next) statement?
Printable View
I just started learning C++ today, can anyone give me an example of a For Loop (Next) statement?
Try this:Hope this helps.Code:#include <iostream>
using namespace std;
int main(int argc, char * args[])
{
cout << "For loop example..." << endl;
//Simple for loop to count from 1 to 10.
for(int i = 1; i<11;i++)
{
cout << "i is now: " << i << endl;
}
return 0;
}
I picked this up off a site. What's the differences between it and the one you posted? Is there speed differences or ...? When should each be used?
Code:#include "stdafx.h"
#include "iostream.h"
int main()
{
int counter = 0;
while (counter < 5) {
cout << "Number is: " << counter << endl;
counter++;
}
return 0;
}
1. int main() is used here because the program doesn't need any command lines as the other program can use if it wants to....not so sure of this thou...
2. the while loop is used instead of the for loop: just another type of conditional loop, another one is the do while loop....
3. And one more thing;
using namespace std;
use this one, because it uses the standard name space which for example don't psuh you to use .h on header files and some other things....
hope you understood everything!
when you have the following:
or:Code:A;
while(condition){
A;
}
it is better to switch to the other one. Note that do will always iterate at least once. for(;;) is general purpose and gives more control trough break and continue between the iterated statements. break will terminate the iteration and continue will skip the rest of the code in the current iteration.Code:do{
A;
}while (condition);
A;
The last statement in the for clause ex X++ in for(;;X++) will always execute after each iteration except on break.
Exactly what are you responding to?Quote:
Originally posted by [praetorian]
1. int main() is used here because the program doesn't need any command lines as the other program can use if it wants to....not so sure of this thou...
2. the while loop is used instead of the for loop: just another type of conditional loop, another one is the do while loop....
3. And one more thing;
using namespace std;
use this one, because it uses the standard name space which for example don't psuh you to use .h on header files and some other things....
hope you understood everything!
tried to answer this:
Quote:
I picked this up off a site. What's the differences between it and the one you posted? Is there speed differences or ...? When should each be used?
Oh. I must have been really messed up that day. I understand everything you're saying now. Thanks :)