Skip to main content

Iteration Statement in C# Language


Objective

In this tutorial, we will show you how to use some type of loops in C# language to control the flow of program.

Let's go

In many cases you want to repeat a piece of code multiple times. When you want to repeat code, C# language provides you a number of iteration statements. Iteration statements can execute a block of code zero or more times. Each execution of code is a single iteration.

While statement

The while command is used to repeat a block of code as long as a condition is true. If the condition is false, then the while loop ends. The format of the while statement is as follows:
While (condition)
{
Block of statements
}
If the condition is always true, so the loop never ends. Let's see an example of while statement bellow to get more understanding:

  1. static void Main(string[] args)
  2. {
  3. //this code print the number from 1 to 10
  4. int i=1;
  5. while (i <= 10)
  6. {
  7. Console.WriteLine("This is number " + i);
  8. i = i +1;
  9. //you can write statements to do more works here
  10. }
  11. }

In above example, we just check whether or not the value of number “I” is between 1 and 10. If true, so we print its value. When the value is greater than 10, so the loop breaks.

Do statement

A while statement always checks the condition first before doing anything. But what is happened if you want to do a statement before checking condition. In this case, C# language supports you a do loop statement.
The format of the do statement is shown here:
Do
{
Block of statemetns
}while(condition);
Same as while statement, loop will never end if the condition is always true. Example bellows will show a code using do statement:

  1. do
  2. {
  3. Console.WriteLine("This is number " +i);
  4. i = i + 1;
  5. //you can write statements to do more works here
  6. } while (i <= 10);

In above code, the condition “i


For statement

As said above, in case of while and do statements, if the condition is always true, so the loop never ends. In some cases, the number of loop is infinite of definite. If the number of loop is definitive, so we might want to use for statement for a replacement.
The format of for statement is shown as follows:
For (condition)
{
Block of statements
}
If the condition is true the loop will run. Example bellow shows how to use for statement:

  1. for (int j = 1; j <= 10; j++)
  2. {
  3. Console.WriteLine("This is number " + i);
  4. //you can write statements to do more works here
  5. }

Here you can see that the condition is might be more complicated than the condition of while and do statement. In the condition, we have defined the number of loop (from 1 to 10) clearly.

Foreach statement

The foreach statement iterates in a way similar to the for statement. However, the foreach statement has a special purpose: It can loop through collections such as arrays. An example of foreach statement is shown here:

  1. int[] arr = { 1, 2,3,4,5,6,7,8,9,10 };
  2. foreach (int k in arr)
  3. {
  4. Console.WriteLine("This is number " + k);
  5. //you can write statements to do more works here
  6. }

Note that the array may be in many data types, not only the basic data type.


Breaking out of and continuing loop

C# language supports break and continue keyword for breaking out of or continuing the loop. For example, in case you want to stop the loop when condition match, so you can place break keyword. If you want ignore the remaining of statements and go back to the top when condition happens, uses continue keyword to control. See example bellows:

  1. Random rn = new Random();
  2. int ran_value = rn.Next(1, 100);
  3. while (true)
  4. {
  5. if (ran_value < 60)
  6. {
  7. Console.WriteLine("This is number " + ran_value);
  8. ran_value = rn.Next(1, 100);
  9. }
  10. else if (ran_value < 80)
  11. {
  12. continue;
  13. }
  14. else
  15. {
  16. break;
  17. }
  18. }

In above code, we assign condition of while statement is always true, and let checking of condition for block of statements in the loop. Here if random value is between 1 and 59 so we print its value. In case of the value is between 60 and 79, we ignore the remaining check and go to top (go to next loop). If the random value is between 80 and 100 so we break the loop and go outside the while loop.

Summary

In this tutorial we have seen some kinds of loop as bellow:

  • while statements
  • do statement
  • for/foreach statement
  • break and continue keyword
  • They are very important and very helpful when do with complicated program. Remember of how to use and apply to a specific case will give you the power to control the flow of program.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.