In programming, while
and do-while
loops are control flow statements that allow a piece of code to be executed repeatedly based on a condition.
A while
loop is used to execute a block of code repeatedly while a specified condition is true. The condition is evaluated before each iteration of the loop. If the condition is true, the loop body is executed. If the condition is false, the loop is exited and the program continues with the next statement after the loop.
A do-while
loop is similar to a while
loop, but it guarantees that the loop body is executed at least once, even if the condition is false to begin with. The condition is evaluated after the first iteration of the loop. If the condition is true, the loop body is executed again. If the condition is false, the loop is exited and the program continues with the next statement after the loop.
public class Main {
public static void main(String[] args) {
int x = 0;
do {
System.out.println(x);
x++;
} while (x < 10);
/*int x = 0;
while (x < 10) {
System.out.println(x);
x++;
}*/
}
}
While loop
int x = 0;
while (x < 10) {
System.out.println(x);
x++;
}
This code initializes an integer variable x
to 0 and then enters a while
loop. The condition for the loop is x < 10
, which means the loop body will execute as long as the value of x
is less than 10.
Inside the loop body, the program prints the value of x
to the console using the System.out.println
statement, which outputs a new line after printing the value of x
.
The program then increments the value of x
by 1 using the x++
statement. This means that the value of x
will be increased by 1 on each iteration of the loop.
When x
reaches the value of 10, the condition x < 10
will no longer be true, and the loop will terminate. The program will have printed the values of x
from 0 to 9, each on a separate line.
Do..While Loop
int x = 0;
do {
System.out.println(x);
x++;
} while (x < 10);
This code initializes an integer variable x
to 0 and then enters a do-while
loop. The do-while
loop is similar to the while
loop, but it guarantees that the loop body will be executed at least once, even if the condition is false to begin with.
Inside the loop body, the program prints the value of x
to the console using the System.out.println
statement, which outputs a new line after printing the value of x
.
The program then increments the value of x
by 1 using the x++
statement. This means that the value of x
will be increased by 1 on each iteration of the loop.
After the loop body is executed, the program checks the condition x < 10
. If the condition is true, the program goes back to the beginning of the loop and executes the loop body again. If the condition is false, the loop terminates.
In this case, the loop will iterate 10 times because the value of x
starts at 0 and increases by 1 on each iteration, so the loop will terminate when x
reaches the value of 10. The program will have printed the values of x
from 0 to 9, each on a separate line.
Shorthand Notation
x++
is a shorthand notation for incrementing the value of the variable x
by 1. It is equivalent to writing x = x + 1
.
The ++
operator is known as the increment operator, and it is commonly used in programming languages to add 1 to a variable. The ++
operator can be used before or after the variable, but the behavior differs slightly.
When used before the variable, as in ++x
, the increment operator adds 1 to the value of x
before using it in the expression. So ++x
is equivalent to writing x = x + 1
before using the new value of x
in the expression.
When used after the variable, as in x++
, the increment operator adds 1 to the value of x
after using it in the expression. So x++
is equivalent to writing x = x + 1
after using the original value of x
in the expression.
In the code example provided, x++
is used to increment the value of x
by 1 on each iteration of the loop.
No comments:
Post a Comment