×

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration is known in advance. For loop in C# is same as it was In C and C++.
Syntax:
for(<initialization>;<condition>;<increment/decrement>)
{
//Code which is to be executed several number of times
}
C# for loop example:
using System;
public class ForLoopExample1
{
            public static void Main (string[] args)
            {
                        Console.WriteLine("Enter The number of which you want to print the table??");
                        int num=Convert.ToInt32(Console.ReadLine());
                        for(int i=1;i<=10;i++)
                        {
                                    Console.WriteLine(num +"X" +i +"=" +num*i);
                        }
            }          
}
Output
Enter The number of which you want to print the table??
6
6X1=6
6X2=12
6X3=18
6X4=24
6X5=30
6X6=36
6X7=42
6X8=48
6X9=54
6X10=60
C# nested for loop:

Two loops get executed in nested for loop named as inner loop and outer loop. The inner loop iterates n number of times for each outer loop iteration.

Syntax:
for(<initialization>;<condition>;<updation>)
{
            for(<initialization>;<condition>;<updation>)
            {
                        //some statements
                        //the loop iterates until the inner condition fulfills for every outer loop execution             }

C# Nested for loop Example:
using System;
public class Nested_For_Loop_Example2
{
            public static void Main (string[] args)
            {                     
            int row=Convert.ToInt32(Console.ReadLine());
            for(int i=1;i<=row;i++)
            {
                        for(int j=1;j<=i;j++)
                        {
                                    Console.Write("*");
                        }
                        Console.WriteLine();
            }
}
}
Output
Enter The Number of Rows
10
*
**
***
****
*****
******
*******
********
*********
**********
C# Infinite for loop: An infinite loop is the one which iterates infinite number of times. the for loop becomes an infinite loop if the condition provided is never going to be fulfilled.
C# infinite for loop Example: If we put two semicolons only in for loop then the loop will iterate till infinite times. this is the simplest way to create an infinite for loop.
using System;
public class InfiniteForLoopExample
{
            public static void Main (string[] args)
            {
            for(;;)
{
Console.writeLine("this is an infinite loop");
}
}
}

Related Topics

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.