Iteration Statements In C#
In C#, iteration statements are used to repeat a block of code several times or until a specific condition is met. The process of repeating a task repeatedly is called looping or iteration. There are four types of iteration statements in C#: For, Foreach, Do, and While
C# for statement
The for statement in C# is used to execute a block of code a specific number of times. It consists of three parts: the initialization statement, the condition expression, and the iterator statement.
The general syntax of a for the statement in C# is.
The initialization statement is executed once before the loop starts and is used to declare and initialize any variables used in the loop.
The condition expression is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. If the condition is false, the loop terminates.
The iterator statement is executed after each iteration of the loop and is used to modify any variables used in the condition expression.
C# for statement example
Here is an example of a for statement that loops through a block of code five times, and each time, it prints a string "The iteration number is" with the count value to the console. In this code, int i=1; is a declaration of a local variable that will participate throughout the loop.The i<=5; is the condition that decides how often iteration will continue.
Output. The output of the above program looks like this
The iteration number is 1
The iteration number is 2
The iteration number is 3
The iteration number is 4
The iteration number is 5
The iteration number is 6
The iteration number is 7
C# foreach statement
The foreach statement in C# is used to iterate over a collection of objects. It provides a simpler and more readable way to loop through the elements of a collection than using a for or while loop.
The general syntax of a foreach statement in C# is.
In this syntax, type is the type of the elements in the collection, the variable is a variable that refers to each element in turn, and collection is the collection of objects being iterated over.
C# foreach statement example
Here's an example of a foreach statement in C# that iterates over an array of strings and prints each element of the array. The string array daysofWeek has five elements.
Output. The output of the above program looks like this.
The Month is : January
The Month is : February
The Month is : March
The Month is : May
The Month is : June
The Month is : July
The foreach statement is recommended on a collection of objects.
C# while statement
The while statement in C# is used to execute a block of code as long as a specific condition is true. It evaluates the condition before each iteration of the loop.
The general syntax of a while statement in C# is:
In this syntax, the condition is the expression evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. If the condition is false, the loop terminates, and the program continues with the next statement after the loop.
The following flowchart diagram represents a while loop.
C# while statement example
Output. The output of the above program looks like this
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
C# do-while statement
The do-while statement in C# is used to execute a block of code at least once and as long as a specific condition is true. It evaluates the condition after each iteration of the loop.
The general syntax of a do-while statement in C# is:
In this syntax, the block of code inside the do statement is executed at least once, regardless of the value of the condition. After the block of code is executed, the while statement checks if the condition is true. If the condition is true, the loop repeats, and the block of code is executed again. If the condition is false, the loop terminates, and the program continues with the next statement after the loop.
C# do-while statement example
The following example executes the block of code with the do scope until the value ofnum is less than or equal to 6.
Output. The output of the above program looks like this:
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
Comments
Post a Comment