Posted by : Sudhir Chekuri Thursday, 31 December 2015

C#.NET for loop is used to execute a code repeatedly until the condition is true. For loops are appropriate when you know exactly how many times you want to perform the statements within the loop. Normally, for loop statements execute from the opening curly brace to the closing curly brace without interruption. Every for loop can contain three sections commonly.
Those sections are
Initialization: Which sets the starting point of the loop.
Condition: Once the initializer list has been evaluated, the for loop gives control to its second section, ie.,the boolean expression. The code is executed until the condition satisfied ie.,until it returns true value.
Iteration(Increment/Decrement): Once Condition has been evaluated, the for loop gives to its control to third section ie.,Iteration, Which takes to the next side either positive or negative direction. Sometimes it will be incremented, sometimes decremented depending on the situation.

Syntax for C#.NET for loop

for(initialize; condition; inc/dec)
{
statements
}

Example program to print even numbers between 10 and 45 using for loop and if condition

Program

using System;
namespace For
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 10; i < 45; i++)
            {
                if (i % 2 == 0)
                { Console.WriteLine(i); }

            }
            Console.ReadKey();
        }
    }

}

Output

10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44

Example program using C#.NET for loop

Program

using System;

namespace loops
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < i + 1; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }

}

Output

*
**
***
****

C#.NET program to print stars in triangle format

Output required: Print a message to ask user to enter the number of rows based on which stars are printed.

In the first row there should be one star, In the second row 2 stars, In the third row 3 stars,... as shown in the below image.

Program
using System;

namespace PrintingStars
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter no. of rows");
            //taking no. of rows from user
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i <= n; i++)
            {
                for (int j = 0; j < i; j++)
                { Console.Write("*"); }
                //for moving to second row
                Console.WriteLine("");
            }
            Console.ReadKey();
        }
    }

}

Output

Enter no. of rows

4

*
**
***
****

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

Blog Archive

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -