Download presentation
Presentation is loading. Please wait.
Published byAugust Hood Modified over 9 years ago
1
Programming with C# Iteration LECTURE 3
2
Summary of last lecture SequenceSelectionif and switch statementsCastingRandom
3
Recap Exercise What does the following program do?
4
What’s to come today? Principles of Repetition, Unconditional and Conditional In terms of For loopsWhile loopsDo … While loops
5
Principles of Repetition Also know as iteration, it is the process of repeating a set of statements over and over We have loops which tell the computer to repeat a set of statements until a condition is met
6
Infinite Loops When using iteration we have to be wary of infinite loops occurring An infinite loop is when a sequence of instructions loop endlessly it will cause your program to have an error Often due to a loop having no terminating condition having a condition that can’t be met or one that cause the loop to start over
7
for loop For loops are used whenever the number of repetitions is known or can be calculated beforehand useful for traversing through a collection of data for loops can be especially powerful when used totraverse arrays. If the second expression of the for loopis always true then an infinite loop will occur
8
for Loop Syntax Overview Sets up the loop Terminates the loop if false Carried out at the end of every iteration
9
for Loop Example Explained for (int i = 1; i <= 5; i++){ Console.WriteLine(i); } The initialisation happens once before the loop starts. In this case, a variable i is set to 1 before the loop starts. The i <= 5 is the termination condition that is checked before each iteration of the loop. The variable i is incremented by 1 after each iteration of the loop. This loop will execute by printing out the numbers 1 through 5 each on a separate line.
10
for Loop Example for (int count = 0; count < 4; count++) { //Statement Executed 4 times Console.WriteLine("The for loop has done " + (count + 1) + " loops."); } Console.ReadKey(); Console.WriteLine(); int i; for (i = 0; i > -10; i-=2) { //Statement Executed 5 times Console.WriteLine("The for loop has done " + ((i-i+- (i/2))+1) + " loops."); }
11
for Loop Example Trace for (int i = 1; i <= 5; i++){ Console.WriteLine(i * 2); } iConsolei <= 5 12True 24 35 48 510True 6-false This is a trace table that lists out the variables and condition checks at each iteration of the loop.
12
for Loop Example int numOfChars = 0; string name1 = "Michael"; for (int j = 0; j < name1.Length; j++) { numOfChars++; } Console.WriteLine("The name " + name1 + " has " + numOfChars + " characters in it."); Console.ReadKey();
13
Increments/Decrements The operator ++ is known as the Increment operator. The increment operator increases the value of an integer variable by 1. int i = 0; i++; Console.Write(i); //1 The operator -- is known as the Decrement operator. The decrement operator decreases the value of an integer variable by 1. int y = 10; y--; Console.Write(y); //9
14
Increments/Decrements in Loops You may have noticed that the increment/decrement operators are used in loop. These operators specify after each execution of the loop, I want to perform a decrement or increment. Typically, loops are incremented/decremented in steps of 1. However you can also specify to increment/decrement at set sizes.
15
More For Loops //this loop counts from 10 down to 1 for(int i = 10; i > 0; i--){ Console.WriteLine(i); } //print odd numbers from 1 to 100 (achieve this by //going in steps of 2) for(int i = 1; i < 100; i = i + 2){ Console.WriteLine(i); }
16
Nested For Loops You can place a for loop inside another for loop. This is known as a nested for loop. The outer for loop only increments when the inner for loop is complete for that iteration. Can you work out what it does? for (int i=1; i<=5; i++) { for (int j=1; j<=i; j++) { Console.Write(i * j + " "); } Console.WriteLine(); } Outer for loop Inner for loop
17
while Loop A while loop’s statements will be executed as long as it’s condition is true It is known as a pre-test loop, i.e. the condition is checked before any statements are executed This means that the loop can be exited without any of it’s statements being executed The while Loop and the do … while loop are both very useful for validation If the condition is always true an infinite loop will occur
18
while Loop while(condition) { Console.WriteLine("condition is true"); } Console.WriteLine("condition is false"); Condition is checked before every loop. Condition has to be true for loop’s body to be executed.
19
while Loop Example Console.Write("Please enter your name:\t"); string name = Console.ReadLine(); while (name.Length 30) { Console.Write("Your name was not of a valid length, please enter it again:\t"); name = Console.ReadLine(); } Console.WriteLine(); Console.WriteLine("Welcome " + name + "!"); Console.ReadKey();
20
do … while Loop A do … while loop’s statements will be executedas long as it’s condition is trueBut all it’s statements will be executed at least once This kind of loop is known as a post test loop, as the condition check takes place after all the statements have been executed If the condition is always true an infinite loop will occur
21
do … while Loop do { Console.WriteLine("It is either the first pass or condition is true."); } while (condition); Console.WriteLine("There has been at least one pass and condition is false."); Condition is checked after every iteration. Condition has to be true for loop’s body to be executed after the first iteration.
22
do … while Loop Example int total = 0; do { Console.WriteLine("Type a number to add it to the total:\t"); total += Convert.ToInt32(Console.ReadLine()); Console.WriteLine("The total is :\t" + total + "\nIf you want to quit press 'c'" + "otherwise press any key."); } while (!Console.ReadLine().Equals("c")); Console.ReadKey();
23
What did we cover today? For Loops Nested For Loops Increment/Decrement Operators While Loops Do…While Loops
24
What’s to come next time Classes Objects Constructors Access Modifiers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.