The Loops in C# - C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

Chapter 8. The Loops in C#

In some situations, you have to execute a code block several times. Entering the same statements repeatedly, however, is boring and time consuming. That means you should find a way to repeat blocks of codes quickly and easily. If you don’t know how to accomplish that, this chapter can help you greatly.

Important Note: Statements are generally executed in a sequential manner. That means C# executes the first statement first, followed by the next one, etc. You should remember this concept as you read this chapter.

Just like other programming languages, C# provides you with different control structures in terms of execution paths. That means you can access effective structures if you need to work on complex programs.

If you have to execute the same code blocks multiple times, you can streamline your task using a loop statement. The image below will show you the basic form of loop statements in C#:

The C# programming language offers different kinds of loops. You can use these loops for your own codes. Check the list below:

· While Loops– These loops repeat a statement or sets of statements if the specified condition becomes true. In general, while loops check the result of the condition before running the loop commands.

· For Loops– With these loops, you can execute a series of statements several times. Additionally, you can abbreviate the code that controls the loop’s variable.

· Do…While Loops– These loops are similar to while statements. However, they check the condition after executing the loop body.

· Nested Loops– You can use these loops within other loops.

Let’s discuss each loop type in detail:

The While Loops

These loops can repeatedly execute your desired statements if any of the given conditions is true. The image below shows the syntax of while loops in the C# language:

The For Loops

“For loops” are considered as a structure for controlling repetitions. You can use this structure to write statements that must be executed multiple times. Here’s the syntax you should follow when writing for loops in C#:

The Do…While Loops

These loops test the condition upon reaching the end of their body. Since they perform their functions first before checking the condition, do…while loops execute their body at least once. The image below shows the syntax of do…while loops in the C# language:

As you can see, the conditional expression is located at the last part of the syntax. That means the loop will execute the statement/s first before checking the condition entered by the programmer.

The Nested Loops

The C# programming language allows you to place loops inside other loops. That means you can combine different types of loops in your programs. The syntax you have to use depends on the loop that you want to use as the“container.” Check the following syntaxes:

· Nested For Loops:

· Nested While Loops:

· Nested Do…While Loops: