Showing posts with label loop. Show all posts
Showing posts with label loop. Show all posts

While loop in C#


//While loop in C#

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 1;
            while (x != 6)
            {
                Console.Write(x + " ");
                x++;
            }
        }
    }
}

For loop in C#


//For Loop in C#

using System;
using System.Windows.Forms;//To use MessageBox.Show

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 3; i++)
            {
                MessageBox.Show("Hi  "+i); //Displays the message in box. Works like Console.Write("Hi  "+i) (i.e. print command).
            }
        }
    }
}