Looping III do while statement CSCE 106 Outline

  • Slides: 11
Download presentation
Looping III (do … while statement) CSCE 106

Looping III (do … while statement) CSCE 106

Outline § do … while statement § Review of looping § break and continue

Outline § do … while statement § Review of looping § break and continue in loops CSCE 106 2

Repetition § Repetition or looping provides for the repeated execution of part of the

Repetition § Repetition or looping provides for the repeated execution of part of the algorithm. CSCE 106 3

do … while Statement § The do - while statement is similar to the

do … while Statement § The do - while statement is similar to the while loop, but the do - while loop has the test at the end. § General syntax: do { statement; } while ( loop repetition condition ) ; § Notice the iterative part executes before the looptest § Convenient when at least one repetition of the loop body is required CSCE 106 4

Data-validation Loop do prompt for and read a data item while data item is

Data-validation Loop do prompt for and read a data item while data item is invalid § E. g. do { cout << “Enter number of employees: “; cin >> num. Emp; } while (num. Emp <= 0); CSCE 106 5

Data-validation Loop (cont’d( do { cout << “Please enter a number between 0 and

Data-validation Loop (cont’d( do { cout << “Please enter a number between 0 and 255”; cin >> number; } while (number < 0 || number > 255); CSCE 106 6

do … while and Menus § do … while is also very useful to

do … while and Menus § do … while is also very useful to control a menu -driven program § Displaying a list of choices for user to select from § E. g. list of edit operations: D - Delete a substring F - Find a string I - Insert a string R - Replace a substring Q - Quit Enter D, F, I, R, or Q as your selection: CSCE 106 7

Review of Looping § C++ provides three statements for implementing loops § Use for

Review of Looping § C++ provides three statements for implementing loops § Use for to implement counting loops § Use do … while to implement loops that must execute at least once § Use while to code other conditional loops § Remember that you could nest all types of loop statements when necessary. CSCE 106 8

break and continue in Loops § break and continue are two C++ reserved words.

break and continue in Loops § break and continue are two C++ reserved words. § You have seen break before with the switch statement. § It could be used in the body of a loop to give the same effect of jumping out of the body of the loop. § continue reserved word is used to jump to the loop testing condition, and hence ignoring anything else in the rest of the loop body. CSCE 106 9

break and continue in Loops (cont’d) int k = 40; Output while (k <

break and continue in Loops (cont’d) int k = 40; Output while (k < 100) { k = 50 k += 10; k = 60 if (k == 70) Finished looping continue; if (k == 80) break; cout << "k = " << k <<'n'; } cout << "Finished looping" << endl; CSCE 106 10

Next lecture will be about Arrays CSCE 106 11

Next lecture will be about Arrays CSCE 106 11