×

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# 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# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

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# 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# 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# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 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.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute 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.

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.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 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.

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# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.