How do I skip a number in a loop in Visual Studio?

0
3
Asked By CodeWizard2000 On

I've got a piece of code that successfully runs a loop 80 times. Here's the code snippet:

```c
for (int i = 0; i < 80; ++i) {
// some logic here
}
```

But I want to modify it so that it skips the number '19'. Does anyone know the best way to implement this?

3 Answers

Answered By CuriousCoder99 On

Just to clarify, using `++i` versus `i++` in this case doesn't affect your loop output. Both will give you a loop from 0 to 79, but the increment method does change when the value is applied in calculations. So, in this case, it's all good!

TechieTommy -

Right? I thought the same thing initially, but they both work fine here.

Answered By DevDude45 On

Another way to handle this is by using an `if-else` structure. Check if `i` is '19' to avoid executing your logic. Just use `if (i != 19) { /* logic */ } else { /* blank or other action */ }`. But honestly, the `continue` approach is way cleaner!

CodeWizard2000 -

Yeah, I totally agree. The `continue` makes it much easier to read!

Answered By TechieTommy On

You can easily skip the number '19' by adding a simple `if` statement inside your loop. Just use `if(i == 19) continue;` to jump to the next iteration whenever it hits '19'. Here's how it looks:

```c
for (int i = 0; i < 80; ++i) {
if(i == 19) continue;
// your logic here
}
```

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.