# Thread: While Loop???

1. ## 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. 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

3. 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. I've got to learn to type faster.....

5. 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...

Popular pages Recent additions