I need help with this while loop anyone?
The while
statement is composed by a condition and a job to do
while(condition){ do job; }
The while statement will repeat the job until the condition is not true, (by example: count < 1000
)
If the job doesn’t change the result of the condition, then the while
will not finish
Example (in the block inside the while
the value of count
never changes, so the while is infinite):
int count = 1;
int result = 0;
while(count < 1000){ result = result + count; }
Example (the value of count
increases, the while
will end when count
has a value of 1001
):
int count = 1;
int result = 0;
while(count < 1000){
result = result + count;
count = count + 1;
}
The condition can be anything that evaluates to true
or false
The job can be anithing, just remember it must change the values used in the condition
that is an Infinite loop never reach the sum+1 you should put a const for example
while (sum < 1000){
// here put your code…
}