×

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# 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# 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# 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# 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# 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# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

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

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# 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# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

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

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# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute 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# 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# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

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