Steps in Loop in C/C++


What are possible ways to skip or step a variable in a loop in C/C++ particular in C only?

In Basic, there's the STEP

for x = 0 to 10 step 2
print x ; print out even numbers..
next

I found something like using double commas (,,) but couldn't figure out the whole setup.
Last edited on
1
2
for (int i = 0; i < 10; i += 2) 
  std::cout << i << '\n';  
Thanks mbozzi, I already figured the += as one way.

are there other ways?
Last edited on
In C++, there are endless ways to obfuscate the ‘step’ value, but just as in BASIC you can only write step 2 in C++ the most correct, most readable way is simply to increment the variable correctly, as exampled by mbozzi.

Ranges make life pretty, but weird. Stick with your standard for-loop syntax (unless you have good reasons not to).
Simple syntax? any combined equal operator, which includes bit logic, so that += is like unto
*= //eg 2,4,8,16
/= //16,8,4,2
-= //opposite +=
&= //bit and logic
|= //bit or
^= //xor
~= //I think, not = (not the same as !=, which is an expression not an assignment)
%= //modulo or remainder
dunno if I missed any, its 2 am and stuff here... is there a shift one?

and then there are the home grown ones, via lambda, functions, objects, or simply looping over a container of values like prime numbers.

a function, for example...
int sequencer()
{
static int x = 1;
return (++x * 5 +42)^ 1973;
}
...
for(int i = 0; i < 100; i++)
cout << sequencer() << endl;
Note that in a for loop there are no 'constraints' as to what can be performed in each of the 3 parts - unlike Basic. Simply:

1
2
3
4
for (a; b; c)
    // d

// f 


a statement is executed once. It may include a variable definition.

b condition is evaluated at the beginning of each of the loops. This can be any statement that can be evaluated to a bool (true/false, 1/0). If this evaluates to false (0) then statement f following the for loop is executed.

d statement is executed (if present) if the b condition is evaluated true (1). This can be a compound statement.

c is executed after d (even if d is not present).

b condition is evaluated again - and the loop continues...

All of a, b or c are optional. They need not be present if not required - although the 2 ; are required. If b is not present, it is assumed to be true.

This gives great flexibility in the use of the for statement (and abuse in making b and c almost unreadable!).


Also note that in C/C++ there is the , operator.

 
x, y, z


This is one statement - consisting of 3 expressions x y z
x is executed, then y, then z. The result of the statement is that obtained from z. Using this , operator can be used anywhere a statement is required - but is especially useful for statement c in the for loop.

https://en.cppreference.com/w/cpp/language/for
https://en.cppreference.com/w/cpp/language/operator_other
Last edited on
The condition (b in the above explanation) can also be the declaration of a variable (with a brace-or-equals initializer) which is contextually convertible to bool.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    // process input files file.0.txt, file.1.txt, file.2.txt ... till a file open fails
    for( int i = 0 ; std::ifstream file{ "file." + std::to_string(i) + ".txt" } ; ++i )
    {
          std::cout << file.rdbuf() << "\n--------------------------\n" ;
          // ...
    }
}
Last edited on
... and can also be used where a condition is required - eg with if and while statements.
Topic archived. No new replies allowed.