Thread: for loops - newbie q's

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    for loops - newbie q's

    I understand that for loops are used as
    for(variable = value)(relational expression)(inc dec value) {loop}

    my question is, do you have to increment or decrement the variable set at the first of the function? and do you even have to set a variable at all? Like could you just say..
    for (i != 0) {loop that modifies i} ..or would that just be a for loop acting as a while loop.

    I cant see a whole lot of uses for the for loop.. from what I see it's just a while loop with a preset number of loops.

    could anyone give me a simple example of other uses for the for loop besides the plain for(i = 1)(i <=20) (i++)

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The good thing about for loops is that they do three functions in one. They set the starting value, evaulate the expression, and make the incremental change.
    With a while loop you have to set the value before the while loop and make sure you put in the incremental change in the loop. I can't count the number of times I've forgotten to put the incremental change in a while loop.
    Every different typle of loop has its strong points and weak points. Just need to identify them and use the best loop for the job.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    9
    so the only valid expression on the third part of the function is (var)++ or (var)--?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Nope, you can do other ones.
    ie

    for(i=0;i<x;i+=15)

    It seems like you can do any math function. I don't know all the limits but i++ or i-- are not the only ones that can be done.

  5. #5
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    >I cant see a whole lot of uses for the for loop.. from what I see it's just a while loop with a preset number of loops.

    A for loop is a subset of the while loop - a for loop is only used for counter-controlled repetition, and has very little other use. A while loop can be used for counter controlled repetition, but not very often. Normally a while loop is used to evaluate a condition, like an if/else statement.

    Code:
    while (condition1 == true){
      do(someaction)
    }
    while (condition 1 !=  true){
      do(someotheraction)
    }
    Try here for some examples of the for loop (go onto the other sections as well).

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    With for loops, you can have a loop without the increment value, without the condition, without the intialization, or any combination of two, or none of the above.

    Code:
    for(  ;  ;  )   /*empty*/
    {
       if(condition) break;
    }
    
    i = 0
    for(; i < 5; ) /*conditional only*/
    {
        printf("%d", i++);
     }
    Just two of the permitations of the for loop.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thats pretty cool bigtamscot, never knew that. Of course in both cases I would have used a while loop.

  8. #8
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    >Of course in both cases I would have used a while loop

    Well a while loop generally saves you time, but bigtamscot is right in what he's saying.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Ofcourse we would use a while loop first of all, just demonstrating flexability of for loop.
    Last edited by bigtamscot; 09-26-2001 at 05:36 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM