Syntax of for Loop

for(variable = static value ; condition ; increment / decrement)
{
        Statements
        .
        .
}


The code in for loop will be executed from the start value until the condition is met. The increment will be added continuously to the start value until the condition is met.

Example :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//wap to print first ten numbers on the screen
namespace CABasics
{

    class ForExample10
    {
        static void Main(string[] args)
        {
            Console.WriteLine("First 10 Numbers are");
            for (int i = 1; i <=10; i++)
            {

                Console.WriteLine(i);
               
            }
            Console.Read();
        }
    }
}



output:-




Example 2 :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//wap to print the multiplication table of a given number
namespace CABasics
{
    class ForExample12
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter any Number");
            int N = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("{0}*{1}={2}",N,i,N*i);
            }
            Console.Read();
        }
    }
}




 output:-







No comments:

Post a Comment