OverHeaven
OverHeaven

Reputation: 15

Why does it stop even though I used continue?

Basic coding with while loop and conditional statements. However I've encountered a problem where I'm oblivious to where the fault is at.

So what happens here is that the code will ask the user to input the starting and the ending point for 'loading' and it will do it's thing. The catch is that the code should exclude numbers divisible by 4 in between the start and the end integers.

But in my case, the loop suddenly stops at 3 even though I used 'continue;' in my if statement just below the 'while' line, how should I fix this? 'output'

Anyways, here's my code.

#include<stdio.h>
int main(){
int start, end;

printf("Enter the starting point: ");
scanf("%d", &start);
printf("Enter the ending point: ");
scanf("%d", &end);

while(start <= end){
    if(start % 4 == 0){
        continue;
    }
    printf("Loading...%d%\n", start);

    start++;
}

return 0;

}

Upvotes: 1

Views: 78

Answers (2)

Ratul
Ratul

Reputation: 96

continue keyword doesn't have any problem. The thing is your start variable doesn't increment when continue executed. You can use continue like this. So that, the start variable is incremented before it continues.

if(start % 4 == 0){
    start++;
    continue;
}

One more thing to add. printf("Loading...%d%\n", start); this line should get some warning. because on printf(), % has a special meaning to format variable. So you should use printf("Loading...%d%%\n", start); to get rid of that warning.

Upvotes: 1

Reda Meskali
Reda Meskali

Reputation: 275

while(start <= end){
    if(start % 4 != 0){
        printf("Loading...%d%\n", start);
    }
    start++;
}

Here is a suggestion which does involve continue.

Upvotes: 1

Related Questions