Thread: changing a for loop to a while loop

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    7

    changing a for loop to a while loop

    need to change this to a while loop help plz

    for (x=1; x < 100; x++)
    sum=sum +x;

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    x = 100;
    while (--x) sum+=x;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    or

    Code:
    x = 1;
    while (x < 100)
      {
         sum += x;
         x++;
    }
    Mr. C: Author and Instructor

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Originally posted by Mister C
    or

    Code:
    x = 1;
    while (x < 100)
      {
         sum += x;
         x++;
    }
    Thats a better version

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing ifstream's source file in a while loop
    By bengreenwood in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2009, 12:45 PM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  3. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM