you could use a variable to increment your counter

for(int counter = 0, increment = 0; counter < 100; increment++, counter += increment){
   ...do_something...
}
Answer from fabio.ivona on Stack Overflow
🌐
Quora
quora.com › How-can-a-variable-increment-by-2-in-a-while-loop-in-C-language
How can a variable increment by 2 in a while loop in C language? - Quora
Answer (1 of 13): counter+=2; This has the same effect as counter = counter + 2; Read as, add two to the existing value of counter and assign that to counter.
Discussions

How do I put two increment statements in a C++ 'for' loop? - Stack Overflow
Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... New site design and philosophy for Stack Overflow: Starting February 24, 2026... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... 2 Can I have two initializing statements in a "for" loop? ... 2 A "for" loop with two variables, one increments ... More on stackoverflow.com
🌐 stackoverflow.com
For loop increment question
Why is it when you want to increment by one, it's variable++ but if you want to increment by other value, you have to spell it out fully like variable = variable +2? Why can't I just do a straight variable + 2 in the for loop? for (int x = 0; x More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
May 25, 2020
[C] Post-increment or pre-increment in for loops?
Well, you can try it out and see. At least, neither gcc nor clang (LLVM) showed any difference at all: https://godbolt.org/g/zOhRse #include int main() { for (int i = 0; i < 5; i++) { // } for (int j = 0; j < 5; ++j) { // } return 0; } generates (for gcc, but you can change the compiler to to your heart's desire in the given link, and check) main: push rbp mov rbp, rsp mov DWORD PTR [rbp-4], 0 .L3: cmp DWORD PTR [rbp-4], 4 jg .L2 add DWORD PTR [rbp-4], 1 jmp .L3 .L2: mov DWORD PTR [rbp-8], 0 .L5: cmp DWORD PTR [rbp-8], 4 jg .L4 add DWORD PTR [rbp-8], 1 jmp .L5 .L4: mov eax, 0 pop rbp ret So, .L3 corresponds to the first for loop, and .L5 corresponds to the second for loop. As you can see, identical. Bottomline: micro-optimisations (if at all) like these are practically useless unless you are working in a domain which does require you to understand exactly what your code is generating. In 99% of the cases, don't sweat it. Modern compilers (especially C++ compilers) do a lot of magic behind the scenes that makes it hard for us to keep track of exact details at times (without looking at the generated Assembler/IR code, that is). EDIT: And oh, by the way, I use post-increments all the time, unless I need to use pre-increments. I just find it easier to type i++) { than ++i) { - more efficient usage of the Shift key... hahaha! :P More on reddit.com
🌐 r/learnprogramming
33
11
May 9, 2017
I need for loop help - C++ Forum
Instead of using double x within the for loop, use a different name, ex: double a. When you have it recall x, it will just erase the input from earlier in the code. This most likely causes nothing to be outputted cause x is most likely a random big number greater than 0.5. The other thing x++ would increment by ... More on cplusplus.com
🌐 cplusplus.com
🌐
Reddit
reddit.com › r/cprogramming › question about incrementation when looping.
r/cprogramming on Reddit: Question about incrementation when looping.
February 7, 2023 -

Hi. As the title state my question is about incrementation when looping. I understand it may sound stupid and for that I apologize.

When you use a nested for loop you write :

for(int i=0; i<5; i++) { for(int j=0; j<5; j++) { Task; } }

My question is when exactly does incrementing happen? Does i get incremented then go into the inner for loop? Does j complete the task then get incremented?

Thank you to anyone who help. I also apologize if I'm not making sense.

Top answer
1 of 5
2
In a for(initialization; condition; increment) loop, the "initialization" code is always executed, before anything else happens. Then the loop begins. The loop is equivalent to a while loop in that there is a "condition", and this condition is tested before every iteration -- including the first iteration. (This means your for or while loop might never execute the body of the loop, which is correct.) At the end of the loop body, or the beginning of every iteration except the first, the "increment" part is executed. Thus, the increment does not run before the first time, but does run every other time. The use of a continue statement to advance to the next iteration through the loop will run the increment part. Simplified, it looks like this: initialization part goto condition top-of-loop: body of loop /* continue stmt */ goto end-of-body /* break stmt */ goto break end-of-body: increment part condition: evaluate condition part if truthy goto top-of-loop break: whatever code is after your for loop Note here that truthy in C has a particular meaning: non-zero. Any "condition" type test will compare the result of a contained expression against zero. A zero is considered false, and any value other than zero is considered true. "1" is true. "1000000" is true. "6.02E+23" is true. "&main" is true. "NaN" is true. "Inf" is true. "-Inf" is true. I honestly do not know if -0 is true or not, on a sign+magnitude CPU. Yes, there is a boolean type. But the boolean type was added much, much, much later. Before there was a boolean type, everything was int, and so the foundation of the language assumes that the truthiness of int value: 0 vs. non-0, drives everything.
2 of 5
1
You could try it, for example, by printing i and j as your task. What should happen is: i=0, j=0, task i=0, j=1, task i=0, j=2, task i=0, j=3, task i=0, j=4, task i=1, j=0, task i=1, j=1, task ... So the incrementing happens before task, if I understand you question correctly.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 57856-incrementing-2-not-1-a.html
Incrementing by 2 not 1
October 14, 2004 - for ( i = 3; i < sqrt(n); i++ ) "this adding by one" could i write for ( i = 3; i < sqrt(n); i2++ ) What is the proper way of writing this so the counter increments by 2 Thanks Matt ... View Forum Posts Cheesy Poofs! ... Thanks man. quick response, very cool.
🌐
Codecademy
codecademy.com › forum_questions › 508fe06f900ba10200002d7f
the for loop increment | Codecademy
As @Alex J explained above, the shorthand arithmetic operators are stand-ins for the full blown statement, as in, counter = counter + 2, or counter = counter -5. Increment always means ‘add to’, decrement always means ‘subtract from’.
🌐
Tutorial Gateway
tutorialgateway.org › for-loop-in-c-programming
For Loop in C Programming
February 7, 2026 - Step 1 (Initialization): We initialize the counter variable(s). It is an entry to the for loop. Example, i=1. Step 2 (Expression): It will check the condition against the counter variable. Step 3: If the condition is True, the compiler moves to the group of statements section and executes the statements inside it. Step 4 (Update): After completing the iteration Step 3 (from the group of statements section), it will execute the Increment and Decrement Operators inside it to increment or decrease the value.
🌐
Quora
quora.com › How-do-you-code-a-for-loop-that-increments-by-two-numbers-at-once
How to code a for loop that increments by two numbers at once - Quora
Former Games developer MD Graftgold. (1985–1998) · Author has 1.2K answers and 331.4K answer views · 3y · A for loop is just a loop that may include an Increment within the for statement. In C the Increment doesn't have to be in the statement, or you can have more than one separated by commas.
Find elsewhere
🌐
The Valley of Code
thevalleyofcode.com › lesson › c-basics › loops
C Basics: Loops
Then it is incremented as the increment part says (i++ in this case, incrementing by 1), and all the cycle repeats until you get to the number 10. Inside the loop main block we can access the variable i to know at which iteration we are. This program should print 0 1 2 3 4 5 6 7 8 9 10:
🌐
Learning Monkey
learningmonkey.in › home › multiple initializations and increments in for loop
Multiple Initializations and Increments in for loop Easy Way Lec:: 38.2
March 13, 2019 - Multiple Initializations and Increments in for loop Example 1 · In the above program, two variable i and j has been initialized and incremented. A comma has separated each initialization and incrementation.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
For loop increment question - Programming - Arduino Forum
May 25, 2020 - Why is it when you want to increment by one, it's variable++ but if you want to increment by other value, you have to spell it out fully like variable = variable +2? Why can't I just do a straight variable + 2 in the for loop? for (int x = 0; x
🌐
Sololearn
sololearn.com › en › Discuss › 1492044 › what-to-do-to-increment-by-2-instead-of-1-
What to do to increment by 2 instead of 1 ? | Sololearn: Learn to code for FREE!
public class Program { public static ... what should I do to get the output 7 instead of 6? ... You have written ++test. Here ++operator increments the value of test by 1 .You can also write like this test += 1; it means test = test+1; For ...
🌐
Reddit
reddit.com › r/learnprogramming › [c] post-increment or pre-increment in for loops?
r/learnprogramming on Reddit: [C] Post-increment or pre-increment in for loops?
May 9, 2017 -

I habitually used post-increments in for loops because the examples C Programming: The Modern Approach used them. E.g:

for(int i = 0, i < size; i++) {
    // Some code here
}

Somebody here on r/learnprogramming mentioned that s/he uses pre-increments instead because a post-increment creates a temp variable to return the value of i, which might reduce performance, so I started using ++i instead in for loops. But AFAIK the compiler is fairly smart at optimizing code, so I'm not sure if this code will compile to a more optimized binary if I use the pre-increment. Does it really make a difference, or is it merely a style thing?

🌐
Programtopia
programtopia.net › home › c programming › for loop in c programming
for loop in C Programming - Programtopia
January 15, 2021 - In this part, the variable required for the loop is initialized. It is executed only once at the first iteration. This part is optional and may be absent. ... for(i=0; condition; increment/decrement) for( ; condition; increment/decrement) // initialization is absent
🌐
W3Resource
w3resource.com › c-programming › c-for-loop.php
C for loop - w3resource
August 19, 2022 - A "For" Loop is used to repeat a specific block of code (statements) a known number of times. The for-loop statement is a very specialized while loop, which increases the readability of a program. Here is the syntax of the of for loop. for ( initialize counter ; test counter ; increment counter) { execute the statement(s); }
🌐
Florida A&M University
web1.eng.famu.fsu.edu › ~haik › met.dir › hcpp.dir › notes.dir › cppnotes › node45.html
for Loop
The for is one of the most versatile statements in the C++ language because it allows a wide range of variation from its traditional use. For example, multiple loop control variables can be used. Consider the following fragment of code: ... Here, commas separate the two initialization statements and the two increment expressions.
🌐
R-bloggers
r-bloggers.com › r bloggers › mastering for loops in c: a comprehensive beginner’s guide with examples
Mastering For Loops in C: A Comprehensive Beginner’s Guide with Examples | R-bloggers
December 4, 2024 - Condition: Next, the condition is evaluated. If it’s true, the body of the loop is executed. If it’s false, the body of the loop is skipped and the loop is terminated. Increment/Decrement: After the body of the loop executes, the increment/decrement statement is executed, and the condition is evaluated again.
🌐
Cplusplus
cplusplus.com › forum › beginner › 115698
I need for loop help - C++ Forum
I most likely think you want to increment by 0.1. ex: x += 0.1. So overall, your for loop should look like: for(double a = x; a <= 0.5; a += 0.5). Or even yet, you could try this: for(x; x <= 0.5; x += 0.5).
🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › c-for-loop
C – for loop in C programming with example
Step 3: After successful execution of statements inside the body of loop, the counter variable is incremented or decremented, depending on the operation (++ or –). #include <stdio.h> int main() { int i; for (i=1; i<=3; i++) { printf("%d\n", i); } return 0; } ... I am using variable num as the counter in all the following examples – 1) Here instead of num++, I’m using num=num+1 which is same as num++. ... 2) Initialization part can be skipped from loop as shown below, the counter variable is declared before the loop.