Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Loops in C# - Prof. Briana Morrison, Study notes of Computer Science

The concept of loops in c#, including the four types of loops (for, while, do-while, and foreach) and their characteristics. It also provides examples of how to use loops in various scenarios, such as writing sentences on the board or counting from 1 to 1000. The document also covers initialization, test, and update in loops.

Typology: Study notes

Pre 2010

Uploaded on 08/03/2009

koofers-user-cpj-2
koofers-user-cpj-2 🇺🇸

10 documents

1 / 20

Toggle sidebar

Related documents


Partial preview of the text

Download Understanding Loops in C# - Prof. Briana Morrison and more Study notes Computer Science in PDF only on Docsity! CSE 1301 Lecture 6A Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison CSE 1301 6A-2 Overview • Iteration • 4 kinds of loops – for – while – do while – foreach • Infinite Loops CSE 1301 6A-3 Looping • In computing, we often need to perform the same operations on multiple items. • Typically, these tasks follow this pattern: – initialize values (set total to 0) – process items one at a time (add price to total) – report results (report total) The flow of control that programmers use to complete jobs with this pattern is called looping, or repetition. CSE 1301 6A-4 Iteration • One thing that computers do well is repeat commands • Programmers use loops to accomplish this • 4 kinds of loops in C# – for loop – while loop – do while loop – foreach loop CSE 1301 6A-5 Criteria for loops 1.Usually have some initial condition • Starting a counter • Beginning in a certain state 2.Must have a test to continue 3.Must make progress towards finishing CSE 1301 6A-6 Loops in Everyday Life • Bad children are told to write sentences on the board “I will not pour Clorox in the fish tank” • Have to write this sentence either – A certain number of times – Until the teacher is happy – As many as you can during break CSE 1301 6A-7 The for Loop • Ideal when you know the number of iterations to perform before the loop begins • Examples: – Find the sum of 5 numbers – Find the maximum of 20 numbers – Print the odd numbers from 1 to 10 CSE 1301 6A-8 The for loop • Good when you know exactly how many times you need to execute something • Has format: for (<initialization>; <test to continue>; <increment>) { // everything in here is what is repeated // over and over again } • Initialization is where the counter is given a starting value • The test determines whether or not to continue • The increment can be any amount, including negative, and occurs after the loop statements execute CSE 1301 6A-9 More for Loop Syntax for ( initialization; loop condition; loop update ) { // loop body } Notes: • semicolons separate terms in the loop header • no semicolon follows the loop header • curly braces are required only if more than one statement is in the loop body CSE 1301 6A-10 for Loop Flow of Control CSE 1301 6A-11 for Loop Flow of Control 1. The initialization statement is executed (once only). 2. The loop condition is evaluated. If the condition is true, the loop body is executed. 3. The loop update statement is executed, and the loop condition is reevaluated (#2). • And so on, until the condition is false. CSE 1301 6A-12 Satisfying the Teacher • Example: 1000 sentences? No problem… int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (“I will not pour Clorox…”); } // Remember, counter++ is the same as // counter = counter + 1 CSE 1301 6A-25 Why this works int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } counter 999true CSE 1301 6A-26 Why this works int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } Output: 999 I will not pour Clorox in the Fish Tank counter 999 CSE 1301 6A-27 Why this works int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } counter 1000 CSE 1301 6A-28 Why this works int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } counter 1000 true for last time CSE 1301 6A-29 Why this works (are we finished?) int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } Output: 1000 I will not pour Clorox in the Fish Tank counter 1000 CSE 1301 6A-30 Why this works int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } counter 1001 CSE 1301 6A-31 Why this works int counter; for (counter = 1; counter <= 1000; counter++) { Console.WriteLine (counter + “I will not pour…”); } // Jump down here and continue … … counter 1001 false CSE 1301 6A-32 Final Output 1 I will not pour Clorox in the fish tank. 2 I will not pour Clorox in the fish tank. 3 I will not pour Clorox in the fish tank. 4 I will not pour Clorox in the fish tank. . . . 999 I will not pour Clorox in the fish tank. 1000 I will not pour Clorox in the fish tank. CSE 1301 6A-33 Another Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { C.O.Wln(num + “Potato”); } CSE 1301 6A-34 num int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); OUTPUT ? CSE 1301 6A-35 num OUTPUT 1 int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); CSE 1301 6A-36 int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); num OUTPUT 1 true CSE 1301 6A-37 num int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); OUTPUT 1 1Potato CSE 1301 6A-38 num OUTPUT 2 int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); 1Potato CSE 1301 6A-39 int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); num OUTPUT 2 true 1Potato CSE 1301 6A-40 num int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); OUTPUT 2 1Potato 2Potato CSE 1301 6A-41 num OUTPUT 3 int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); 1Potato 2Potato CSE 1301 6A-42 int num; for ( num = 1 ; num <= 3 ; num++ ) C.O.Wln(num+"Potato"); num OUTPUT 3 true 1Potato 2Potato CSE 1301 6A-55 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT count CSE 1301 6A-56 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT count 4 CSE 1301 6A-57 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT count 4 TRUE CSE 1301 6A-58 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 count 4 CSE 1301 6A-59 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 count 3 CSE 1301 6A-60 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 count 3 TRUE CSE 1301 6A-61 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 count 3 CSE 1301 6A-62 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 count 2 CSE 1301 6A-63 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 count 2 TRUE CSE 1301 6A-64 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 2 count 2 CSE 1301 6A-65 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 2 count 1 CSE 1301 6A-66 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 2 count 1 TRUE CSE 1301 6A-67 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 2 1 count 1 CSE 1301 6A-68 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 2 1 count 0 CSE 1301 6A-69 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 2 1 count 0 FALSE CSE 1301 6A-70 Count-controlled Loop int count ; count = 4; while (count > 0) { C.O.Wln(count); count -- ; } C.O.Wln("Done"); OUTPUT 4 3 2 1 Done count 0 CSE 1301 6A-71 Example: Re-Writing 1-1000 (using a while loop) int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); } counter 1 CSE 1301 6A-72 Example: Re-Writing 1-1000 (using a while loop) int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); } counter 1 CSE 1301 6A-85 Example: Re-Writing 1-1000 (using a while loop) int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 2 CSE 1301 6A-86 Problem Solved… int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 2 CSE 1301 6A-87 Problem Solved… int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } Output: 2 I will not pour Clorox in the fish tank counter 2 CSE 1301 6A-88 Problem Solved… int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 3 CSE 1301 6A-89 How does it end? int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 999 CSE 1301 6A-90 How does it end? int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 999 CSE 1301 6A-91 How does it end? int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 999 CSE 1301 6A-92 Problem Solved… int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } Output: 999 I will not pour Clorox in the fish tank counter 999 CSE 1301 6A-93 How does it end? int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 1000 CSE 1301 6A-94 How does it end? int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 1000 CSE 1301 6A-95 How does it end? int counter = 1; while (counter < 1000) { Console.WriteLine (counter + “I will not…”); counter++; } // So we never print out // 1000 I will not pour Clorox in the fishtank counter 1000now false CSE 1301 6A-96 Another Problem Solved int counter = 1; while (counter <= 1000) { Console.WriteLine (counter + “I will not…”); counter++; } counter 1000now true CSE 1301 6A-97 Example bool teacherHappy = false; int lineNumber = 1; while (!teacherHappy) { Console.WriteLine (lineNumber + “I will not…”); lineNumber++; teacherHappy = attitudeFunction ( ); } // assume attitudeFunction can change // teacherHappy CSE 1301 6A-98 The do-while loop • Similar to while loop • Must execute at least one time (test is at bottom) • Has format: do { }while (<boolean value>); CSE 1301 6A-99 Example (count from 1 to 3) int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); counter 0 CSE 1301 6A-100 Example (count from 1 to 3) int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); counter 0 CSE 1301 6A-101 Example (count from 1 to 3) int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); counter 1 CSE 1301 6A-102 Example (count from 1 to 3) int counter = 0; do { counter++; Console.WriteLine (counter); } while (counter < 3); counter 1 Output: 1
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved