KEMBAR78
Creating Loops: C# Programming | PDF | Control Flow | Computer Science
0% found this document useful (0 votes)
57 views17 pages

Creating Loops: C# Programming

The document discusses different types of loops in C# programming including do-while loops, while loops, and for loops. It provides examples of how to write each type of loop and how to use break and continue statements to control loop execution. It also warns about ways to accidentally write infinite loops.

Uploaded by

surafel getachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views17 pages

Creating Loops: C# Programming

The document discusses different types of loops in C# programming including do-while loops, while loops, and for loops. It provides examples of how to write each type of loop and how to use break and continue statements to control loop execution. It also warns about ways to accidentally write infinite loops.

Uploaded by

surafel getachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Creating Loops

C# Programming

©Rob Miles
What we can do so far
• Store data (using variables)
• Change data (using assignments)
• Make decisions (using conditions)
• There is not much more that we need to
know how to do
– But we do need to know how to create loops

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 2


Loops
• We create a loop so that we can repeat one
or more statements
• A condition is used to determine whether
or not the loop stops
• The condition is either true or false, just
like that used in an if construction

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 3


A Stupid Loop
• We can write never ending loops if we like:
do
Console.WriteLine ("Har har");
while (true);

• This loop will never finish (use CTRL+C to


kill a program if it does this..

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 4


The do – while loop
• do-while continues while the condition is true

int i = 0;
do {
Console.WriteLine (i);
i = i + 1;
} while ( i < 4 );

• We can use a block to get more than one


statement repeated

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 5


Another Stupid Loop
• We can write “non loops” if we like:
do
Console.WriteLine ("Har har");
while (false);

• In this case the loop will not repeat, but it


will execute once as the test is at the end
• Remember that statements are executed in
sequence
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 6
Doing the test at the end of the loop

• In the do – while loop the test to see if the


loop continues is performed after the
statements in the loop have been
performed
• This is useful if you want the code to do
something and then check the result
– For example if you were reading numbers in
from a user..
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 7
Reading in Numbers

do {
Console.Write("Enter width:");
widthString = Console.ReadLine();
width = double.Parse(widthString);
} while ((width<0) || (width>3.0));

• This will repeatedly read the width value


until a valid one is entered
• Make sure you test with invalid values too

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 8


Doing the test first
• Sometimes you want to do the test before
you perform the loop code
• There is a C# construction for this too:
while (false)
Console.WriteLine("Never Printed");

• Note that the word do is not required


• Note that the statement could be a block

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 9


For loops
• We have already seen how we can create
code which will repeat something a
particular number of times
• However, since this is something that we
need to do a lot, C# provides a special
constructions for this, the for loop

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 10


The For loop
• The for loop has the following form:

for ( setup ; finish test ; update ) {


// things we want to do a given
// number of times
}

• The setup, finish test, and update are


added to get the loop that we want
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 11
A working For Loop
int i ;
for ( i = 1 ; i < 11 ; i = i+1 )
{
Console.WriteLine ( "Hello" ) ;
}

• This will print out Hello 10 times


• When the value in i reaches 11 the loop
stops
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 12
A stupid For Loop
int i ;
for ( i = 0 ; i < 11 ; i = i-1 )
{
Console.WriteLine ( "Hello" ) ;
}

• This will print out Hello for ever because


the control variable is updated in the
wrong direction

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 13


Another stupid For Loop
int i ;
for ( i = 0 ; i < 11 ; i = i+1 )
{
Console.WriteLine ( "Hello" ) ;
i = 0;
}

• This will print out Hello for ever because


the control variable is reset in the code
inside the loop

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 14


Breaking out
int i ;
for ( i = 0 ; i < 11 ; i = i+1 )
{
Console.WriteLine ( "Hello" ) ;
if ( i==3 ) break;
}

• The break keyword lets us escape from any loop


• You can use it in do-while, while and for loops

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 15


Continuing
int i ;
for ( i = 0 ; i < 11 ; i = i+1 )
{
Console.WriteLine ( "Hello" ) ;
if ( i==3 ) continue;
Console.WriteLine ( "Not 3" ) ;
}

• The continue keyword takes us back to the


"top" of any loop

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 16


Summary
• We now have the three fundamental loop
constructions
• The trick with programming is to use the
construction which is appropriate to the
task in hand
• You can make the code work with any loop
design

Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 17

You might also like