Download presentation
Presentation is loading. Please wait.
1
1 CS 105 Lecture 6 While Loops Wed, Feb 16, 2011, 5:13 pm
2
2 Switch Statements Also called switch/case statement Just "case" in other languages Selects among several different actions Can only select from integer or character If an integer value is matched, statements under control of that case block are executed.
3
3 Switch/Case Example int numPassengers; cout << “Enter Passengers: “; cin >> numPassengers; switch (numPassengers) { case 0: zeroPassengers(); break; case 1: onePassenger(); break; default: manyPassengers(); break; }
4
4 Switch/Case Example char menuItem; cout << "Enter Menu Selection: "; cin >> menuItem; switch (menuItem) { case 'O': orderFunction(); break; case 'C': checkoutFunction(); break; default: errorFunction(); break; }
5
5 Iteration Iteration (Looping): Performing a series of statements multiple times until some condition is met. Eliminates the need for redundant coding or function calls Multiple ways to loop in C++
6
6 Syntaxes: while (condition) statement; while (condition) { statement; statement; } The while Loop
7
7 while Loop With Counter const int MAX = 100; int counter = 0; while (counter < MAX) { cout << counter << “ “ << endl; counter++; }
8
8 while Loop With Limit Read- In int numEmployees,curNum = 0; cout << “Enter Number of Employees: “; cin >> numEmployees; if (numEmployees > 0) { while (curNum < numEmployees) { cout << “Welcome to CorpLand!” << endl; curNum++; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.