This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

WHILE LOOPS IN C#
(1 vote, average: 5.00 out of 5)
Written by Christopher   
Thursday, 29 May 2008 16:04

INTRODUCTION

C# provides several mechanisms for flow control in a program.  The loops in C# allow you to execute a block of code repeatedly, until a certain condition is met. This condition can be the number of repetition, a variable taking a specified value or something completely different. The while loop is a pre-test loop and is used to execute a block of code for a number of times not known before the loop begins. The  do..while loop is a post-test loop and is used for the same purposes as the  while loop.

 

SYNTAX AND USE: WHILE LOOP

The syntax for a while loop is the following one:

While(condition)

                                Statement;

 

Usually, the while loop will check a Boolean variable and act according to its value. If the condition is true, the loop will execute again. If at a point the condition results to false the loop will not be executed. Take notice that if the value of the control variable changes to false inside the loop variable and then to true before the end of the loop, nothing will happen. The condition is evaluated just before an iteration is to begin, not during the execution of the loop statements. The following example illustrates a basic while loop:

static void Main(string[] args)

        {

            bool condition = true;

            while (condition)

            {

                //Execute som statements

                // assign new condition value:

                condition = checkCondition();

 

            }

 

        }

 

It is important that the condition value changes inside the loop. If the condition value can’t change inside the loop, it will never stop executing.

 

SYNTAX AND USE: DO…WHILE LOOP

This loop is the post-test version of the while loop. The difference of the do…while loop from the while loop is that the first will execute at least one time. This is due to the fact that the condition statement is evaluated after the first execution of the loop. The following snippet of code demonstrates the basic usage of the  do…while loop:

 

  static void Main(string[] args)

        {

            bool condition = true;

            do

            {

                AfunctionToBeCalledAtLeastOnce();

                condition = CheckCondition();

            }

            while (condition);

        }

In the previous example we have a function that must be called at least once. When the program reaches the  do…while loop it enters the loop and an iteration takes place. The condition statement is assigned a value. After the successful completion of the loop the condition statement is evaluated. If it is true a new iteration will take place. If not, the program will continue normally.

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options