The i++ (and ++i) is done as part of the while expression evaluation, which happens before the printing. So that means it will always print 1 initially.

The only difference between the i++ and ++i variants is when the increment happens inside the expression itself, and this affects the final value printed. The equivalent pseudo-code for each is:

while(i++ < 10)            while i < 10:
                               i = i + 1
    printf("%d\n", i);         print i
                           i = i + 1

and:

                           i = i + 1
while(++i < 10)            while i < 10:
    printf("%d\n", i);         print i
                               i = i + 1

Let's say i gets up to 9. With i++ < 10, it uses 9 < 10 for the while expression then increments i to 10 before printing. So the check uses 9 then prints 10.

With ++i < 10, it first increments i then uses 10 < 10 for the while expression. So the check uses 10 and doesn't print anything, because the loop has exited because of that check.

Answer from paxdiablo on Stack Overflow
Discussions

python - Increment and while loop - Stack Overflow
Following is the code I'm executing in IDLE for Python 3.5.2 on Windows 10: spam = 0 while spam More on stackoverflow.com
๐ŸŒ stackoverflow.com
Do-While loop. Increment - C++ Forum
The output is 6 10 5 This is what ... the while condition, how did 'funny' and 'serious' output their values up to the 4th iteration. I thought their outputs would be at 3rd iteration. And more confusing to me, how did 'count' increment all the way to 5 (skipping 4). Please advise. Thanks in advnace... ... before it is incremented. The last time through the loop: The second ... More on cplusplus.com
๐ŸŒ cplusplus.com
while loops with increments - C++ Forum
I would like to be shown a bit about how to do while loops with increments Hi there I have recently started using Dev c++ in my computer labs at school, I know your not here to give the ansers to my work but i would just like somebody to point me in the write direction It took me a while to ... More on cplusplus.com
๐ŸŒ cplusplus.com
May 30, 2010
c - How to increment a counter for a while loop within the loop? - Stack Overflow
I have a feeling I'm gonna feel really stupid here, but I'm just learning about using ++ and -- to increment and decrements variables for while loops, and was wondering why this piece of code works... More on stackoverflow.com
๐ŸŒ stackoverflow.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.
๐ŸŒ
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.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ while-loop-in-c
While Loop in C Programming
March 23, 2025 - If the condition results true, the number is added to the total. Otherwise, it will exit from the iteration. We used the ++ operator in the next line to increment the number value.
Find elsewhere
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-while-loop
Python While Loop
March 25, 2025 - The condition (number<= 10) checks whether the number is less than or equal to 10. If the expression result is true, the number is added to the total. Next, We used + to increment the number value in the Python while loop.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ StudentCSP โ€บ CSPWhileAndForLoops โ€บ whileCount.html
8.3. Counting with a While Loop โ€” AP CS Principles - Student Edition
Counter is incremented each time the loop executes. ... The last value to be printed is 10. But, the counter is incremented after the current value is printed. ... Counter gets incremented to 11 after printing, and then the while loop tests counter, finds counter is not less than 11 and then ...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 159476
Do-While loop. Increment - C++ Forum
The value of count++ is 3 so the loop continues. The last thing through the loop count++ increments count from 4 to 5.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ lounge โ€บ 24448
while loops with increments - C++ Forum
May 30, 2010 - I was really starting to enjoy c ++ before i could majorly stuck for hours on end! heres what i have managed to do so far #include <math.h> #include <stdio.h> #include <string.h> #include <iostream> int read_int(char [],char []); int int1; double double1; /*int result; I dont know how to use this, but just know it musthere.*/ int main(){ /* int1=1 /this is me trying to get a while loop running while (int1<500)*/ double1=read_int("number1:","lf"); int1=read_int("number2:","%d"); printf("number1 to the power of number2 = %lf \r\n", pow(double1,int1)); /*return result;*/ system ("PAUSE"); return 0; } int read_int(char prompt[],char format[]) // subroutine to get undisclosed varying size of number// { char line[100]; int retvalue; printf("%s",prompt); fgets(line,sizeof(line),stdin); line[strlen(line)-1]='\0'; sscanf(line, "%d", &retvalue); return retvalue; }
๐ŸŒ
Quackit
quackit.com โ€บ python โ€บ tutorial โ€บ python_while_loops.cfm
Python While Loops
while loops are often used with a counter โ€” an incrementing (or decrementing) number.
Top answer
1 of 3
5

In the first case

while (ctr < 10)
  printf("%d",ctr);
  ctr=ctr+1;

the while loop body is considered only the printf() statement. ctr=ctr+1; is not part of the loop body. So you have an unchanged variable in loop condition check, which makes it infinite loop.

You need to enclose both the statements in a block scope, using {} so that both the statements become part of the loop body. Something like

while (ctr < 10) {
  printf("%d",ctr);
  ctr=ctr+1;
}

will do.


In the second case

int ctr=0;
while (ctr++ < 10)
    printf("%d",ctr);

ctr is already incremented as the side effect of the postfix increment operator, in the while condition checking expression. Thus, while printing the value, the already incremented value is being printed.

2 of 3
0

It is quite simple, indeed.

int ctr = 0;
while (ctr < 10)
  printf("%d",ctr);
  ctr=ctr+1;

In this first piece of code, in spite of the indentation, your while is only involving printf("%d",ctr);, because there is no block making ctr=ctr+1; belong to the while.

It could be written:

int ctr = 0;
while (ctr < 10)
  printf("%d",ctr);
ctr=ctr+1;     // This is not in the loop, even with the previous indentation.

There is no increment to ctr in this loop and then it will run forever printing zeros.

In this second piece of code

int ctr=0;
while (ctr++ < 10)
    printf("%d",ctr);

you do increment ctr every pass and it will work fine.

If you want to make the first loop work, write it like this:

int ctr = 0;
while (ctr < 10) {
  printf("%d",ctr);
  ctr=ctr+1;
}

Now ctr=ctr+1; is really inside the while loop.

๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ javascript fundamentals
Loops: while and for
But here we have the postfix form i++. So it increments i to 5, but returns the old value. Hence the comparison is actually while(4 < 5) โ€“ true, and the control goes on to alert. The value i = 5 is the last one, because on the next step while(5 < 5) is false. ... For each loop write down ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ can someone explain increment and decrement operator in a while loop?
r/learnjava on Reddit: Can someone explain increment and decrement operator in a while loop?
January 15, 2020 -

I've been doing the MOOC Introduction to OOP course part 1 for a while, and I've been having a blast. I'm used to python, and no other language has got my attention like this, for some reason.

There is a thing, I don't quite understand. The increment operator in it self, raises the value of your variable by 1. However, in several excercises now, we have been doing something similar to this:

import java.util.Scanner;

public class ManyPrints {
    // NOTE: do not change the method definition, e.g. add parameters to method
    public static void printText() {
        System.out.println("In the beginning there were the swamp, the hoe and Java");
        // Write your code here
    }

    public static void main(String[] args) {
        // ask the user how many times the text should be printed
        // use the while structure to call the printText method several times
        Scanner reader = new Scanner(System.in);
        int num = 0;
        System.out.println("How many? ");
        int times = Integer.parseInt(reader.nextLine());
        
        
        while(num <= times) {
            printText();
            num++;
        }
    }
}

On the first try, I didn't even have the increment operator in my code, and it would just constantly print my code. Then I thought of this, because we have been doing it a lot, and it worked. However, I don't understand why. Why does the increment operator, make the program able to stop after the number in the user input? Why doesn't it just keep increasing the value by 1?
Sure while num is less or equal to times that part of the code runs, but if i remove the increment operator, it would be infinite.

Thank you.

๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Need help understanding this while loop - Python Help - Discussions on Python.org
February 13, 2024 - Iโ€™m new to while loops, and I donโ€™t quite get whatโ€™s going on in this code from my book: current_number = 1 while current_number <= 5: print(current_number) current_number += 1 After just watching a video on Yoโ€ฆ
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ why-would-you-increment-a-variable-inside-the-condition-portion-of-a-while-loop-and-not-in-the-statement
Why would you increment a variable inside the condition portion of a while loop and not in the statement? (Example) | Treehouse Community
June 19, 2020 - The content of the "while" clause still constitutes an expression, it just happens that the evaluation of this expression causes the $year variable to be incremented. And this style of increment is performed ยท before the comparison, which is not something that could be accomplished inside the loop body.