0% found this document useful (0 votes)
35 views

Using Loops: C# Programming

The document discusses different types of loops in C# programming including do-while, while, and for loops. It provides examples of when each loop type would be best to use and how to nest loops by putting one loop inside another. The document also includes an example of using nested for loops to print a square of asterisks of a given size.

Uploaded by

surafel getachew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Using Loops: C# Programming

The document discusses different types of loops in C# programming including do-while, while, and for loops. It provides examples of when each loop type would be best to use and how to nest loops by putting one loop inside another. The document also includes an example of using nested for loops to print a square of asterisks of a given size.

Uploaded by

surafel getachew
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Using Loops

C# Programming

©Rob Miles
What we can do so far
• We know there are three flavours of loops
do – while – put the test at the end
while – put the test at the start
for – create and update a control variable
• We also know that we can continue
(go round again) and break (leave the
loop)
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 2
Pick a Loop
• I want to ask a user for a film number and
reject values outside the range of 1-6
• What kind of loop?

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


Pick a Loop
• I want to ask a user for a film number and
reject values outside the range of 1-6
• What kind of loop?
• A do – while loop will work best

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


Pick a Loop
• I want to ask a user for a film number and
reject values outside the range of 1-6
• What kind of loop?
• A do – while loop will work best
• Why?

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


Pick a Loop
• I want to ask a user for a film number and
reject values outside the range of 1-6
• What kind of loop?
• A do – while loop will work best
• Why?
• Because this loop always performs the
statements in the loop at least once

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


Pick another Loop
• I want the user to enter a number, and
make my program print out the times
table for that number, up to 12
• What kind of loop?

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


Pick another Loop
• I want the user to enter a number, and
make my program print out the times
table for that number, up to 12
• What kind of loop?
• A For loop would work best for this

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


Pick another Loop
• I want the user to enter a number, and
make my program print out the times
table for that number, up to 12
• What kind of loop?
• A For loop would work best for this
• What would it look like?

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


Pick another Loop
• I want the user to enter a number, and
make my program print out the times
table for that number, up to 12
• What kind of loop?
• A For loop would work best for this
• What would it look like?
for ( i = 1; i < 13 ; i = i+1)

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


Final Loop
• I want to print out a square of * characters
on the screen
• The square should be 40 across and 10
down
• What kind of loop?

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


Final Loop
• I want to print out a square of * characters
on the screen
• The square should be 40 across and 10
down
• What kind of loop?
• This is a tricky one
• We need to put one loop inside another

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


Nesting Loops
• It is perfectly OK to put one loop inside
another
• Many programs do this
• In this case it might help if we rephrase
the problem
“Print out 10 lines, each containing 40
characters”

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


Two control variables
• If we have two loops we need two counters
int charNo;
int lineNo;

• One counts the lines, the other counts the


characters

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


The Outer Loop
• The Outer Loop repeats for each line
for (lineNo = 0; lineNo < 10; lineNo = lineNo + 1)
{
}

• We want 10 lines, and so the loop goes


round 10 times

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


The Inner Loop
• The Inner Loop repeats for each character
for (charNo = 0; charNo < 40; charNo = charNo + 1)
{
}

• We want 40 characters and so the loop


goes round 40 times

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


The Final Program
int charNo;
int lineNo;
for (lineNo = 0; lineNo < 10; lineNo = lineNo + 1)
{
for (charNo = 0; charNo < 40; charNo = charNo + 1)
{
Console.Write("*");
}
Console.WriteLine();
}

• This is the final program

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


The Final Program
int charNo;
int lineNo;
for (lineNo = 0; lineNo < 10; lineNo = lineNo + 1)
{
for (charNo = 0; charNo < 40; charNo = charNo + 1)
{
Console.Write("*");
}
Console.WriteLine();
}

• Create the counter variables

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


The Final Program
int charNo;
int lineNo;
for (lineNo = 0; lineNo < 10; lineNo = lineNo + 1)
{
for (charNo = 0; charNo < 40; charNo = charNo + 1)
{
Console.Write("*");
}
Console.WriteLine();
}

• This is the outer loop, it goes round once


for each line on the screen
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 19
The Final Program
int charNo;
int lineNo;
for (lineNo = 0; lineNo < 10; lineNo = lineNo + 1)
{
for (charNo = 0; charNo < 40; charNo = charNo + 1)
{
Console.Write("*");
}
Console.WriteLine();
}

• This is the inner loop


• It goes round once for each character
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 20
The Final Program
int charNo;
int lineNo;
for (lineNo = 0; lineNo < 10; lineNo = lineNo + 1)
{
for (charNo = 0; charNo < 40; charNo = charNo + 1)
{
Console.Write("*");
}
Console.WriteLine();
}

• This writes a single *


• It uses Write, so it doesn’t take a new line
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 21
The Final Program
int charNo;
int lineNo;
for (lineNo = 0; lineNo < 10; lineNo = lineNo + 1)
{
for (charNo = 0; charNo < 40; charNo = charNo + 1)
{
Console.Write("*");
}
Console.WriteLine();
}

• Once we have printed the * characters the


program must move on to the next line
Chapter 6.2 : Loops 11-Oct-13 ©Rob Miles 22
Final Output

• This is what the program produces

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


Changing the code
• To print more lines we need to change the
value 10 to a different one
• To print more characters we need to
change the value 40 to a different one

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


Summary
• You need to pick the loop that is
appropriate for the task in hand
– Decide where the test needs to go
– Decide if you are repeating something a
particular number of times
• You can put one loop inside another

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

You might also like