website/content/blog/do-while-other-lang.md

26 lines
744 B
Markdown
Raw Normal View History

2021-08-28 02:03:42 +00:00
---
2023-02-19 02:37:22 +00:00
date: 2021-08-28 01:50:02
2021-08-28 02:03:42 +00:00
draft: false
math: false
2023-01-05 19:04:45 +00:00
medium_enabled: true
2023-02-19 02:37:22 +00:00
medium_post_id: 5a9c792673f2
tags: []
title: Do-While Loop in Other Languages
2021-08-28 02:03:42 +00:00
---
Some languages like C, C++, and Java have a concept of a Do-While loop which normally look like the following:
```
do {
statements;
} while(condition);
```
This would ensure that your group of statements at least run once and then continue while the condition is still met. If you're used to that pattern, then it can be annoying when you switch to another language like Python and find that it doesn't exist. To replicate this behavior, its as simple as adding an extra variable.
```python
first_run = True
while condition or first_run:
first_run = False
statements
2023-02-19 02:37:22 +00:00
```