1 / 14

Looping Structures in C#: Examples and Syntax

Understand while, for, do-while loops in C# with syntax and examples. Learn how to iterate until a condition or flag is met, and work with nested loops. Improve your programming skills with practical exercises.

rkirk
Download Presentation

Looping Structures in C#: Examples and Syntax

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. CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm

  2. Exam 1 • 16 Scores: • 91 88 88 88 85 82 76 75 74 73 71 71 • 67 62 47 43 • Avg 73.8 • Std dev 14.1

  3. The while Loop • Syntax • while (condition) statement; • With multiple statement body: • while (condition) { statement; statement;}

  4. The for Loop • Syntax: • for (expression1; condition; expression2) statement; • for (expression1; condition; expression2){ • statement; • statement; • } • Same as: • expression1;while (condition){ statement; expression2;}

  5. Example for Loop • int numEmployees,curNum; • cout << “Enter Number of Employees: “; • cin >> numEmployees; • if (numEmployees > 0) • { • for (curNum = 0; curNum < numEmployees; curNum++) • { • cout << “Welcome to CorpLand!” << endl; • } • } • else • { • cout << "Egads!! Negative number of employees??" << endl; • }

  6. Looping for Input • char letterEntered = ‘A’; • while (letterEntered != ‘Z’) • { • cout << “Enter a letter: “; • cin >> letterEntered; • }

  7. Looping for Input • char command; • cout << "Welcome to our program!!" << endl • << "We'll process a sequence of commands" • << endl << "Enter command (or Q to quit): "; • cin >> command; • while (command != 'Q') • { • // ... do stuff for this command ... • cout << “Enter command (or Q to quit): “; • cin >> command; • }

  8. Looping until Flag • boolean noMoreData(); • boolean done = false; • … • while (!done) • { • … • // do a bunch of stuff • … • if (noMoreData() == true) • done = true; • } • cout << “We’re all through – thank you!”

  9. Nested Loops • int i, j; • for (i=0; i < 5; i++) • { • for (j=0; j < 6; j++) • { • cout << i << j << “ “; • } • cout << endl; • }

  10. Nested Loops • int i, j; • for (i=0; i < 5; i++) • { • for (j=0; j < i; j++) • { • cout << i << j << “ “; • } • cout << endl; • }

  11. Do-while Loop • Syntax • do statement;while (condition); • do{ statement; statement;}while (condition);

  12. do-while Example • int inputNum = 0; • do • { • // ... do something with inputNum ... • cout << “Please Input Number, –1 to quit: “; • cin >> inputNum; • } • while (inputNum != -1);

  13. What Is This? • while (stringEntered != “apple”) • { • cout << “Enter a red fruit: “; • cin >> stringEntered; • } • while (stringEntered != “banana”) • { • cout << “Enter a yellow fruit: “; • cin >> stringEntered; • } • while (stringEntered != “pomegranate”) • { • cout << “Enter a random fruit: “; • cin >> stringEntered; • }

  14. What Is This? • for (i = 0; i < 10 ; i++) • cout << “Interesting, isn’t it?” << endl; • while (answer != ‘D’) • { • cout << “Enter an answer: “; • cin >> answer; • } • while (answer != ‘D’) • { • cout << “Enter an answer: “; • cin >> answer; • }

More Related