Good morning everyone, I'm learning to program and at the moment I already manage the for loop better, but regarding the do while I would like to understand it since I have seen several exercises that solve them with do, instead of just the while
Good morning everyone, I'm learning to program and at the moment I already manage the for loop better, but regarding the do while I would like to understand it since I have seen several exercises that solve them with do, instead of just the while
Ok Good day, the While is used when you want to repeat a piece of code zero or many times. For its part, Do While forces the program to execute the code fragment at least once.
Suppose a user is going to log in to your system, and you use a While to show the error every time he enters the wrong data, but when he enters the correct data he will not enter the loop. On the other hand with Do While you would have to enter the loop at least once.
FOR: The for loop executes a statement or a block of statements repeatedly until a given expression evaluates to false. The for loop is useful for iterating through arrays and for sequential processing. Example In the following example the value of int i is written to the console and the value of i is incremented by 1 each time the loop is traversed. C#
WHILE: The while statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. In this loop the statement body is executed as long as a condition remains true at the time the condition becomes false the loop ends.
DO WHILE: Its basic difference with the while loop is that the condition test is done at the end of the loop, that is, the instructions are executed at least once because the instructions are executed first and the condition is evaluated at the end; It is also known for this reason as an exit condition loop.
Example with DO WHILE
Example with WHILE
Everything will depend on your context, I hope it helps you. Cheers
basic do-While structure:
Example:
The sample code will print numbers until the condition is not met. (should end in 20, when the condition is not met, and would exit do-while). It should be noted that in this structure, the code that is in
do
Always is executed , since what the code must do is evaluated at the end, for this reason, the example reaches number 20, and not until 19, where In theory it should stop.It can also be useful when you don't know how many times your cycle will repeat.
As in this example, it will ask you if you want to continue, until you say no, and it is the main difference with respect to the for,
**do-While puede repetirse X veces que sea necesario.**
The difference between do while and while is that the given condition is executed at least once, since it validates at the end if it should repeat the cycle.
simple example
With While validates at the beginning. It will enter the loop only if the value is equal to no .
With DoWhile it validates at the end. It will enter the loop once and repeat the loop if the value is equal to no .
do while is a repetitive structure, which executes its repetitive block at least once, unlike while or for which may not execute the block. This repetitive structure is used when we know in advance that the repetitive block will be executed at least once.
Example In the following example, the statements in the do...while loop are executed on the condition that the variable i is less than 10.