Lesson 5.4:

C# for Loop - with programming example


Objective

What is for loop in c#?
Syntax of for loop
Programming example

What is for loop in c#?

for loop is one of the most famous looping constructs among c# programmer. It is very useful when you exactly know how many times iteration is required. All the conditions as initialization, condition and increment/decrement is written in one block.

While(true) Loop

Structure:

 for ( init; condition; increment )
{
   statement(s);
}
	  

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace for_loop
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine(i + ". Hello For Loop");
            }
            Console.ReadKey();
        }
    }
}

		  

Output

1. Hello For Loop
2. Hello For Loop
3. Hello For Loop
4. Hello For Loop
5. Hello For Loop
6. Hello For Loop
7. Hello For Loop
8. Hello For Loop
9. Hello For Loop
10. Hello For Loop

__

Summary

for loop is very useful when you know the entire three conditions in advance. In this chapter you have learned for loop with programming example. In the next chapter you will learn about foreach loop.

BackNext