Results 1 to 8 of 8

Thread: For Loop

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    For Loop

    I just started learning C++ today, can anyone give me an example of a For Loop (Next) statement?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Smile 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]

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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;
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    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!
    [p r a e t o r i a n]

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    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?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    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?
    [p r a e t o r i a n]

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Oh. I must have been really messed up that day. I understand everything you're saying now. Thanks
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width