Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.

Slides:



Advertisements
Similar presentations
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Fundamental Programming Fundamental Programming for Loops.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
PHY 107 – Programming For Science. Today’s Goal  How to use while & do / while loops in code  Know & explain when use of either loop preferable  Understand.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
Lecture 7: Repeating a Known Number of Times
Chapter 2.2 Control Structures (Iteration)
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Structured Program Development in C
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Chapter 6: Repetition Statements
Lab5 PROGRAMMING 1 Loop chapter4.
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Looping and Repetition
Presentation transcript:

Programming with C# Iteration LECTURE 3

Summary of last lecture SequenceSelectionif and switch statementsCastingRandom

Recap Exercise What does the following program do?

What’s to come today? Principles of Repetition, Unconditional and Conditional In terms of For loopsWhile loopsDo … While loops

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

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

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

for Loop Syntax Overview Sets up the loop Terminates the loop if false Carried out at the end of every iteration

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.

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."); }

for Loop Example Trace for (int i = 1; i <= 5; i++){ Console.WriteLine(i * 2); } iConsolei <= 5 12True True 6-false This is a trace table that lists out the variables and condition checks at each iteration of the loop.

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();

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

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.

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); }

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

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

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.

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();

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

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.

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();

What did we cover today? For Loops Nested For Loops Increment/Decrement Operators While Loops Do…While Loops

What’s to come next time Classes Objects Constructors Access Modifiers