Why does my Loop skip control flow?

The second loop wouldn't stop as if it passed an if, so I changed the location of the y variable, it should be inside the first loop and this code would work. I just can't imagine why this happens, in the order of the first loop then the second loop, because the control flow doesn't work then the second loop becomes an unlimited process and the first loop will never finish.

Is it because the y variable is too far away (wrapped in loop with loop) so that the control flow cannot check the value of Y how much?

fn main (){
   let mut x: i32 = 0;
   let mut y =  5;


   'first_loop: loop {

      println!("x = {x}");
      x += 1;

   'second_loop: loop {
      println!("y = {y}");
      y -= 1;
      if y == 2 {break 'second_loop}
      if x == 10 {break 'first_loop}

   }
   }
}

If you declare a variable with let inside a loop, it will be reinitialized in each iteration of the loop. Does that explain the behavior?

Add another print statement after the if condition for y's value and rerun it yourself:

fn main (){
   let mut x: i32 = 0;
   let mut y =  5;


   'first_loop: loop {

      println!("x = {x}");
      x += 1;

   'second_loop: loop {
      println!("y = {y}");
      y -= 1;
      if y == 2 {break 'second_loop}
      println!("y down here = {y}");
      if x == 10 {break 'first_loop}

   }
   }
}

Let us know if you still don't understand what's happening after that :wink:.

I tried by changing other things so that the code became:

fn main (){
   let mut x: i32 = 0;
   let mut y =  5;


   'first_loop: loop {

      println!("x = {x}");
      x += 1;

   'second_loop: loop {
      println!("y = {y}");
      if y == 2 {break 'second_loop}
      y -= 1;

   }
   if x == 10 {break 'first_loop}

   }
}

This is a successful run, even more confused

In this case let is outside the loop so there is no initialization?

Yes. But I probably don't understand what you're really asking.

Actually, my question is why the loop in the code is unlimited, even though I have stopped with break.

In which version of the code? Please show the code where it loops forever.

Code in the OP indeed loops forever, since it is stuck in the inner loop without incrementing x and therefore never hits if x == 10 condition.

This code is lead to forever loop behaviour

fn main (){
   let mut x: i32 = 0;
   let mut y =  5;


   'first_loop: loop {

      println!("x = {x}");
      x += 1;

   'second_loop: loop {
      println!("y = {y}");
      y -= 1;
      if y == 2 {break 'second_loop}
      if x == 10 {break 'first_loop}

   }
   }
}

Right. I couldn't understand which version of the code they were asking about it.

Here, try running this version. Maybe this will help you:

fn main() {
    let mut x: i32 = 0;
    let mut y = 5;

    'first_loop: loop {
        x += 1;

        'second_loop: loop {
            y -= 1;
            println!("y = {y}");
            if y == 2 {
                println!("breaking out of second loop");
                break 'second_loop;
            }
            println!("x = {x}");
            if x == 10 {
                println!("breaking out of first loop");
                break 'first_loop;
            }
        }
    }
}

Consider, what would happen if you tested for y <= 2 instead?

I would also recommend starting up a debugger and walking through the program step by step. Looks like you're using visual studio code, it should have an extension you can download for that.

Whenever you face a problem you don't understand, try approaching it in small increments. The more things you change, thre less chances you'll have to narrow down what was wrong to begin with.

Oh also, if you run this version without a debugger, stop running it quickly and scroll back to the top of the terminal output. It's important to see the printed lines at the beginning to see what happens.