stepping through a for loop

This is a discussion on stepping through a for loop within the C++ Programming forums, part of the General Programming Boards category; In VB we can use a step statement during a for next loop to step between the counter.My question is,is ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    7

    stepping through a for loop

    In VB we can use a step statement during a for next loop to step between the counter.My question is,is there any alternative to that in C++?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You could probably make one pretty easily. It's been a long time since I did VB, so if you could describe it's behavior in a little more detail I'd be happy to code something for you along that lines.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The 3rd part of the for loop is usally used to control the counter.

    so given:
    Code:
    for (int i=0; i < 10; i++)
    After every iteration of the loop it will add 1 to i.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    7
    aah... i got it.An alternative to:
    Code:
    for x=1 to 10 step 2
    would be:
    Code:
    for (int x=0;x<=10;x+2);
    Is that right?

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Agh.... step... Now I know what you're talking about... must be like the loops in QBasic.

    edit: yes, exactly

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    If the VB for syntax is anything like the Pascal for syntax then

    Code:
    for x=1 to 10 step 2
    would be
    Code:
    for (int x=1; x <= 10; x+=2)

  7. #7
    01000011 00100000 0010000
    Join Date
    Jul 2004
    Posts
    38
    um, you might like the dowhile statement if you came from VB (like me) because dowhile comes with VB too.

    the syntax of dowhile is:

    do{

    'code'...

    }

    while(condition)

    though this is different than for, the difference is that the for statement might not even be executed once if the conditions in the for declarations aren't met, but the dowhile statement is always executed at least once. This is because the conditions for the two functions are listed in 2 seperate places. The for statement's conditions are listed above the code, and therefore are checked before the code executes. The dowhile statement on the other hand has its conditions stated after the code. Therefore the code has to execute at least once before the conditions are checked for the loop. For more info just check back to the tutorials. Those are found at http://www.cprogramming.com.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 08:44 AM
  2. banking, and side stepping
    By scwizzo in forum Game Programming
    Replies: 3
    Last Post: 11-07-2007, 06:35 PM
  3. Stepping through code - can't get past strcat()
    By XenoCodex Admin in forum C++ Programming
    Replies: 2
    Last Post: 07-25-2002, 04:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21