1 / 27

Loops

Loops. ISYS 350. Two Types of Loops. while 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:

rmorena
Download Presentation

Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loops ISYS 350

  2. Two Types of Loops • while loop • for loop

  3. 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 True Statement(s) False

  4. 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

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

  6. 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(); }

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

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

  9. Flag Example bool myFlag = true; int counter2 = 1; while (myFlag) { MessageBox.Show(counter2.ToString()); ++counter2; if (counter2 > 5) {myFlag = false;} }

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

  11. While Loop ExampleN Factorial, N! int N, NFact, Counter = 1; N = int.Parse(textBox1.Text); NFact = 1; while (Counter <= N) { NFact *= Counter; ++Counter; } MessageBox.Show(NFact.ToString());

  12. Enter present value and rate in textboxes, then display future values for years from 5 to 40 with a step of 5 in a ListBox double pv, rate, year, fv; pv = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); year = 5; while (year <= 40) { fv = pv * Math.Pow(1 + rate, year); listBox1.Items.Add("Year = " + year.ToString() + " Future valuet is: " + fv.ToString("c")); year += 5; }

  13. Enter present value and rate in textboxes, then display future values with user specified starting year, ending year and step value in a ListBox

  14. Code Example double pv, rate, year, startYr, endYr,stepValue,fv; pv = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); startYr = double.Parse(textBox3.Text); endYr = double.Parse(textBox4.Text); stepValue = double.Parse(textBox5.Text); year = startYr; while (year <= endYr) { fv = pv * Math.Pow(1 + rate, year); listBox1.Items.Add("Year = " + year.ToString() + " Future valuet is: " + fv.ToString("c")); year += stepValue; }

  15. Enter pv and year in textboxes, then display fv for rates from 5% to 10% with a step of 0.5% in a ListBox double pv, rate, year, fv; pv = double.Parse(textBox1.Text); year = double.Parse(textBox2.Text); rate = 0.05; while (rate <= .1) { fv = pv * Math.Pow(1 + rate, year); listBox1.Items.Add("Rate = " + rate.ToString("p") + " Future valuet is: " + fv.ToString("c")); rate += .005; } Note: A double may not exactly equal to .1 while (rate < .105)

  16. 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

  17. 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”); }

  18. 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--) { MessageBox.Show(count.ToString());}

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

  20. Enter present value and rate in textboxes, then display future values for years from 5 to 40 with a step of 5 in a ListBox double pv, rate, year, fv; pv = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); for (year=5;year<=40;year+=5) { fv = pv * Math.Pow(1 + rate, year); listBox1.Items.Add("Year = " + year.ToString() + " Future valuet is: " + fv.ToString("c")); }

  21. 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 pv, rate, year, fv; pv = double.Parse(textBox1.Text); year = double.Parse(textBox2.Text); for (rate=.03;rate<=.1;rate+=0.005) { fv = pv * Math.Pow(1 + rate, year); listBox1.Items.Add("Rate = " + rate.ToString("p") + " Future valuet is: " + fv.ToString("c")); } Note: for (rate = .03; rate < .105; rate += .005)

  22. Find the Sum of All Even Numbers between 1 and N int N, Sum, Counter; N = int.Parse(textBox1.Text); Sum = 0; for (Counter = 1; Counter<=N;Counter++ ) { if (Counter % 2 ==0) Sum += Counter; } MessageBox.Show(Sum.ToString());

  23. Search a String string myString = textBox1.Text, searchChar = textBox2.Text; int index=0, counter = 0; Boolean found = false; for (index=0;index<myString.Length;index++) { if (myString.Substring(index, 1) == searchChar) { ++counter; found = true; } } if (found) { textBox3.Text = "Found " + counter.ToString(); } else { textBox3.Text = "Not found!"; }

  24. Flag Example bool myFlagTrue = true; int counter = 1; for (counter=1;myFlagTrue;counter++) { MessageBox.Show(counter.ToString()); if(counter>=5) { myFlagTrue = false; } }

  25. The loop control variable may be declared before the loop or within the for statement Int counter; for (counter = 0; counter <=5; counter++) { MessageBox.Show(counter.ToString());} What is the value of counter after the loop? • Or for (int counter= 0; counter <=5; counter++) { MessageBox.Show(counter.ToString());}

  26. Search a String • Useful property and method: • Length: number of characters in a string • Substring(starting index, number of character) • IndexOf()

  27. Code Example string myString = textBox1.Text, searchChar = textBox2.Text; int index=0, counter = 0; Boolean found = false; while (index<myString.Length) // while (index<=myString.Length-1) { if (myString.Substring(index,1)==searchChar) { ++counter; found = true; } ++index; } if (found) { textBox3.Text= "Found " + counter.ToString(); } else { textBox3.Text = "Not found!"; }

More Related