Skipping a number in a counting code

Hi All,

I am having trouble with my code for a counting program. I currently have this:

void setup() {
 
 Serial.begin(9600);
 Serial.println("Counting to 10");
 Count4Me(10);
 
}

void loop() {
 // put your main code here, to run repeatedly:

}

void Count4Me(int num) {
 int i = 1;

 while (i <= num) {
   Serial.println(i);
   i = i + 1;
 }
}

This counts to 10 and then stops, which is perfectly working. However, I now want to make it so that when it goes to count it skips or just doesn't print out a certain number on the monitor (for example the number 4).

I have been looking into the reference for the constrain() command and doing research into it, but I am pretty new to all this and still am not quite sure how it works, or whether I am even on the right track.

Help please!

Thanks in advance, Jack.

void setup()
{

  Serial.begin(9600);
  Serial.println("Counting to 10");
  Count4Me(10);

}

void loop()
{
  // put your main code here, to run repeatedly:

}

void Count4Me(int num)
{
   for (byte i = 1; i <= num; i++)
  {
    if (i != 4)
    {
      Serial.println(i);
    }
  }
}

See ITEM #7

https://forum.arduino.cc/index.php?topic=148850.0

Wow, that works perfectly thanks heaps!

I have looked more into the if() command and the != statement and that makes a lot of sense actually.

Thanks for putting me on the right path!

Then, how about paying the forum back, and editing your code to use code tags, as requested in reply #1?

Done.

Since you pass the number to count up to as a parameter, perhaps you should do the same with the number you want to skip.

Another way to do it:

void Count4Me(int num)
{
   for (byte i = 1; i <= num; i++)
  {
    if (i == 4)
      continue;
   
   Serial.println(i);
  }
}

The 'continue' statement causes the sketch to skip to the next iteration of the loop.