C# Lesson 6

Learn about "while" loops, a way to make sections of code repeat, in our 6th lesson in the C# series for beginners.

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.
Afterwards you can continue to the next lesson!
Or go back and review the previous lesson.
We also have the source code on GitHub.


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)
        {
        }
    }
}


Recommended posts

We have similar articles. Keep reading!

C# Lesson 3

Learn how to do some basic math with C# in the 3rd lesson from our series for beginners learning C#.

C# Lesson 12

Lesson 12 in a our series about learning C# teaches you about working with methods.

Bubble Sort

Sample source code for implementing bubble sort in C# with an accompanying video explaining how to do it yourself.

C# Lesson 2

Learn how to read input from the user in the second lesson from our series on learning C# for beginners.

Comments

Log in or sign up to leave a comment.