|
-
Feb 6th, 2002, 03:51 PM
#1
Thread Starter
Stuck in the 80s
For Loop
I just started learning C++ today, can anyone give me an example of a For Loop (Next) statement?
-
Feb 6th, 2002, 04:46 PM
#2
Fanatic Member
Code.
Try this:
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;
}
Hope this helps.
Digital-X-Treme
Contact me on MSN Messenger: [email protected]
[VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
/ (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]
-
Feb 6th, 2002, 05:13 PM
#3
Thread Starter
Stuck in the 80s
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;
}
-
Feb 6th, 2002, 06:33 PM
#4
Addicted Member
explanations
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!
-
Feb 6th, 2002, 07:23 PM
#5
transcendental analytic
when you have the following:
Code:
A;
while(condition){
A;
}
or:
Code:
do{
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.
The last statement in the for clause ex X++ in for(;;X++) will always execute after each iteration except on break.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 6th, 2002, 11:17 PM
#6
Thread Starter
Stuck in the 80s
Re: explanations
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!
Exactly what are you responding to?
-
Feb 9th, 2002, 07:44 AM
#7
Addicted Member
tried to answer this:
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?
-
Feb 9th, 2002, 02:37 PM
#8
Thread Starter
Stuck in the 80s
Oh. I must have been really messed up that day. I understand everything you're saying now. Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|