PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 17, 2022

[FIXED] How to skip certain numbers in a for-loop?

 August 17, 2022     c, for-loop, numbers, output, skip     No comments   

Issue

I started practicing a for loop in C and so far I understand the main principle behind it. But I can't figure out how to get the following output:

1 2 3  5 6 7  9 10 11 ...

I managed to print 1 to 12 with the following for loop but how can I skip 4 and 8 or how to skip any number in general?

for(int i = 1; i < 12; i++)
{
    printf("%d", i);
}

Solution

the simplest solution would be to check with an if statement for any values that you don't want. if you have a rule like not printing all numbers that are divisible by 4 you can make your if statement like this

if(i % 4 == 0)
{
   //print
}

there is no way to do it specifically with the for loop expression.



Answered By - Gilad
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing