Skip to content

Fix do while continue stmt#160

Merged
nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
lucic71:do-while-continue
May 31, 2026
Merged

Fix do while continue stmt#160
nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
lucic71:do-while-continue

Conversation

@lucic71

@lucic71 lucic71 commented May 29, 2026

Copy link
Copy Markdown
Contributor

A continue statement inside a do while breakes the iteration and re-evaluates the condition.

Our previous translation, i.e.

loop {
  ...
  if !cond {
    break
  }
}

never re-evaluated the condition after a continue, resulting in an infinite loop.

To fix this, use while instead of loop. This ensures that the condition is re-evaluated right after the continue:

let mut __do_while = true;
while __do_while || cond {
  __do_while = false;
  ...
}

__do_while makes sure that the first iteration of the loop is always executed.

@nunoplopes nunoplopes merged commit c4b9533 into Cpp2Rust:master May 31, 2026
9 checks passed
lucic71 added a commit to lucic71/cpp2rust that referenced this pull request Jun 4, 2026
A continue statement inside a `do while` breakes the iteration and
re-evaluates the condition.

Our previous translation, i.e. 

```rs
loop {
  ...
  if !cond {
    break
  }
}
```

never re-evaluated the condition after a continue, resulting in an
infinite loop.

To fix this, use while instead of loop. This ensures that the condition
is re-evaluated right after the continue:

```rs
let mut __do_while = true;
while __do_while || cond {
  __do_while = false;
  ...
}
```

__do_while makes sure that the first iteration of the loop is always
executed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants