Do-While Loop
Every loops has a number pf the same basic elements in it.
1. A beginning
2. An important block of code that needs to be run.
3. A testing condition or statement that judges whether or not to exit the loop.
4. An end
A Do - While loop changes the recipe from the While loop slightly. In the beginning of the Do - While loop we start executing the loop body and then test to see if we should continue.
1. A beginning
2. An important block of code that needs to be run.
3. A testing condition or statement that judges whether or not to exit the loop.
4. An end
A Do - While loop changes the recipe from the While loop slightly. In the beginning of the Do - While loop we start executing the loop body and then test to see if we should continue.
Flowchart of the Do - While loop
How it looks in code
using System;
class dowhileloopDemo
{
public static void Main()
{
int x = 21;
do
{
// The line will be printed even
// if the condition is false
Console.WriteLine("www.digitalfxtbook");
x++;
}
while (x < 20);
}
}
class dowhileloopDemo
{
public static void Main()
{
int x = 21;
do
{
// The line will be printed even
// if the condition is false
Console.WriteLine("www.digitalfxtbook");
x++;
}
while (x < 20);
}
}
Assignment
Create a program that:
1. Asks the user for a number from 1-100
2. The program then multiplies that all of the numbers below and including the number together to get a total
For example
Please input a number (user inputs 5)
the computer then multiplies 1 x 2 x 3 x 4 x 5 and gets a result of 120
3. it then prints out to the console
"If we multiply all of the numbers leading up to and including (the user inputted number) we will get a result of (result)"