Loops ISYS 350. A Box of Chocolate Repeat this process until box is empty: – Take one chocolate...

25
Loops ISYS 350

Transcript of Loops ISYS 350. A Box of Chocolate Repeat this process until box is empty: – Take one chocolate...

Page 1: 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?

Loops

ISYS 350

Page 2: 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?

A Box of Chocolate• 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:

– Take one chocolate from the box– Eat the chocolate– Am I full?

• Or• Eat three chocolates

Page 3: 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?

Two Types of Loops

• while loop• for loop

Page 4: 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?

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

BooleanExpression Statement(s)

True

False

Page 5: 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?

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

Page 6: 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?

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

}

Page 7: 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?

An Infinite Loop that plays beep sound

while (1 > 0)// while (true) { SystemSounds.Beep.Play();

}

To play beep, we need System.Media namespace:

using System.Media;

Page 8: 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?

Using a Counter to Control the Loop

int counter = 0; while (counter<5) { MessageBox.Show(counter.ToString()); ++counter; //counter++; }

Page 9: 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?

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

Page 10: 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?

Accumulator

Find the sum of all numbers between 1 and N.

int N, Sum, Counter = 1;N = int.Parse(textBox1.Text);Sum = 0;while (Counter <= N) { Sum += Counter; ++Counter; }MessageBox.Show(Sum.ToString());

Method 1:

Page 11: 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?

AccumulatorMethod 2

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

Page 12: 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?

Interactive Input using VB’s InputBox Statement1. Add a reference to Microsoft Visual Baisc: Project/Add Reference/Assemblies\Framework\Microsoft Visual Basic

2. Add this code to the form:using Microsoft.VisualBasic;

int myint;myint= int.Parse(Interaction.InputBox("enter a number:"));MessageBox.Show(myint.ToString());

Example of using InputBox:

Page 13: 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?

Accumulator

Find the sum of all numbers between 1 and N.

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

Method 1:

Page 14: 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?

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

Page 15: 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?

While Loop ExampleN 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());

Page 16: 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?

Using Visual Basic’s Pmt Function to Compute Monthly Loan Payment

• payment = Financial.Pmt(rate / 12, term * 12, -loan);

Page 17: 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?

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

Page 18: 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?

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

Page 19: 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?

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

Page 20: 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?

Sample Codeint 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 expressionfor (int count = 1; count <= 5; count++){ MessageBox.Show(“Hello”);}

Page 21: 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?

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 10for (int count = 0; count <=100; count += 10){ MessageBox.Show(count.ToString());}

• You can decrement the counter variable to make it count backward

//counting backwardfor (int count = 10; count >=0; count--){ MessageBox.Show(count.ToString());}

Page 22: 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?

AccumulatorFind the sum of all 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++ ) { Sum += Counter; } MessageBox.Show(Sum.ToString());

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

Mrthod 1:

Method 2:

Page 23: 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?

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

Page 24: 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?

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

Page 25: 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?

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