110 likes | 220 Views
A general condition loop is a catch-all category for loops that are not count-controlled nor sentinel-controlled The program loops until some general condition becomes false Usually it is implemented with the while statement. General Condition Loop.
E N D
A general condition loop is a catch-all category for loops that are not count-controlled nor sentinel-controlled The program loops until some general condition becomes false Usually it is implemented with the while statement General Condition Loop
Consider Nim's game. This is a two player game. We begin with a pile of 21 stones, and the players takes turns, each player taking 1, 2, or 3 stones from the pile. The player who takes the last stone from the pile loses. Nim's Game
int num = 21, winner = 1, n; while (num > 0) { cout << num << " stones. Plr 1: How many?"; cin >> n; num -= n; if(num <= 0) winner = 2; else { cout << num << " stones. “ << “Plr 2: Take how many?"; cin >> n; num -= n; } } cout << "Winner is " << winner << endl; Nim's Game (Code)
21 stones. Plr 1: Take how many?2 19 stones. Plr 2: Take how many?3 16 stones. Plr 1: Take how many?1 15 stones. Plr 2: Take how many?2 13 stones. Plr 1: Take how many?3 10 stones. Plr 2: Take how many?1 9 stones. Plr 1: Take how many?2 7 stones. Plr 2: Take how many?3 4 stones. Plr 1: Take how many?3 1 stones. Plr 2: Take how many?1 Winner is 1 Nim's Game (Output)
Three different C++ statements can be used to change how the loop code is done: The break statement ends the loop immediately. Control pass to the first statement after the loop. The continue statement jumps to the end of the loop, but the loop continues. The return statement returns from the function (main). Break, continue, and return
int num = 21, p = 1, n; while (true) { cout << num << " stones. Plr " << p << ": Take how many?"; cin >> n; num -= n; p = 3 - p; // Change player, 1->2, 2-> 1 if(num <= 0) { cout << "Winner is " << p << endl; break; } } Nim's Game (Code 2)
The do-while statement is similar to the while statement, but the test comes after the loop body: do { <statement> while (<Boolean expression>); So, the loop body is always done first, then the expression is evaluated and if true, the loop is repeated. Do-While statement
The do-while statement could be used to implement a sentinel controlled loop, but at the cost of writing the test twice. do { cin >> temp; if(temp >= 0) <process temp>; while(temp>=0); Sentinel-controlled Loop
The comma operator is used to sequentially evaluate expressions. For example, a+b, c-d, i++, will first evaluate a+b, then evaluate c-d, and then evaluate i++. The value of whole expression is the value of the last term. It is most commonly used in the iteration step of a for-loop. for(i=0, j=0; i < 10; i++, j+=2){} The Comma Operator
Consider how to print out one page of a monthly calendar: Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 12 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Exercise
The program should read in the number of days in the month, and the first day of the month (0=Sun, 1=Mon, 2=Tue,...) and then print out the month properly formatted. Exercise (Cont'd)