>> ZG·Lingua >  >> Theoretical Linguistics >> Syntax

Can you use a semicolon before while?

No, you cannot use a semicolon before a `while` loop in most programming languages, including:

* C, C++, Java, Python, JavaScript, C#

The semicolon (;) is a statement terminator, and a `while` loop is a control flow statement. Putting a semicolon before a `while` loop would effectively end the statement before the loop begins.

This would lead to an infinite loop where the code inside the loop is never executed.

Here is an example of incorrect code:

```python

i = 0;

; while i < 5:

print(i)

i += 1

```

In this case, the semicolon after `i = 0` ends the statement. The loop will never start because the `while` condition is never evaluated.

Correct syntax:

```python

i = 0

while i < 5:

print(i)

i += 1

```

This code will execute the loop until the condition `i < 5` is false.

Copyright © www.zgghmh.com ZG·Lingua All rights reserved.