Difference Between While And Do While In Javascript
__main__ Python module |
array Python module |
Arrays |
base64 Python module |
calendar Python module |
code Python module |
COM PHP module |
dis Python module |
Ev PHP module |
exp |
FFI PHP module |
http Python module |
imp Python module |
io Python module |
JavaScript |
Loops |
Lua PHP module |
operator Python module |
os Python module |
Other Programming |
PS PHP module |
Python functions |
re Python module |
ssl Python module |
StackOverflow |
stat Python module |
struct Python module |
test Python module |
time Python module |
UI PHP module
Michael Zippo
04.11.2021
👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
In programming, there are often times when you have a repetitive task that you want to do multiple times. Loops are used to automate these repetitive tasks and allow you to create more efficient code.
while
and do ... while
loops in Java, they are used to execute a block of code as long as a specific condition is met. These loops are similar to if
conditional statements, which are blocks of code that are only executed if a specific condition evaluates to true. Unlike a if
statement, however, while
loops are executed until a condition is no longer true.
This tutorial will discuss the basics of while
and do ... while
Java instructions and some examples will be presented to demonstrate these instructions in a Java program .
Java While
while
Loops through a block of code until a specified condition is true. The syntax of the while
loop is similar to that of a so traditional
statement. Here is the Java syntax while
loop:
while
will test the expression, in parentheses. If the boolean expression evaluates to true, the body of the loop will be executed, the expression will be evaluated again. The program will continue this process until the expression evaluates to false, after which the while
is canceled and the rest of the program will be executed.
Let’s illustrate an example to show how the while
loop can be used in Java. Let’s say we are a carpenter and we decided to start selling a new table in our shop. We only have the option of creating five tables, after which people who want a table will be placed on a waiting list.
We want to create a calendar that tells us how many people can order a table in advance. to put them on the waiting list. We could do this using a while
loop like this which will run the body of the loop until the number of commands passed is below the limit:
Our code returns:
Downtime break our code. In the first line we declare a variable called limit
which keeps track of the maximum number of tables we can create. Next, we declare a variable called orders_made
which stores the number of orders placed
Our program runs then a while
loop, which is executed while orders_made
is less than limit
. So, we use the orders_made ++
increment operator to add 1 to orders_made
.
After the increment operator has been executed, our program calculates the remaining capacity of the tables by subtracting "orders_made
from limit
. Then print the message [capacity] you can sort multiple tables.
on the console.
Our "while" statement stops working when orders_made
is greater than limit
.
Java Do While
do ... while
loop is a type of then
loop do .. while loop
executes the block of code in do once before checking if a state ion evaluates to true. Then the program will repeat the loop as long as the condition is true.
The difference between while
and do ... while
loop is that while
loops evaluate state before execute the code in the while
block, while do ... while evaluate the state after. by executing the code in the do
block
the syntax of the do ... while loop is as follows:
we are using a example to explain how to do it ... everything works in a loop.
Suppose we create a puzzle that asks a user to guess a number between one and ten. We want our user to first be prompted for a number before checking to see if they have guessed the correct number. If the user enters the wrong number, they should be promoted to try again. We could do this using a do ... while loop.
Here is an example of a program that asks a user to guess a number, then evaluates whether the user has guessed the correct number using a do ... loop:
When we run our code, we are asked to guess the number prime , before the condition in our do ... while
cycle is evaluated. Here’s what happens when you try to guess a few digits before finally guessing the correct one:
Pause of Descended our code. First, we import util.Scanner
, which is used to collect user input. Next, we define a class called GuessingGame
in which our code exists
we then define two variables: a name number
which stores the number to guess, and another name guess
which stores the users guess.
Next, we use the scanner method to start user input . We print the message Enter a number between 1 and 10:
on the console, then use the input.nextInt ()
to retrieve the number that the user entered < br>.
After this code has been executed, the do ... while loop evaluates whether the number the user guessed equals the number the user must guess. If the user guessed the wrong number, the contents of the do
loop are rerun ; if the user guessed the correct number, the do ... while loop stops execution and the message You are right!
is printed on the console.
In completed loops
Infinite loops are loops that will keep working forever. If you have a while
loop whose declaration does not return false, the loop will continue and may crash your program. Therefore, it is important to ensure that at some point the While Loop stops working
Here is an example of an infinite loop in Java.
This loop will run endlessly. However, we can stop our program using the pause
statement. Let’s say we are creating a program that keeps track of the number of tables available. We only have five paintings in stock. When there are no tables in stock, we want our while
loop to stop. We could create a program that meets these specifications using the following code: