Loops ISYS 350.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Computer Science 1620 Loops.
CS150 Introduction to Computer Science 1
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
5.05 Apply Looping Structures
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Chapter 12: How Long Can This Go On?
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Loops ISYS 350. A Box of Chocolate Repeat this process until box is empty: – Take one chocolate from the box – Eat the chocolate – Box has more chocolate?
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6: The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
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.
Loops ISYS 350. Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Overview Go over parts of quiz? Another iteration structure for loop.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Loops ISYS 350. Two Types of Loops while loop for loop.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Controlling Program Flow with Looping Structures
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.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Loops ISYS 350. Two Types of Loops while loop for loop.
Loops ISYS 350. Two Types of Loops while loop for loop.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Loops ISYS 350. Write a Program that asks user to enter any numbers and displays the largest number Process: Largest = the first number Get the next number.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Loops ISYS 350.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Loops and Files.
Chapter 5: Repetition Structures
Repeating Program Instructions
Lecture 4B More Repetition Richard Gesick
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
Visual Basic..
Microsoft Visual Basic 2005: Reloaded Second Edition
Loops ISYS 350.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Loops ISYS 350.
Chapter (3) - Looping Questions.
CIS 16 Application Development Programming with Visual Basic
Loops ISYS 350.
Chapter 6: Repetition Statements
Loops ISYS 350.
Loops ISYS 350.
Loops ISYS 350.
Loops ISYS 350.
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Loops ISYS 350

A Box of Chocolate Analogy Repeat this process until box is empty: Take one chocolate from the box Eat the chocolate Box has more chocolate? Or Repeat this process until you are full: Am I full? Eat three chocolates

Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat this part until no more data Show the value of Sum

Compute the maximum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Determine Max Set Max to the first number (Max = first number) Get a number from the list If the number is greater than Max Then Max = the number Repeat this part until no more data Show the value of Max

Three Types of Loops while loop do loop for loop

The while Loop The while loop causes a statement or set of statements to repeat as long as a Boolean expression is true The simple logic is: While a Boolean expression is true, do some task A while loop has two parts: A Boolean expression that is tested for a true or false value A statement or set of statements that is repeated a long as the Boolean expression is true Boolean Expression Statement(s) True False

Structure of a while Loop In C#, the generic format of a while loop is: while (BooleanExpression) { Statements; } The first line is called the while clause Statements inside the curly braces are the body of the loop When a while loop executes, the Boolean expression is tested. If true, the statements are executed Each time the loop executes its statement or statements, we say the loop is iterating, or performing an iteration

Example: An Infinite Loop while (1 > 0) { MessageBox.Show("Looping"); }

An Infinite Loop that plays beep sound To play beep, we need System.Media namespace: using System.Media; while (1 > 0) // while (true) { SystemSounds.Beep.Play(); }

Using a Counter to Control the Loop int counter = 0; while (counter<5) { MessageBox.Show(counter.ToString()); ++counter; //counter++; }

Using a Flag Boolean continueFlag = true; while (continueFlag) { SystemSounds.Beep.Play(); if (MessageBox.Show("Do you want to continuue? ", "Continue?", MessageBoxButtons.YesNo) == DialogResult.No) continueFlag = false; }

Accumulator Find the sum of all numbers between 1 and N. Method 1: int N, Sum, Counter = 1; N = int.Parse(textBox1.Text); Sum = 0; while (Counter <= N) { Sum += Counter; ++Counter; } MessageBox.Show(Sum.ToString());

Accumulator Method 2 int N, Sum; N= int.Parse(textBox1.Text); Sum = 0; while (N > 0) { Sum += N; N -= 1; } MessageBox.Show(Sum.ToString());

Interactive Input using VB’s InputBox Statement Add a reference to Microsoft Visual Baisc: 1. From the Solution Explorer, right-click the References node, then click Add Reference 2. From the .Net Framework tab, select Microsoft Visual Baisc 3. Add this code to the form: using Microsoft.VisualBasic; Example of using InputBox: int myint; myint= int.Parse(Interaction.InputBox("enter a number:")); MessageBox.Show(myint.ToString());

Accumulator Find the sum of all numbers between 1 and N. Method 1: int N, Sum, Counter = 1; N = int.Parse(Interaction.InputBox("enter an integer:")); Sum = 0; while (Counter <= N) { Sum += Counter; ++Counter; } MessageBox.Show(Sum.ToString());

Accumulator Method 2 int N, Sum; N= int.Parse(Interaction.InputBox("enter a integer:")); Sum = 0; while (N > 0) { Sum += N; N -= 1; } MessageBox.Show(Sum.ToString());

While Loop Example N Factorial, N! int N, NFact, Counter = 1; N = int.Parse(Interaction.InputBox("enter a integer:")); NFact = 1; while (Counter <= N) { NFact *= Counter; ++Counter; } MessageBox.Show(NFact.ToString());

Enter Loan and Rate in textboxes, then display monthly pay for terms from 5 to 40 with a step of 5 in a ListBox double loan, rate, term, payment; loan = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); term = 5; while (term <= 40) { payment = Financial.Pmt(rate / 12, term * 12, -loan); listBox1.Items.Add("Term = " + term.ToString() + " Payment is: " + payment.ToString("c")); term += 5; }

