In programming, we have different types of requirements of logic to perform the block of statements in several repetition series at a certain required number of times. For resolving such types of requirements, we use loop, which has the mechanism to execute this in various types of conditions. In this article, we’ll understand the basic concepts of a loop in C#.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

How Loop Works in C#

In programming, for loop is a defined repetition of conditional logic of writing the definite number as per the certain times. The loop allows us to perform x number of prints concurrently in one line.

When you write the Loop, you should know how many times you want to declare it within a block of code.

Statement:

Below is the syntax of Loop declaration:

for (statement 1; statement 2; statement 3) 

{

  // code block to be completed

}

  • It defines statement 1 should perform before the execution of the code block. 
  • Statement 2 determines the condition for performing the code block.
  • Statement 3: to Execute (each time) after we have executed the code block.

The below code snippet is the example of printing the numbers 0 to 9:

using System;

namespace TestApplication

{

  class Program

  {

    static void Main(string[] args)

    {

      for (int j = 0; j < 10; j++) 

      {

        Console.WriteLine(j);

      }    

    }

  }

}

Output after executing the program:

C_Sharp_For_Loop_1.

  1. In for loop, a loop variable is used to handle the loop. 
  2. First, declare the loop variable to an appropriate value. 
  3. Check if this variable is smaller than or bigger than the counter value.
  4. If this step is true, then the loop body is executed and the loop variable gets changed. 
  5. Repeat these steps until the exit condition occurs.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Steps to Perform the for Loop

Step 1 

Initialization of Expression: Here, we define the loop counter to a specific value. 

For example: int a=1;

Step 2

Test Expression: Here, we need to test the condition as per the test cases basis on true and false. If it is true, then the code will execute on particular logic inside the body of the loop, and will perform as the logic; if it is false then it will go to else block logic.

For example: j <= 20;

Step 3

Update Expression: After the test expression executing loop body, this expression increments/decrements and changes the loop variable by a specific value. 

For example: j++;

Flowchart

C_Sharp_For_Loop_2

Multiple Expressions Inside a for Loop

Using multiple expressions, we can define the one to more initialization with "and, or" iterator statement inside a for loop.

Below is the code snippet to help you understand these scenarios:

  • We are initializing two variables a and b along with two iterator expressions. Accordingly, a and b will increment by 1 within the iteration.

using System;

namespace LoopExample

{

class TestForLoop

{

public static void Main(string[] args)

{

for (int a=0, b=0; a+b<=5; i++, j++)

{

Console.WriteLine("a = {0} and b = {1}", a,b);

}         

}

}

}

Output

C_Sharp_For_Loop_3.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

For Loop Without Initialization and Iterator Statements

From the above example, we have come to know about using multiple expressions and iterator statements. However, we can also use the loop without these statements and expressions when implementing.

In such situations, for loop performs as a while loop.

Below is the code snippet that will help you understand the while loop.

using System;

namespace TestApplication

{

  class Program

  {

    static void Main(string[] args)

    {

      int a = 0;

      while (a < 9) 

      {

        Console.WriteLine(a);

        a++;

      }

    }

  }

}

Output 

C_Sharp_For_Loop_4.

Infinite for Loop

Sometimes, we have the requirement of working on repeated activities in multiple events. In such scenarios, we can define the iteration using for loop in programming. 

In a certain condition, if we define the loop as true, then this loop will run as infinite.

Example:

using System;  

public class Example  

    {  

      public static void Main(string[] args)  

      {  

          for(char A='a'; x&lt;='f'; x++)

  {    

            Console.WriteLine(A);    

          }    

      }  

    }

Output 

C_Sharp_For_Loop_5

Free Course: Programming Fundamentals

Learn the Basics of ProgrammingEnroll Now
Free Course: Programming Fundamentals

Exit for the Loop

To exit the loop, we use a for loop mutually with the break statement.

Below is the snippet of code Here loop will exit when the variable j is equal to "8":

<script>

function myTestFunction() {

  var text = "";

  var i;

  for (i = 0; i < 10; i++) {

    if (i === 8) {

      break;

    }

    text += "Test number is " + i + "<br>";

  }

  document.getElementById("Test").innerHTML = text;

}

</script>

Output 

C_Sharp_For_Loop_6.

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

We hope this article helped you understand C# For loop. In this article, we discussed the concept of code structure of different types of for loop which will be helpful to professional developers from Java and .net backgrounds, application architectures, and other learners looking for information on JavaScript events. Besides exploring various professional courses offered by Simpleilearn, you can also sign up on our SkillUp platform, a Simplilearn initiative. The platform offers numerous free online courses to help with the basics of multiple programming languages, including JavaScript. You can also opt for our Full-Stack Web Development Certification Course to further enhance your career prospects.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.