Bài giảng C++ Programming: From Problem Analysis to Program Design, Fourth Edition - Chapter 5: Control Structures II (Repetition)

Tài liệu Bài giảng C++ Programming: From Problem Analysis to Program Design, Fourth Edition - Chapter 5: Control Structures II (Repetition): C++ Programming: From Problem Analysis to Program Design, Fourth EditionChapter 5: Control Structures II (Repetition)C++ Programming: From Problem Analysis to Program Design, Fourth Edition2ObjectivesIn this chapter, you will:Learn about repetition (looping) control structuresExplore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structuresExamine break and continue statementsDiscover how to form and use nested control structuresC++ Programming: From Problem Analysis to Program Design, Fourth Edition3Why Is Repetition Needed?Repetition allows you to efficiently use variablesCan input, add, and average multiple numbers using a limited number of variablesFor example, to add five numbers:Declare a variable for each number, input the numbers and add the variables togetherCreate a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbersC++ Programming: From Problem Analysis to Pr...

ppt48 trang | Chia sẻ: honghanh66 | Lượt xem: 854 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng C++ Programming: From Problem Analysis to Program Design, Fourth Edition - Chapter 5: Control Structures II (Repetition), để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
C++ Programming: From Problem Analysis to Program Design, Fourth EditionChapter 5: Control Structures II (Repetition)C++ Programming: From Problem Analysis to Program Design, Fourth Edition2ObjectivesIn this chapter, you will:Learn about repetition (looping) control structuresExplore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structuresExamine break and continue statementsDiscover how to form and use nested control structuresC++ Programming: From Problem Analysis to Program Design, Fourth Edition3Why Is Repetition Needed?Repetition allows you to efficiently use variablesCan input, add, and average multiple numbers using a limited number of variablesFor example, to add five numbers:Declare a variable for each number, input the numbers and add the variables togetherCreate a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbersC++ Programming: From Problem Analysis to Program Design, Fourth Edition4while Looping (Repetition) StructureThe general form of the while statement is: while is a reserved wordStatement can be simple or compoundExpression acts as a decision maker and is usually a logical expression Statement is called the body of the loop The parentheses are part of the syntaxC++ Programming: From Problem Analysis to Program Design, Fourth Edition5while Looping (Repetition) Structure (continued)Infinite loop: continues to execute endlesslyAvoided by including statements in loop body that assure exit condition is eventually falseC++ Programming: From Problem Analysis to Program Design, Fourth Edition6while Looping (Repetition) Structure (continued)C++ Programming: From Problem Analysis to Program Design, Fourth Edition7Designing while LoopsC++ Programming: From Problem Analysis to Program Design, Fourth Edition8Case 1: Counter-Controlled while LoopsIf you know exactly how many pieces of data need to be read, the while loop becomes a counter-controlled loopC++ Programming: From Problem Analysis to Program Design, Fourth Edition9Case 2: Sentinel-Controlled while LoopsSentinel variable is tested in the condition and loop ends when sentinel is encounteredC++ Programming: From Problem Analysis to Program Design, Fourth Edition10Telephone DigitsExample 5-5 provides an example of a sentinel-controlled loopThe program converts uppercase letters to their corresponding telephone digitC++ Programming: From Problem Analysis to Program Design, Fourth Edition11Case 3: Flag-Controlled while LoopsA flag-controlled while loop uses a bool variable to control the loopThe flag-controlled while loop takes the form:C++ Programming: From Problem Analysis to Program Design, Fourth Edition12Number Guessing GameExample 5-6 implements a number guessing game using a flag-controlled while loopThe program uses the function rand of the header file cstdlib to generate a random numberrand() returns an int value between 0 and 32767To convert it to an integer greater than or equal to 0 and less than 100:rand() % 100C++ Programming: From Problem Analysis to Program Design, Fourth Edition13Case 4: EOF-Controlled while LoopsUse an EOF (End Of File)-controlled while loopThe logical value returned by cin can determine if the program has ended inputC++ Programming: From Problem Analysis to Program Design, Fourth Edition14eof FunctionThe function eof can determine the end of file statusLike other I/O functions (get, ignore, peek), eof is a member of data type istreamThe syntax for the function eof is: where istreamVar is an input stream variable, such as cinC++ Programming: From Problem Analysis to Program Design, Fourth Edition15More on Expressions in while StatementsThe expression in a while statement can be complexFor example:while ((noOfGuesses > acctNumber >> beginningBalance;Get transaction code and transaction amount infile >> transactionCode >> transactionAmount;Analyze transaction code and update appropriate variablesC++ Programming: From Problem Analysis to Program Design, Fourth Edition27Programming Example: Steps (continued)Repeat Steps 4 and 5 until there is no more dataSince the number of entries in the input file is not known, use an EOF-controlled while loopPrint the resultC++ Programming: From Problem Analysis to Program Design, Fourth Edition28Programming Example: Main AlgorithmDeclare and initialize variablesOpen input fileIf input file does not exist, exitOpen output fileOutput numbers in appropriate formatsRead accountNumber and beginningBalanceC++ Programming: From Problem Analysis to Program Design, Fourth Edition29Programming Example: Main Algorithm (continued)Set accountBalance to beginningBalanceRead transactionCode and transactionAmount while (not end of input file) if transactionCode is 'D' or 'd' Add transactionAmount to accountBalance Increment numberOfDeposits if transactionCode is 'I' or 'i' Add transactionAmount to accountBalance Add transactionAmount to interestPaidC++ Programming: From Problem Analysis to Program Design, Fourth Edition30Programming Example: Main Algorithm (continued)if transactionCode is 'W' or 'w' Subtract transactionAmount from accountBalanceIncrement numberOfWithdrawalsif (accountBalance = 1; i--)Answer: ***** **** *** ** *C++ Programming: From Problem Analysis to Program Design, Fourth Edition46SummaryC++ has three looping (repetition) structures:while, for, and dowhilewhile, for, and do are reserved wordswhile and for loops are called pretest loopsdo...while loop is called a posttest loopwhile and for may not execute at all, but do...while always executes at least onceC++ Programming: From Problem Analysis to Program Design, Fourth Edition47Summary (continued)while: expression is the decision maker, and the statement is the body of the loopA while loop can be:Counter-controlledSentinel-controlledEOF-controlledIn the Windows console environment, the end-of-file marker is entered using Ctrl+zC++ Programming: From Problem Analysis to Program Design, Fourth Edition48Summary (continued)for loop: simplifies the writing of a counter-controlled while loopPutting a semicolon at the end of the for loop is a semantic errorExecuting a break statement in the body of a loop immediately terminates the loopExecuting a continue statement in the body of a loop skips to the next iteration

Các file đính kèm theo tài liệu này:

  • ppt9781423902096_ppt_ch05_4313.ppt
Tài liệu liên quan