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# Project 2

Test out your knowledge on basic C# programming with a challenging project making a hangman game. Covers lessons 7-12 from the beginner series.

C# Lesson 10

Lesson 10 in a our series about learning C# teaches you about working with arrays.

C# Lesson 9

Lesson 9 in our series about learning C# teaches you how to make Strings do your bidding.

C# Lesson 4

You truly need this lesson from our beginner series if you aren't familiar with using a Boolean in C#.

Comments

Log in or sign up to leave a comment.