C# Lesson 7

You're familiar with "while" loops in C#, but but it's time to learn about "for" loops. Let's get loopy in the 7th lesson in our series about learning C#.

The For loop if the natural progression built off of the While loop. An iterator is used dictate the duration of the loop. These loops will be especially useful when we cover Lists and Arrays in a future lesson.
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 Lesson7
{
    class ForLoops
    {
        static void Main(string[] args)
        {

            //int x = 0;
            //x = x + 1; // x = 1
            //x++;       // x = 2
            //x++;       // x = 3
            //Console.WriteLine("x = " + x);
            //x--;       // x = 2
            //Console.WriteLine("x = " + x);

            //int y = 0;
            //y = y + 2;  // y = 2
            //y += 2;     // y = 4
            //Console.WriteLine("y = " + y);
            //y -= 3; // y = 1
            //Console.WriteLine("y = " + y);

            //  /=
            //  *= 


            // Modify the same loop instead of making new ones
            int a = 0;
            while (a < 10)
            {
                Console.Write("| a = " + a);

                a++;
            }

            Console.WriteLine();


            for (int b = 0; b < 10; b++)
            {
                Console.Write("| b = " + b);
            }

            Console.WriteLine();


            for (int c = 0; c < 20; c += 2)
            {
                Console.Write("| c = " + c);
            }

            Console.WriteLine();


            for (int d = 50; d >= 40; d--)
            {
                Console.Write("| d = " + d);
            }

        }
    }
}



/*
* TASK:
*   Make a for loop that prints 10-20.
*   Print '**********' to the console.
*   Make a for loop that prints all the even numbers from 2 - 20
*   Print '**********' to the console.
*   Make a for loop that prints the powers of 2 from 1 - 256
*   
*   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 Lesson7Task
{
    class ForLoopTask
    {
        static void Main(string[] args)
        {
        }
    }
}

Recommended posts

We have similar articles. Keep reading!

C# Lesson 1

Learn how to declare and use variables in the very first lesson from our series on learning C# for beginners.

C# Lesson 5

Run sections of your code only IF you want them to with the "if" statement in the 5th lesson from the series on learning C# for beginners.

C# Lesson 4

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

C# Lesson 12

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

Comments

Log in or sign up to leave a comment.