While Loop???

This is a discussion on While Loop??? within the C Programming forums, part of the General Programming Boards category; Hi all, This is a "very beginner" questions so I apologize. Anyway, I am trying to understand how this line ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Unhappy While Loop???

    Hi all,
    This is a "very beginner" questions so I apologize. Anyway, I am trying to understand how this line of code can detect only even number?

    while( x % 2 != 0)

    I understand that "%" will output only the reminder of x / 2 but, what if x is some other number than 2?

    Thanks.

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    the modulus operator does return the remainder... since x is variable, it will return the remainder of x / 2, correct... this means that on even numbers for x [like 0 2 4 etc], there is no remainder, and it is viewed as zero and the while loop does not continue. on odd numbers for x [like 1 3 5 etc], there remainder is 1, which is not zero, and the while loop prevails... any questions?

    hth
    hasafraggin shizigishin oppashigger...

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    This caode will exit as soon as X is an even number.

    The while continues until x/2 does not (!=) have a remainder.

    For example if x=1, x/2=0 but x%2=1 so the while continues

    if x=100, x/2=50 but x%2=0 so the while will exit.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    I've got to learn to type faster.....

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    hehe, well if it's any consolation, i favor wordy replies dealing with concepts rather than numbers if i can avoid it... so we hit both sides of the brain...
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

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