Enter Loan and Term in textboxes, then display monthly pay for rates from 0.5% to 6% with a step of 0.5% in a ListBox double loan, rate, term, payment; loan = double.Parse(textBox1.Text); term = double.Parse(textBox2.Text); rate = 0.005; while (rate <= .06) { payment = Financial.Pmt(rate / 12, term * 12, -loan); listBox1.Items.Add(“Rate = " + rate.ToString(“p”) + " Payment is: " + payment.ToString("c")); rate += .005; }

The for Loop The for loop is specially designed for situations requiring a counter variable to control the number of times that a loop iterates You must specify three actions: Initialization: a one-time expression that defines the initial value of the counter Test: A Boolean expression to be tested. If true, the loop iterates. Update: increase or decrease the value of the counter A generic form is: for (initializationExpress; testExpression; updateExpression) { } The for loop is a pretest loop

Sample Code int count; for (count = 1; count <= 5; count++) { MessageBox.Show(“Hello”); } The initialization expression assign 1 to the count variable The expression count <=5 is tested. If true, continue to display the message. The update expression add 1 to the count variable Start the loop over // declare count variable in initialization expression for (int count = 1; count <= 5; count++) { MessageBox.Show(“Hello”); }

Other Forms of Update Expression In the update expression, the counter variable is typically incremented by 1. But, this is not a requirement. //increment by 10 for (int count = 0; count <=100; count += 10) { MessageBox.Show(count.ToString()); } You can decrement the counter variable to make it count backward //counting backward for (int count = 10; count >=0; count--)

Accumulator Find the sum of all numbers between 1 and N. Mrthod 1: int N, Sum, Counter; N = int.Parse(Interaction.InputBox("enter an integer:")); Sum = 0; for (Counter = 1; Counter <= N;Counter++ ) { Sum += Counter; } MessageBox.Show(Sum.ToString()); Method 2: int N, Sum, Counter; N = int.Parse(Interaction.InputBox("enter an integer:")); Sum = 0; for (Counter = N; Counter>0;Counter-- ) { Sum += Counter; } MessageBox.Show(Sum.ToString());

Enter Loan and Rate in textboxes, then display monthly pay for terms from 5 to 40 with a step of 5 in a ListBox double loan, rate, term, payment; loan = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); for (term = 5; term <= 40;term+=5 ) { payment = Financial.Pmt(rate / 12, term * 12, -loan); listBox1.Items.Add("Term = " + term.ToString() + " Payment is: " + payment.ToString("c")); }

Increment smaller than 1: Enter Loan and Term in textboxes, then display monthly pay for rates from 3% to 10% with a step of .5% in a ListBox double loan, rate, term, payment; loan = double.Parse(textBox1.Text); term = double.Parse(textBox2.Text); for (rate = .03; rate <= .1;rate+=.005 ) { payment = Financial.Pmt(rate / 12, term * 12, -loan); listBox1.Items.Add("rate = " + rate.ToString("p") + " Payment is: " + payment.ToString("c")); }

Find the Sum of All Even Numbers between 1 and N int N, Sum, Counter; N = int.Parse(Interaction.InputBox("enter an integer:")); Sum = 0; for (Counter = 1; Counter<=N;Counter++ ) { if (Counter % 2 ==0) Sum += Counter; } MessageBox.Show(Sum.ToString());