Search

5.7 — For statements

By far, the most utilized looping statement in C++ is the for statement. The for statement (also called a for loop) is ideal when we know exactly how many times we need to iterate, because it lets us easily define, initialize, and change the value of loop variables after each iteration.

The for statement looks pretty simple in abstract:

for (init-statement; condition-expression; end-expression)
   statement

The easiest way to understand a for loop is to convert it into an equivalent while loop:

{ // note the block here
    init-statement;
    while (condition-expression)
    {
        statement;
        end-expression;
    }
} // variables defined inside the loop go out of scope here

The variables defined inside a for loop have a special kind of scope called loop scope. Variables with loop scope exist only within the loop, and are not accessible outside of it.

Evaluation of for statements

A for statement is evaluated in 3 parts:

1) The init-statement is evaluated. Typically, the init-statement consists of variable definitions and initialization. This statement is only evaluated once, when the loop is first executed.

2) The condition-expression is evaluated. If this evaluates to false, the loop terminates immediately. If this evaluates to true, the statement is executed.

3) After the statement is executed, the end-expression is evaluated. Typically, this expression is used to increment or decrement the variables declared in the init-statement. After the end-expression has been evaluated, the loop returns to step 2.

Let’s take a look at a sample for loop and discuss how it works:

First, we declare a loop variable named count, and assign it the value 0.

Second, count < 10 is evaluated, and since count is 0, 0 < 10 evaluates to true. Consequently, the statement executes, which prints 0.

Third, ++count is evaluated, which increments count to 1. Then the loop goes back to the second step.

Now, 1 < 10 is evaluated to true, so the loop iterates again. The statement prints 1, and count is incremented to 2.
2 < 10 evaluates to true, the statement prints 2, and count is incremented to 3. And so on.

Eventually, count is incremented to 10, 10 < 10 evaluates to false, and the loop exits.

Consequently, this program prints the result:

0 1 2 3 4 5 6 7 8 9

For loops can be hard for new programmers to read -- however, experienced programmers love them because they are a very compact way to do loops of this nature. For the sake of example, let's uncompact the above for loop by converting it into an equivalent while loop:

That doesn't look so bad, does it? Note that the outer braces are necessary here, because count goes out of scope when the loop ends.

More for loop examples

Here's an example of a for loop used to calculate an exponentiation of integers:

This function returns the value base^exponent (base to the exponent power).

This is a straightforward incrementing for loop, with count looping from 0 up to (but excluding) exponent.

If exponent is 0, the for loop will execute 0 times, and the function will return 1.
If exponent is 1, the for loop will execute 1 time, and the function will return 1 * base.
If exponent is 2, the for loop will execute 2 times, and the function will return 1 * base * base.

Although most for loops increment the loop variable by 1, we can decrement it as well:

This prints the result:

9 8 7 6 5 4 3 2 1 0

Alternately, we can change the value of our loop variable by more than 1 with each iteration:

This prints the result:

9 7 5 3 1

Off-by-one errors

One of the biggest problems that new programmers have with for loops (and other kinds of loops) is off-by-one errors. Off-by-one errors occur when the loop iterates one too many or one too few times. This generally happens because the wrong relational operator is used in the conditional-expression (eg. > instead of >=). These errors can be hard to track down because the compiler will not complain about them -- the program will run fine, but it will produce the wrong result.

When writing for loops, remember that the loop will execute as long as the conditional-expression is true. Generally it is a good idea to test your loops using known values to make sure that they work as expected. A good way to do this is to test your loop with known inputs that cause it to iterate 0, 1, and 2 times. If it works for those, it will likely work for any number of iterations.

Rule: Test your loops with known inputs that cause it to iterate 0, 1, and 2 times.

Omitted expressions

It is possible to write for loops that omit any or all of the expressions. For example, in the following example, we'll omit the init-statement and end-expression:

This for loop produces the result:

0 1 2 3 4 5 6 7 8 9

Rather than having the for loop do the initialization and incrementing, we've done it manually. We have done so purely for academic purposes in this example, but there are cases where not declaring a loop variable (because you already have one) or not incrementing it (because you're incrementing it some other way) are desired.

Although you do not see it very often, it is worth noting that the following example produces an infinite loop:

for (;;)
    statement;

The above example is equivalent to:

while (true)
    statement;

This might be a little unexpected, as you'd probably expect an omitted condition-expression to be treated as "false". However, the C++ standard explicitly (and inconsistently) defines that and omitted condition-expression in a for loop should be treated as "true".

We recommend avoiding this form of the for loop altogether and using while(true) instead.

Multiple declarations

Although for loops typically iterate over only one variable, sometimes for loops need to work with multiple variables. When this happens, the programmer can make use of the comma operator in order to assign (in the init-statement) or change (in the end-statement) the value of multiple variables:

This loop assigns values to two previously declared variables: iii to 0, and jjj to 9. It iterates iii over the range 0 to 9, and each iteration iii is incremented and jjj is decremented.

This program produces the result:

0 9
1 8
2 7
3 6
4 5
5 4
6 3
7 2
8 1
9 0

This is the only place in C++ where the comma operator typically gets used.

Note: More typically, we'd write the above loop as:

In this case, the comma in the init-statement is part of the variable definition syntax, not a use of the comma operator. But the effect is identical.

For loops in old code

In older versions of C++, variables defined as part of the init-statement did not get destroyed at the end of the loop. This meant that you could have something like this:

This use has been disallowed, but you may still see it in older code.

Nested for loops

Like other types of loops, for loops can be nested inside other loops. In the following example, we're nesting a for loop inside another for loop:

For each iteration of the outer loop, the inner loop runs in its entirety. Consequently, the output is:

a012
b012
c012
d012
e012

Here's some more detail on what's happening here. The outer loop runs first, and char c is initialized to 'a'. Then c <= 'e' is evaluated, which is true, so the loop body executes. Since c is set to 'a', this first prints 'a'. Next the inner loop executes entirely (which prints '0', '1', and '2'). Then a newline is printed. Now the outer loop body is finished, so the outer loop returns to the top, c is incremented to 'b', and the loop condition is re-evaluated. Since the loop condition is still true the next iteration of the outer loop begins. This prints ("b012\n"). And so on.

Conclusion

For statements are the most commonly used loop in the C++ language. Even though its syntax is typically a bit confusing to new programmers, you will see for loops so often that you will understand them in no time at all!

Quiz

1) Write a for loop that prints every even number from 0 to 20.

2) Write a function named sumTo() that takes an integer parameter named value, and returns the sum of all the numbers from 1 to value.

For example, sumTo(5) should return 15, which is 1 + 2 + 3 + 4 + 5.

Hint: Use a non-loop variable to accumulate the sum as you iterate from 1 to the input value, much like the pow() example above uses the total variable to accumulate the return value each iteration.

3) What's wrong with the following for loop?

Quiz solutions

1) Show Solution

2) Show Solution

3) Show Solution

5.8 -- Break and continue
Index
5.6 -- Do while statements

161 comments to 5.7 — For statements

  • Peter Baum

    In section "For loops in old code" - can use use static if you want that behavior?

    • nascardriver

      Hi Peter!

      No. The variable's scope is still limited to the for-loop. Even if it wasn't you'd run into problems when you tried to execute the same loop multiple times, because the variable wouldn't get re-initialized.

      Output

      • Peter Baum

        There are two issues:

        1. Is the scope limited for the iteration variable if defined within the FOR control structure?

        2. Does the CPP FOR loop prevent use of the control variable outside the FOR loop?  That was the issue in the lesson that caught my eye.

        They are, of course, related.  You were focused on 1. but I was thinking more about 2.  If we make the control variable global, there is no problem using the value after exiting the FOR:

        The value of i after exiting the loop is 3 in this example.

        • nascardriver

          I'm not sure I'm getting your questions right, you seem to have answered them yourself.
          Your code is perfectly legal (The comment is wrong). There's rarely a need for it though.

          • Peter Baum

            Yes, the comment was inadvertently left over from your code.  I am not able to edit it out at this point.  I also agree that there is rarely a need to use the main control variable after exiting the FOR.

            There appears to me to be a few strange wrinkles in C++ and I think I would have preferred to have static applied consistently to the variables in any block.

            • Alex

              > I also agree that there is rarely a need to use the main control variable after exiting the FOR.

              If you do find yourself with the need to do this, you're better off defining the loop variable prior to the for loop. That way, it will still be in scope after the for loop executes.

  • Peter Baum

    It would be instructive to explain why

    produces an infinite loop because it is not obvious to me from the definition that this would necessarily be the result.  I might have guessed instead that the conditional would be a null statement, something that might evaluate to zero or false by default and this would mean that the loop would never be executed.

  • Peter Baum

    The same problem occurs here as with the previous section regarding statements.  Perhaps the best solution to this problem is to have a section that explicitly defines statements.  See https://docs.microsoft.com/en-us/cpp/c-language/statements-c for the way this is usually done.  With this careful definition, a lot of the confusion I had completely disappeared.

    At this point I also wondered why the control structures are presented so late in the sequence.  One argument for introducing the concepts earlier is that it would allow the student to write much more interesting programs earlier and thus provide some nice motivation.

    • Alex

      Thanks for the feedback. Lesson updated.

      > At this point I also wondered why the control structures are presented so late in the sequence.

      Because in order to use control statements, you need to understand variables (chapter 2), operators (chapter 3), and scoping (chapter 4). I'd love to introduce loops earlier (for exactly the reason you suggest, programs get way more interesting when you learn them), but the only way to do that would be to interleave content from the chapters, which might be more confusing than not.

  • Matt

    Nice joke in quiz 3.  Ever considered using your talent

    ?

    My code for this chapter's quizzes:

    • nascardriver

      Hi Matt!

      You're using copy initializers in you for-loops, use uniform initializers.
      Quiz 2:
      You didn't write a 'sumTo' function, the behavior is correct though.
      You could've initialized @sum with 1 and started the loop at 2, because the lowest number the user is allowed to enter is 1. That's just performance boost, your solution works too.

      As always, great submission.

      • Matt

        Makes sense, I like uniform initialization best anyway - will do.  Clever suggestion for skipping the "trivial" case of @sum(1), I'll keep my eye out for opportunities to do this in the future!

        Thanks for your continued input!

  • Silviu

    Hello,

    In quiz number 3.

    I don't know why it doesn't print 9,8 to 0(if you change it to 1 will work). Because 0 it isn't either negative or positive... If you have a better explanation ?

    • Alex

      It does print 9, 8, .... 0, but then it wraps around and keep going, resulting in an infinite loop. Because count was declared as unsigned, the loop condition (count >= 0) is always true (count can never be negative) so the loop never terminates.

  • Would be great if you added a small subsection on the usage of ranged for loops. E.g:

    vector vi = {0,1,2,3,4};

    for(auto i : vi) {
    cout << i << " ";
    }

    • nascardriver

      Hi Siddharth!

      Those are covered in lesson 6.12a (For-each loops)

      • Thanks for pointing that out. Also, is there a slicing operator with C++11 or C++14. I haven't been able to find anything close to it. I'm wondering if there's an easy way of doing the following:

        Python:
        a = [1,2,3,4]
        b = a[1:3]
        c = a[:] #shallow copy of the list

        C++: ??

        • nascardriver

          For arrays, no, C++ doesn't offer anything to make working with arrays easier.
          Lesson 6.16 will introduce std::vector which has a constructor that can be used to slice another vector.

          Output
          32
          11

          If you want to slice a normal array you'll have to write a slice function on your own. Here's what I came up with (Using dynamically allocated arrays).

          Output
          2
          3

          So, I don't think there's a way of slicing arrays just now (lesson-wise). If you really need it you can read lesson 6.9a (Dynamically allocating arrays) or 6.16 (An introduction to std::vector) now. You seem to know the basics of programming from Python so you might be able to understand them already.

    • Alex

      I talk about ranged for loops in lesson 6.12a.

      I used to talk about them here, but it was confusing, since it's hard to show meaningful examples without having talked about arrays first.

  • Bouke285

    Small typo, just above the conclusion paragraph, it says:" c is incremented (to 'b')" that c should be a.

    Thanks for the lessons!!

    • nascardriver

      Hi Brouke!

      Nope, c is correct. 'a' is a constant, it cannot ever be changed. c on the other hand is a variable and can be increased.

      • Bouke285

        Haha! I thought he was still referencing the char and just referring to it instead of the variable. I did t even notice that c was a variable in the loop. My bad, thanks!

    • Alex

      I was talking about variable c, not value 'c'. I updated the lesson text to make this clear.

      Thanks for pointing out this ambiguity!

  • xjuggy

    Just out of curiosity (not sure if this was covered/asked before) but is there any significance to the choice of "++count" or "count++" when incrementing using for/while loops?

    I understand the difference between the two ; I was merely wondering if there is a reason to prefer one over the other for purely incrementing a counter.

    e.g.

    • nascardriver

      Hi xjuggy!

      If your compiler didn't optimize it ++i would be faster. But since your compiler does optimize it there's no difference. I still suggest you using ++i when it's purely to increment a counter.

    • Alex

      You should favor pre-increment over post-increment, all other things being equal. In simple cases, the compiler should be able to optimize the difference away, but in more complex expressions that may not be true.

      Personally, the only time I will generally use post-increment is when I want to increment a variable after using it in a single expression.

  • Cat

    Greetings Alex!
    I'm curious, for the second question, i seemed to set both my total and my count to 0. while i experienced the significance of the total being initialized as 1 made the total indeed off by 1, what is the significance of setting the count to 1 instead of 0? i tested several inputs to see if count being set to 0 would throw off the total, and have not found any conclusive difference. appreciate any insight into this 0 vs 1 ordeal haha

    • nascardriver

      Hi Cat! I like cats.
      I'm not quite sure if I understood your question correctly but I'll give it a shot.
      @total needs to be initialized as 0. If you initialized it as somethings else and passed 0 as an argument you'd expect to get 0 as a result but instead get what total was initialized to.

      We could change @count to be initialized as 0, but that wouldn't change anything, because

    • Alex

      Nascardriver answered well. But just to answer using different words:

      We want to sum all the numbers between 1 and value (inclusive), so the loop starts with count 1 and iterates as many times as necessary to add all numbers through value (inclusive).

      If we started at 0, that would make the loop iterate one additional time, adding 0 to the total, which doesn't impact the total, so it's pointless. 🙂

  • Shane

    For number 3 you also didn't use std:: before cout and didn't use the namespace. Do I get bonus points? 😀

  • Shane

    Alex I finally accomplished something without messing it up or overcomplicating it. I'm so happy right now.

  • SO I MADE THIS PROGRAM WHICH USES "WHILE" LOOPS TO DEMONSTRATE THE WORKING OF "FOR" LOOPS.
    ITS OBVIOUSLY NOT PERFECT. SO I REQUEST YOU TO TEST IT AND TELL ME IF IT WORKS IN EVERY WAY A "FOR" LOOP SHOULD WORK.

    ALSO, PLEASE TELL ME HOW IT COULD'VE BEEN BETTER AND MORE READABLE(I TRIED AS MUCH AS I COULD TO MAKE IT READABLE). IT WOULD BE VERY KIND OF YOU.

    #include<iostream>

    using namespace std;

    int ForLoop(double  i, double  j,double  k,char ch)
    {
        switch(ch)
        {
    /*case 1. for
    increasing order*/       case '+':
                               {
                                   while(i<=j)
                                   {
                                     cout<<i<<" ";
                                      i=i+k;
                                   }
                                    break;
                               }
    /*case 2. for
    decreasing order */      case '-':
                                {
                                   while(i>=j)
                                   {
                                      cout<<i<<" ";
                                      i-=k;
                                   }
                                      break;
                                }
    /*case 3. increment
    by multiplication
    */                        case '*':
                                 {
                                     while(i<=j)
                                     {
                                      if(k==1)
                                        {
                                           cout<<"your k is equal to one\ntherefore it will not update your \"initial value\"";
                                           exit(0);
                                        }
                                          else
                                         {  cout<<i<<" ";
                                              i*=k;
                                         }
                                     }
                                            break;
                                 }
    /*case 4. decrement
      by division*/             case '/':
                                  {
                                    while(i>=j)
                                     {
                                      if(k==0)
                                       {
                                        cout<<"your \"k\" is equal to zero\n\nso \"i\" cannot be updated";//because a number cannot be divide by zero
                                         exit(0);
                                       }
                                       else if(k==1)
                                        {
                                   cout<<"WARNING: k equals 1\nso i will not be updated"<<endl;//because number divided by one will result in same number
                                        exit(0);
                                        }
                                         else
                                          {
                                            cout<<i<<" ";
                                            i/=k;
                                          }
                                     }
                                       break;
                                    }
    /*case 5. default*/         default :
                                    cout<<"\nuh oh! the operator is invalid"<<endl;
        }
    }
    int main()
    {
        cout<<"enter the initial value"<<endl;
        double  initial_value;
        cin>>initial_value;

        cout<<"till what value the loop should execute?"<<endl;
        double  val;
        cin>>val;

        cout<<"enter the value by which the \"initial value\" should  be updated" <<endl;
        double  update;
        cin>>update;

        cout<<"enter the relation between \"i and k\""<<endl;
        char relation;
        cin>>relation;

         ForLoop(initial_value,val,update,relation);
    }
    REGARDS, ADITYA RAWAT

  • JimD

    Alex, re quiz question 2, I'm not seeing the need for the extra count variable. Before looking at the solution I wrote the following and it seems simpler (unless I'm missing something...).

  • JimD

    Hi Alex. A strange thing is happening while I'm trying out for loop examples from this chapter.

    My IDE is highlighting the word 'count' in green as if it's a keyword (google says it isn't a c++ keyword). It doesn't seem to be happening in your code, or anyone else's in the comments. Do you know what the reason for this is?

    The programmes are working fine, this is just bugging me because I don't know what's going on.

    I'm using Code::Blocks 13.12 in linux Mint.

    • Alex

      I don't have a clue on this one. Maybe that word is a keyword for some other language Code::Blocks supports? The internets says Code::Blocks supports Fortran, and Count is a function in Fortran...

      • JimD

        Maybe that's it, though you would think Code::Blocks would take account of the selected compiler before highlighting words. As I said, it isn't causing any functional problems, so just a niggle. 🙂

    • in code::blocks the variables that are mostly used by users  act as a keyword( but they are not keywords, they are just there to suggest you that you might want to use this variables, example count).

      they are just normal variables which may get  highlighted, although you still have to declare and initialize them( as normal variables).

  • Lucas F.

    Altough the point of quiz 2 is to illustrate a use for a for loop (pun not intended), as a side note I believe if I ever needed to calculate that sum I would use this, which is more direct and WAY faster for very large numbers (the largest input it handles without overflow is 6 074 000 999).

    At least a couple of people in the comments pointed this formula out, but they implemented it with double division with its accuracy problems, analogously to this:

    That is not needed because if x is a natural number then necessarily either x or x+1 is even!
    Sorry for being fussy 🙂

  • Nicolas

    Hello Alex,
    I thought I'd experiment around a little bit with structs and enums and spent a chunk of time writing this code:

    The code compiles fine but I get a runtime  error: "Debug error! .... " "abort() has been called". Any idea why and how to fix it?

  • Nicolas

    Hi Alex,
    Im a little confused about why in the nested loop example the nested loop executes 3 times before the newline but the actual loop only executes once before the newline. why doesnt the program print the following?

    a0
    b1
    c2
    d0
    e1

    • Alex

      The outer loop starts executing first, and c = 'a'. Then the code in the body of the loop runs. This first prints 'a'. Then the nested loop runs in its entirety. This prints "012". Only when the outer loop block finishes does the outer loop begin its second iteration (c = 'b'). The second iteration prints "b012". Then the third iteration of the outer loop runs. Etc...

      • Nicolas

        I think you mean "only when the inner loop block finishes", makes sense. Thanks a lot!

        • Alex

          Well, both actually. The inner loop block finishes all of its iterations, then the outer loop block finishes its single iteration, then the outer loop re-evaluates the loop condition.

  • sorry i just wanted to share this:
    the formula for the sum of the natural numbers
    is 0.5*n*(n+1)

  • Russell

    I wrote the following for question 2, but return acc; doesn't actually display anything - I have to cout acc. Am I doing something wrong?

    int sumTo(int value)
    {
            int acc = 0;
            for(int count = 1; count <= value; ++count)
            {
                    acc += count;
            }
            return acc;
    }

    int main()
    {
            std::cout << "Enter an integer: ";
            int value;
            std::cin >> value;

            sumTo(value);

            return 0;
    }

    • Alex

      You're so close. sumTo() is great, and it returns the value of variable acc back to main(). However, main is not doing anything with this value, so it's discarded.

      Change this:

      to:

      or

  • Grey

    thank you very much Mr Alex, After learning a couple of your tutorials up to here, i came out with this program.
    but please:
    1. i want a way that a user can input either Y or y, N or n to proceed.
    2. i want a way that, when the user select N/n, the program should ask him/her to re-select it or select 5 to exit it.

    please how do i do it. thank you once again and May GOD Blessed you.

    /*
    Calculating for temperature: conversations units between
    Degrees Celsius and Degrees Fahrenheit.
    */
    #include <iostream>

    using namespace std;

    int main()
    {
        int number;
        
        string ans;
        
        float c, f;
        
        cout << "Know your temperature scale!"
            << endl << endl;

        cout << "This program will help you to enter any temperature value to change from:"
            << endl << endl;

        cout << "1. Degrees Celsius to Degrees Fahrenheit."
            << endl << endl;

        cout << "2. Degrees Fahrenheit to Degrees Celsius."
            << endl << endl;

        cout << "Please enter [Y] to continue or [N] to exit this program:"
            << endl << endl;

        cin >> ans;

        cout << endl;
        if (ans == "y")
        {
            cout << "You want to know more!"
                << endl << endl;
                
            cout << "To change Degrees Celsius to Degrees Fahrenheit"
                << endl << endl;
                
            cout << "Please enter [1]"
                << endl << endl;
                
            cout << "To change Degrees Fahrenheit to Degrees Celsius"
                << endl << endl;
                
            cout << "Please enter [2]"
                << endl << endl;
                
            cin >> number;
            
            cout << endl;
            
            if (number == 1)
            {
                cout << "You are changing from Degrees Celsius to Degrees Fahrenheit!"
                    << endl << endl;
                    
                cout << "Please enter value in Degrees Celsius"
                    << endl << endl;
                    
                cin >> c;
                cout << endl;
                
                f = (1.8 * c) + 32;
                cout << "The current temperature " << c << " Degrees Celsius is equal to " << f << " Degrees Fahreheit"
                    << endl << endl;
                    
                cout << "THANK YOU!"
                    << endl << endl;
            }
            else if (number == 2)
            {
                cout << "You are changing from Degrees Fahreheit to Degrees Celsius!"
                    << endl << endl;
                    
                cout << "Please enter your value in Degrees Fahrenheit"
                    << endl << endl;
                    
                cin >> f;
                cout << endl;
                c = (f - 32) / 1.8;
                
                cout << "The current temperature is " << f << " Degrees Fahrenheit is equal to " << c << " Degrees Celsius"
                    << endl << endl;
                    
                cout << "THANK YOU!"
                    << endl << endl;
            }
        }
        else
            if (ans == "N", "n")
            {
                cout << "don't you want to know your temperature?"
                    << endl << endl;
                    
                cout << "THANK YOU!"
                    << endl << endl;
            }

        return 0;
    }

  • Nguyen

    Hi Alex,

    In the last example (Nested for loops), I don't understand how for statement is evaluated (line 5).

    First, we declare a loop variable named C, and assign it the value 'a'.

    Second, 'a' <= 'e' is evaluated.  How could it be true since 'a' & 'e' are not integers?

    Third, ++C is evaluated.  This can be understood as C = 'a' + 1.  I don't understand how C = 'a' + 1 equals to 'b';

    Thanks,  Have a great day.

    • Alex

      'a' and 'e' are chars, and chars are a type of integer that are interpreted based on their ASCII codes. 'a' is integer 97, and 'e' is integer 101.

      ++c means "c = c + 1". If c is 97 ('a'), then c + 1 is 98 ('b').

      Make sense?

  • Avijit Pandey

    I did this one like this:

    I just wanted to know if its okay to do it like that or is it less efficient for further uses. i am asking this because you did it differetly.

  • ravi

    u haven't explained nested for loops..like the one used to print star patterns

    • Alex

      Nested for loops work just like other kinds of nested loops, which I cover in the lesson on "while statements". I've added an example here too, just to show how it's done with for loops.

  • Jonas

    I know this chapter covers for loops, but after I have wrote the solution with a loop I remembered Gauss. ^^

  • Ahmed Sohail

    1234567654321
    123456  54321
    12345    4321
    1234      321
    123        21
    12          1
    how to form such pattern

  • paltrow

    Duly noted! thank you very much : )

  • paltrow

    Thanks for the answer Alex.
    That means that I can't use assignments (single equals) in the condition-expression of a for loop. As a learning programming I was testing the for loop and I was confused because the compiler didn't warn me. The code just runs as it was ok.
    Thanks again for your very useful answer.

    • Alex

      You can use assignments in the condition-expression (as evidenced by the fact that the compiler lets you), but you generally won't need to. Do your assignments in the initialization or post-loop portions.

  • paltrow

    Hi Alex, thanks for your great site, I have a question about the following for loop.
    I can't understand why when the condition-expression is equal to 0 it doesn't print anything?

    It works fine for i=4 to i=1, but for i=0 it prints anything...

    Thanks!

  • Guido Bai G

    Dear Alex.

    Great tutorials.

    Is there any reason not to use unified declaration inside a for loop?:
    for(int i{0}, j{10}; i < 11; ++i, --j).  It works but I see you are not using it.
    Thanks and best regads.

    • Alex

      You can definitely use uniform initialization here. There's no reason not to, though it doesn't matter much with integer loop variables.

      I don't use it partly out of habit and partly because I find it a little harder to read when nested inside a for loop.

  • Simrit Singh

    Hi alex,
    c++ program to make sure that computer always wins the 21 matchsticks game,my code fails in certin cases where i win,please help me modify it ?

    #include<iostream>
    using namespace std;
    int main()
    {
    int num1,n=0;
    int i=21;
    for(i;i>=1; ){
    cout<<"Enter the number of matchsticks:"<<endl;
    cin>>n;
    i=i-n;
    if(n>4){
    cout<<"Invalid input"<<endl;
    continue;
    }
    num1=num1+1;
    if(num1>4){
    num1=num1-4;
    cout<<num1<<endl;
    i=i-num1;
    }
    if(i==4 || i==3 || i==2)
    num1=i-1;
    if(num1<=4){
    cout<<num1<<endl;
    i=i-num1;
    }
    if(i==1){
    cout<<"You Lost the Game"<<endl;
    break;
    }
    }
    return 0;
    }

Leave a Comment

Put all code inside code tags: [code]your code here[/code]