I want this output:
Insert a integer: 13
13
14
16
17
19
Using a for
loop, it works fine:
for( ; ; num++)
{
if (num%3==0)
continue;
else
if(num%10==0)
break;
printf("%d\n", num);
}
But when I try change to a while
loop:
while(1)
{
if (num%3==0)
continue;
else
if(num%10==0)
break;
printf("%d\n", num);
num++;
}
Something strange happens:
Insert a integer: 13
13
14
Can you guys help me, please?
You should add num++
when changing for
to while
loop:
while(1)
{
if (num%3==0) {
num++; /* <- add this */
continue;
}
else
if(num%10==0)
break;
printf("%d\n", num);
num++;
}
two increments num++
in one loop look ugly, so you'll probably want to redesign the loop into
while (num % 10 != 0) {
if (num % 3 != 0)
printf("%d\n", num);
num++;
}
Add the num++
line at the start of your code (while loop). As when the loop reaches num%3==0
, it keeps on re-iterating.
num--;
while(1)
{
num++;
if (num%3==0)
continue;
else
if(num%10==0)
break;
printf("%d\n", num);
}
Use the below code
Do
{
if (num%3==0)
continue;
else
if(num%10==0)
break;
printf("%d\n", num);
}while(num++);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.