How and when can I use a do while? and how is its syntax [closed]

1

Good morning everyone, I'm learning to program and at the moment I'm better at handling the for cycle, but regarding the while I would like to understand it since I've seen several exercises that solve them with do, instead of just the while

    
asked by jorge 30.08.2017 в 18:19
source

5 answers

1

do while is a repetitive structure, which executes at least once its repetitive block, unlike the while or the for that they could 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 instructions in the do ... while loop are executed with the proviso that variable i is less than 10.

var s = "";

var i = 0;
do
{
    s +=  i + " ";
    i++;
} while (i < 10);

print (s);
// Output: 0 1 2 3 4 5 6 7 8 9
    
answered by 30.08.2017 / 18:34
source
2

Ok Good day the While handles when you want to repeat a piece of code zero or many times. For its part Do While forces the program to run the code snippet at least once.

Suppose a user is going to log into your system, and you use While to show the error every time you enter the incorrect data, but when you enter correct data it will not enter the loop. On the other hand with Do While you would have to enter the loop at least once.

//Do While
do{
  //Operación a realizar por lo menos 1 vez
}while(condicion); //la condición se evalúa al ultimo

//While
//la condición se evalúa primero
while(condicion){
  //Operación a realizar 0 o más veces
}
    
answered by 30.08.2017 в 18:29
2

FOR: The for loop executes an instruction or an instruction block repeatedly until a certain expression evaluates to false. The for loop is useful to iterate through arrays and to process sequentially. 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 an instruction or an instruction block repeatedly until a specified expression evaluates to false. In this cycle the instruction body is executed while a condition remains as true at the moment when the condition becomes false the cycle ends.

DO WHILE: Its basic difference with the while cycle is that the condition test is done at the end of the cycle, that is, the instructions are executed at least once because it first executes the instructions and at the end evaluates the condition; It is also known for this reason as exit condition cycle.

Example with DO WHILE

public class TestDoWhile 
{
    public static void Main () 
    {
        int x = 0;
        do 
        {
            Console.WriteLine(x);
            x++;
        } while (x < 5);
    }
}
/*
    Salida:
    0
    1
    2
    3
    4
*/

Example with WHILE

class WhileTest 
{
    static void Main() 
    {
        int n = 1;
        while (n < 6) 
        {
            Console.WriteLine("El valor actual de n es {0}", n);
            n++;
        }
    }
}
/*
    Output:
    El valor actual de n es 1
    El valor actual de n es 2
    El valor actual de n es 3
    El valor actual de n es 4
    El valor actual de n es 5
 */

There everything will depend on your context, I hope it serves you. Greetings

    
answered by 30.08.2017 в 18:30
2

The difference from do while to while is that the given condition is executed at least once, since it validates at the end whether the cycle should be repeated.

Simple example

With While validate at startup. You will enter the cycle only if the value is equal to no .

char valor = 'si';

while(valor == 'no')
{
    Console.WriteLine("Mensaje prueba");
}

With DoWhile validate at the end. Enter the cycle once and repeat the cycle if the value is equal to no .

char valor = 'si';

do
{
    Console.WriteLine("Mensaje prueba");
}
while(valor == 'no');
    
answered by 30.08.2017 в 18:42
1

basic structure of the do-While:

do//en este bloque de código se pone lo que deseas hacer
{
//lo que quieras hacer
}
while(-condicion-);//mientras tu condición se cumpla, se seguirá realizando lo que se encuentre dentro del bloque do

Example:

do
{
Console.WriteLine(numero);
numero += 1;
}
while (numero "<" 20);

The sample code will print numbers until the condition is met. (It should end in 20, when the condition is not met, and it would come out of the do-while). It should be noted that in this structure, the code that is in do Always runs , since it is evaluated at the end what the code must do, for this very reason, the example reaches the number 20, and not until 19, where in theory it should stop.

It can also be useful when you do not know how many times your cycle will be repeated.

do
{
Console.WriteLine("Desea continuar? Si/No");
respuesta = Console.ReadLine();
}
while (respuesta != "No")

As in this example, it will ask you if you want to continue, until you indicate no, and it is the main difference regarding the for, **do-While puede repetirse X veces que sea necesario.**

    
answered by 30.08.2017 в 18:31