C# - Looping in statements
We must learn how we can loop in statements. Now here we go.
Another essential technique when writing software is looping - the ability to repeat a block of code X times. In C#, they come in 4 different variants, and we will have a look at each one of them.
The while loop
The while loop is probably the most simple one, so we will start with that. The while loop simply executes a block of code as long as the condition you give it is true. A small example, and then some more explanation:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 0;
while(number < 5)
{
Console.WriteLine(number);
number = number + 1;
}
Console.ReadLine();
}
}
}
Try running the code. You will get a nice listing of numbers, from 0 to 4. The number is first defined as 0, and each time the code in the loop is executed, it's incremented by one. But why does it only get to 4, when the code says 5? For the condition to return true, the number has to be less than 5, which in this case means that the code which outputs the number is not reached once the number is equal to 5. This is because the condition of the while loop is evaluated before it enters the code block.
The do loop
The opposite is true for the do loop, which works like the while loop in other aspects through. The do loop evaluates the condition after the loop has executed, which makes sure that the code block is always executed at least once.
do
{
Console.WriteLine(number);
number = number + 1;
} while(number < 5);
The output is the same though - once the number is more than 5, the loop is exited.
The for loop
The for loop is a bit different. It's preferred when you know how many iterations you want, either because you know the exact amount of iterations, or because you have a variable containing the amount. Here is an example on the for loop.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 5;
for(int i = 0; i < number; i++)
Console.WriteLine(i);
Console.ReadLine();
}
}
}
This produces the exact same output, but as you can see, the for loop is a bit more compact. It consists of 3 parts - we initialize a variable for counting, set up a conditional statement to test it, and increment the counter (++ means the same as "variable = variable + 1"). The first part, where we define the i variable and set it to 0, is only executed once, before the loop starts. The last 2 parts are executed for each iteration of the loop. Each time, i is compared to our number variable - if i is smaller than number, the loop runs one more time. After that, i is increased by one. Try running the program, and afterwards, try changing the number variable to something bigger or smaller than 5. You will see the loop respond to the change.
The foreach loop
The last loop we will look at, is the foreach loop. It operates on collections of items, for instance arrays or other built-in list types. In our example we will use one of the simple lists, called an ArrayList. It works much like an array, but don't worry, we will look into it in a later chapter.
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add("John Doe");
list.Add("Jane Doe");
list.Add("Someone Else");
foreach(string name in list)
Console.WriteLine(name);
Console.ReadLine();
}
}
}
Okay, so we create an instance of an ArrayList, and then we add some string items to it. We use the foreach loop to run through each item, setting the name variable to the item we have reached each time. That way, we have a named variable to output. As you can see, we declare the name variable to be of the string type – you always need to tell the foreach loop which datatype you are expecting to pull out of the collection. In case you have a list of various types, you may use the object class instead of a specific class, to pull out each item as an object. When working with collections, you are very likely to be using the foreach loop most of the time, mainly because it’s simpler than any of the other loops for these kind of operations.
-
Regular Expressions in Perl for the Novice
| By Chrys | in Programming
In this article I explain the meaning of Regular Expressions and I introduce you to the concept in Perl. You start ...
-
PHP Regular Expressions
| By Chrys | in Web Development
In this article I explain the meaning of Regular Expressions and I introduce you to the concept in PHP. You start l...
-
How to write mathematics expression in HTML?
| By ori_sonata | in Programming
This article explain how to use HTML for writing mathematics expressions such as exponential, polynomial, fraction ...
-
The Golden Rule For How A Husband Should Express Love To His Wife
| By htlewis | in Marriage
A guide to the most important aspect of expressing love for your other half - you don't need to be married or be a ...
-
Regular Expression Patterns in Perl
| By Chrys | in Programming
In this part of the series, I begin explaining to you the concept of Patterns in Perl Regular Expressions....
-
XA Transaction - Solution for Transaction More Than One Database | By H4d1 | in Programming
Have you ever think that it's too difficult for making database transaction in two different places (or databases) ...
-
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , is integer , check Is Zero , Get Que | By xxris | in Programming
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , i...
-
How to access and use a Window's command line | By MaxwellPayne | in Programming
Learn about the Window's command line in DOS and how to use it....
-
How to Learn to Program Your Computer | By dsj8760 | in Programming
This article is about learning to program a computer. It is a general article giving tips on how to learn about pro...
-
Jailbroken iPhones get RickRolled | By explorer | in Programming
First iPhone worm, attacks via SSH and does the classic rick roll gag on the user....
-
C# - The switch statement | By Victorius | in Programming
Another important statement is the switch statement. Now we will learn it :)...
-
C# - The if statement | By Victorius | in Programming
Now we will learn the most important statement....
-
C# - Variables in higher level | By Victorius | in Programming
In this article i will teach you how you can use variables in most of your program....
-
C# - Variable types | By Victorius | in Programming
I will teach you what kind of variables can you use in C#...
-
C# - Hello world explained | By Victorius | in Programming
I will show you what's the life after Hello Word :)...








No comments yet.