Repeating Instructions

Slides:



Advertisements
Similar presentations
Section 2 - Selection and Repetition. Equality and Relational Operators.
Advertisements

Repeating Instructions
Repeating Instructions
Computer Science 1620 Loops.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
C# Programming: From Problem Analysis to Program Design1 Arrays C# Programming: From Problem Analysis to Program Design 3 rd Edition 7.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
C# Programming: From Problem Analysis to Program Design1 Repeating Instructions C# Programming: From Problem Analysis to Program Design 3rd Edition 6.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
CHAPTER SIX Loop Structures.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Advanced Programming LOOP.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Chapter 5: Control Structures II
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
JavaScript, Fourth Edition
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
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.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
C# Programming: From Problem Analysis to Program Design1 Repeating Instructions C# Programming: From Problem Analysis to Program Design 4th Edition 6.
C# Programming: From Problem Analysis to Program Design
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Control Structures II (Repetition)
Java Programming: Guided Learning with Early Objects
While Loops Chapter 3.
Chapter 5: Control Structures II
Repeating Instructions
Advanced Programming Lecture 02: Introduction to C# Apps
Chapter 3 – Introduction to C# Programming
Chapter 5 – Control Structures: Part 2
Presentation transcript:

Repeating Instructions 6 C# Programming: From Problem Analysis to Program Design 3rd Edition C# Programming: From Problem Analysis to Program Design

Chapter Objectives Learn why programs use loops Write counter-, state-, and sentinel-controlled while loops Examine the conditional expressions that make up a for loop Be introduced to the foreach looping structure C# Programming: From Problem Analysis to Program Design

Chapter Objectives (continued) Compare the do…while looping structure with the predefined forms of loops Write loops nested inside other loops Learn about keywords that can be used for unconditional transfer of control C# Programming: From Problem Analysis to Program Design

Chapter Objectives (continued) Be introduced to recursion and learn how recursive methods work Pick appropriate loop structures for different applications Work through a programming example that illustrates the chapter’s concepts C# Programming: From Problem Analysis to Program Design

Why Use A Loop? Repeat instructions with many data sets Repetition or iteration structures Rich set of looping structures while do…while for foreach statements C# Programming: From Problem Analysis to Program Design

Using the while Statement Simplest and most frequently used loop while (conditional expression) statement(s); Expression – sometimes called loop condition Returns a Boolean result of true or false No semicolon after the conditional expression Null body→ empty bodied loop→ infinite loop Enclose multiple statements for body in { } C# Programming: From Problem Analysis to Program Design

while Statement Pretest If the conditional expression evaluates to true, statement(s) performed If the conditional expression evaluates to false, statement(s) skipped Figure 6-1 Pretest loop C# Programming: From Problem Analysis to Program Design

Counter-Controlled Loop Loop control variable Variable simulating a counter Initialized Conditional expression designed so that you can exit the loop after a certain number of iterations Increment counter with each iteration Otherwise, infinite loop C# Programming: From Problem Analysis to Program Design

Counter-Controlled Loop Example /* SummedValues.cs Author: Doyle */ int sum = 0; //Line 1 int number = 1; //Line 2 while (number < 11) //Line 3 { //Line 4 sum = sum + number; //Line 5 number++; //Line 6 } //Line 7 Console.WriteLine(“Sum of values ” //Line 8 + “1 through 10” //Line 9 + “ is ” + sum); //Line 10 C# Programming: From Problem Analysis to Program Design

Counter-Controlled Loop (continued) Common problem Off-by-one error Loop body not executed for the last value OR Loop body executed one too many times C# Programming: From Problem Analysis to Program Design

Sentinel-Controlled Loop Exact number of times loop body should execute is not known Often used for inputting data Prime read on outside of loop Also referred to as indefinite loops Select a sentinel value Extreme value or dummy value Sentinel value used as operand in conditional expression Tells user what value to type to end loop C# Programming: From Problem Analysis to Program Design

Sentinel-Controlled Loop Example /* InputValuesLoop.cs Author: Doyle */ static void Main( ) { string inValue = ""; //Initialized to empty body Console.Write("This program will let you enter value after value."); Console.WriteLine("To Stop, enter = -99"); while (inValue!= "-99") Console.WriteLine("Enter value (-99 to exit)"); inValue = Console.ReadLine(); } C# Programming: From Problem Analysis to Program Design

Sentinel-Controlled Loop (continued) Useful for loops that process data stored in a file Sentinel is placed as last entry in file Conditional expression must match selected sentinel value C# Programming: From Problem Analysis to Program Design

Sentinel-Controlled Loop (continued) /* PrimeRead.cs Author: Doyle */ static void Main( ) { string inValue = ""; //Initialized to null int sum = 0, intValue; Console.Write("This program will let you enter"); Console.Write(" value after value. To Stop, enter"); Console.WriteLine(" -99"); Console.WriteLine("Enter value (-99 to exit)"); inValue = Console.ReadLine(); // Priming read C# Programming: From Problem Analysis to Program Design

Sentinel-Controlled Loop (continued) /* PrimeRead.cs continued */ while (inValue!= "-99") { intValue = Convert.ToInt32(inValue); sum += intValue; Console.WriteLine("Enter value (-99 to exit)"); inValue = Console.ReadLine(); } Console.WriteLine("Total values entered {0}", sum); C# Programming: From Problem Analysis to Program Design

Windows Applications Using Loops Event-driven model Manages the interaction between user and GUI by handling repetition for you Designed with graphical user interface (GUI) Predefined class called MessageBox Used to display information to users through its Show( ) method C# Programming: From Problem Analysis to Program Design

Windows Applications Example /* SquaredValues.cs Author: Doyle */ using System; using System.Windows.Forms; //Namespace for Windows Form class namespace SquaredValues { class SquaredValues static void Main( ) int counter = 0; string result =""; C# Programming: From Problem Analysis to Program Design

Windows Applications Example (continued) /* SquaredValues.cs - continued */ while (counter < 10) { counter++; result += " \t“+ counter + " \t" // Notice use of += to build + Math.Pow(counter, 2) + "\n"; // string for MessageBox } MessageBox.Show(result, “1 through 10 and their squares”); C# Programming: From Problem Analysis to Program Design

Windows Applications Example (continued) Figure 6-3 MessageBox dialog output C# Programming: From Problem Analysis to Program Design

Windows Applications To use MessageBox class in console application Add a reference to System.Windows.Forms.dll View > Solutions Explorer Right-click on the Reference folder Add Reference Add using directive to System.Windows.Forms namespace in program using System.Windows.Forms; C# Programming: From Problem Analysis to Program Design

Windows Applications (continued) Figure 6-4 Add a reference to a project C# Programming: From Problem Analysis to Program Design

Windows Applications (continued) Add Reference to System.Windows.Forms.dll Figure 6-5 Class libraries of .NET C# Programming: From Problem Analysis to Program Design

Windows MessageBox Class MessageBox – dialog box MessageBox.Show( ) method is overloaded First argument – string displayed in window Second argument – caption for Window title bar Third argument – type of dialog button Fourth argument – button type C# Programming: From Problem Analysis to Program Design

MessageBox class MessageBox.Show("Do you want another number ?", "State Controlled Loop", MessageBoxButtons.YesNo, MessageBoxIcon.Question) C# Programming: From Problem Analysis to Program Design

MessageBox class (continued) MessageBox.Show("Do you want another number ?", "State Controlled Loop", MessageBoxButtons.YesNo, MessageBoxIcon.Question) C# Programming: From Problem Analysis to Program Design

MessageBox class (continued) Figure 6-6 Button and icon arguments to MessageBox.Show( ) C# Programming: From Problem Analysis to Program Design

State-Controlled Loops Similar to sentinel-controlled loop Referred to as flag-controlled loops Instead of requiring a dummy or extreme value, use flag variable Can be Boolean variable (not a requirement) Variable must be initialized For each new iteration, evaluate to see when it changes state Change its value inside the loop – to stop the loop C# Programming: From Problem Analysis to Program Design

State-Controlled Loops Example bool moreData = true; while (moreData) { // moreData is updated inside the loop condition changes if (MessageBox.Show("Do you want another number ?", "State Controlled Loop", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) // Test to see if No clicked { moreData = false; } // End of if statement // More loop body statements } // End of while loop C# Programming: From Problem Analysis to Program Design

MessageBox.Show( ) Method MessageBox.Show("Do you want another number ?", "State Controlled Loop", MessageBoxButtons.YesNo, MessageBoxIcon.Question) 1st argument 2nd argument 4th argument 3rd argument Figure 6-7 State-controlled loop of random numbers C# Programming: From Problem Analysis to Program Design