Home > Net >  infinite for loops in c
infinite for loops in c

Time:11-11

I am playing around a little with for loops , tried the following code and got an infinite loop.

#include<iostream>

int main(){
       int i {0};
       bool condition = i < 5;
       for ( ; condition ; ){
              std::cout << "Hello World!" << std::endl;
              i  ;  
       }
}  

Can someone explain why ?

CodePudding user response:

bool condition = i < 5;

This line defines a variable named condition which has the value true from this line onwards.

It does not bind the expression from the right side, but only copies the result at the time of assignment.

What you intended is more complicated:

auto condition = [&i](){ return i < 5; };
for ( ; condition() ; )

Now condition is a function object which can be evaluated repeatedly.

The right hand of the assignment is called a lambda expression, and follows the form [capture scope](parameters){ body with return statement }.

In the capture scope, you can list variables either by value (without &) in which case they get copied once when the lambda is declared, or by reference (with leading &) in which case they don't get copied but the variable inside the lambda is a reference to the variable of the same name outside the lambda. There is also the short form [&] which captures all variables in the parent scope by reference, and [=] which captures everything by value.

auto can be used for brevity in combined declarations assignments, and automatically resolves the type of the variable from the right hand side.

The closest compatible type you could explicitly specify would be std::function<bool(void)> (generic container for functions with that signature), the actual type of that object is some internal type representing the naked lambda which you can't write explicitly. So if you can't know the exact type, and you don't want to use a generic container type, auto is occasionally even necessary.

CodePudding user response:

Variables in C store values. You seem to be under the impression that condition somehow remains connected to the expression i < 5. It is not. Once you set the value which is true at the time of the assignment it will keep that value until you change it. You never change it again so the value of condition is forever true.

CodePudding user response:

citing cppref:

Executes init-statement once, then executes statement and iteration-expression repeatedly, until the value of condition becomes false. The test takes place before each iteration.

Now, your condition evaluates to true:

bool condition = i < 5; // true

hence, the loop will continue until false, which does not happen in the loop body of your code; condition is not written to after initialization, and therefore the loop will not stop.

CodePudding user response:

The for loop runs on a condition, such as while i is below a specified value, and will keep running until this is no longer satisfied. In your case, it is never satisfied, guaranteeing an infinite loop.

  • Related