C# Lesson 6
Matt Bauer 1/13/2020
While loops loop for a while.
The while statement is simple loop. As long as the condition inside the while() evaluates to true, the section of code will keep repeating itself.
using System;
namespace Lesson6
{
class WhileLoops
{
static void Main(string[] args)
{
//if (true)
//{
// Console.WriteLine("The IF is true");
//}
//while (true)
//{
// Console.WriteLine("The WHILE is true");
//}
//int numberOfLoops = 10;
//while (numberOfLoops > 0)
//{
// Console.WriteLine("numberOfLoops = " + numberOfLoops);
// numberOfLoops = numberOfLoops - 1;
//}
string userInput;
do
{
Console.WriteLine();
Console.WriteLine("1: Cows go");
Console.WriteLine("2: Cats go");
Console.WriteLine("3: EXIT");
Console.Write("Choose an action: ");
userInput = Console.ReadLine();
if (userInput == "1")
{
Console.WriteLine("Moo");
}
if (userInput == "2")
{
Console.WriteLine("Meow");
}
} while (userInput != "3");
}
}
}
/*
* TASK:
* Make a while loop that prints 10-20.
* Print '**********' to the console.
* Make a while loop that prints all the even numbers from 2 - 20
* Print '**********' to the console.
* Make a while loop that prints the powers of 2 from 1 - 256
*
* Example output:
* 10
* 11
* 12
* 13
* 14
* 15
* 16
* 17
* 18
* 19
* 20
* **********
* 2
* 4
* 6
* 8
* 10
* 12
* 14
* 16
* 18
* 20
* **********
* 1
* 2
* 4
* 8
* 16
* 32
* 64
* 128
* 256
*
* BONUS POINTS:
* Research how to use Console.Write() and use it to print the output on a single line each
* with spaces between the numbers.
*
* Example Output:
* 10 11 12 13 14 15 16 17 18 19 20
* **********
* 2 4 6 8 10 12 14 16 18 20
* **********
* 1 2 4 8 16 32 64 128 256
*
*/
using System;
namespace Lesson6Task
{
class WhileTask
{
static void Main(string[] args)
{
}
}
}