LoopStructureS16 - Loops ISYS 350 Two Types of Loops while...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Loops ISYS 350
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Two Types of Loops while loop for loop
Image of page 2
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
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Structure of a while Loop In C#, the generic format of a while loop is: while ( BooleanExpressio n) { 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
Image of page 4
Example: An Infinite Loop while (1 > 0) { MessageBox.Show("Looping"); }
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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;
Image of page 6
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++; }
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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; }
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern