CIS 3260 Intro to Programming in C#

Similar documents
Repeating Instructions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

C++ Programming: From Problem Analysis to Program Design, Third Edition

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Repetition, Looping CS101

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Computer Programming I - Unit 5 Lecture page 1 of 14

Lecture 7 Tao Wang 1

Repetition and Loop Statements Chapter 5

CS112 Lecture: Repetition Statements

Repetition Structures

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Increment and the While. Class 15

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

First Name: Last: ID# 1. Hexadecimal uses the symbols 1, 2, 3, 4, 5, 6, 7 8, 9, A, B, C, D, E, F,G.

Control Structures II. Repetition (Loops)

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Repetition, Looping. While Loop

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

CS112 Lecture: Loops

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

204111: Computer and Programming

Chapter 5: Loops and Files

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Introduction. C provides two styles of flow control:

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

CS110D: PROGRAMMING LANGUAGE I

REPETITION CONTROL STRUCTURE LOGO

Chapter 7. Lists, Loops, and Printing The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

ECE 122. Engineering Problem Solving with Java

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Why Is Repetition Needed?

Making Decisions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

C Programming for Engineers Structured Program

Quiz Determine the output of the following program:

PLD Semester Exam Study Guide Dec. 2018

Problem Solving With Loops

Control Structures of C++ Programming (2)

Chapter 4: Control structures. Repetition

A Look Back at Arithmetic Operators: the Increment and Decrement

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

General Information About Iteration (Loops)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Chapter 4: Control structures

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Flow Control. CSC215 Lecture

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Chapter 5: Control Structures II (Repetition)

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Advanced Computer Programming

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Object-Oriented Programming in Java

switch-case Statements

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Repetition CSC 121 Fall 2014 Howard Rosenthal

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Looping. Arizona State University 1

Programming Fundamentals

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

Problem Solving and 'C' Programming

Name Section: M/W T/TH Number Definition Matching (6 Points)

Repetition Structures II

Name Section: M/W T/TH Number Definition Matching (8 Points)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Control structure: Repetition - Part 2

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Overview. Iteration 4 kinds of loops. Infinite Loops. for while do while foreach

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Principles of Computer Science I

10/9/2012. Comparison and Logical Operators The if Statement The if else Statement Nested if Statements The switch case. switch case Statement

Condition Controlled Loops. Introduction to Programming - Python

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Condition Controlled Loops. Introduction to Programming - Python

Conditionals and Recursion. Python Part 4

AP Programming - Chapter 6 Lecture

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Transcription:

Iteration (looping) McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. Understand the necessity of this program control structure Describe while loops Describe do while loops Describe for loops McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-2 Repeat instructions with many data sets Repetition or iteration structures Rich set of looping structures while do while for foreach statements McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-3 Dr. David McDonald Chapter 7a Page 1

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 McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-4 Pretest If the conditional expression evaluates to true, statement(s) performed If the conditional expression evaluates to false, statement(s) skipped McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-5 5 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 McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-6 Dr. David McDonald Chapter 7a Page 2

int sum = 0; int number = 1; while (number < 11) sum = sum + number; number++; messagebox.show ( Sum of values + 1 through 10 + is + sum, Example ); McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. C# Programming: From Problem Analysis to Program Design 7-7 7 Common problem Off-by-one error Loop body not executed for the last value OR Loop body executed one too many times McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. C# Programming: From Problem Analysis to Program Design 7-8 8 Exact number of times loop body should execute 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 McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-9 Dr. David McDonald Chapter 7a Page 3

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 See class demo for an example McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. C# Programming: From Problem Analysis to Program Design 7-10 10 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 McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-11 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 McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-12 Dr. David McDonald Chapter 7a Page 4

Very similar to the while loop A do block of code is executed first After the block executes at least once, then a while statement is tested Example: double sum = 0; int counter = 0; do sum += sum counter ++ while counter <= 10; //note: this test falls outside the loop itself McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-13 Pretest form of loop (like the while) Considered specialized form of while statement Usually associated with counter-controlled types Packages initialization, test, and update all on one line General form is: for (statement; conditional expression; statement) statement(s); McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-14 McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-15 Dr. David McDonald Chapter 7a Page 5

Replace above while loop with for loop below does same int counter = 0; while (counter < 11) MessageBox.Show(counter +, + Math.Pow(counter,2) +, + Math.Pow(counter,3)); counter++; for (int counter = 0; counter < 11; counter++) MessageBox.Show(counter +, + Math.Pow(counter,2) +, + Math.Pow(counter,3)); McGraw-Hill C# Programming: From Problem 2010 The McGraw-Hill Companies, Analysis to Inc. Program All rights Designreserved. 7-16 16 Never change the value of the index variable inside the loop Poor programming practice May lead to an endless loop // Poor Programming. For (int indexinteger = 1; indexinteger < 10; indexinteger++) indexinteger = 1; McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 7-17 Dr. David McDonald Chapter 7a Page 6