1 / 7

Repetition Structures

While Repetition. The while repetition structure looks like while(condition) //while condition is true{ statement(s); // execute statements}. Using a Counter to Control Repetition. Counter-Controlled Repetition This is a mechanism for controlling how many times the statements with

kermit
Télécharger la présentation

Repetition Structures

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. Repetition Structures A repetition structure allows a user to specify that a program should repeat an action while some condition remains true. The while structure is one of these repetition structures.

    2. While Repetition The while repetition structure looks like while(condition) //while condition is true { statement(s); // execute statements }

    3. Using a Counter to Control Repetition Counter-Controlled Repetition This is a mechanism for controlling how many times the statements within a loop executes by using a counter. This type of repetition is often called definite repetition since we are defining a particular number of repetitions.

    4. Example of Counter-Controlled Repetition In this example we want to determine the average score of a basketball team consisting of just 5 players. We will ask each player the points they scored and then we will want to display their average.

    5. Using a Sentinel Value to control Repetition Sentinel Controlled Repetition What if we do not know the number of repetitions? Well, that is where the sentinel value comes in to play. A sentinel value is just a special value to indicate the end of data entry Obviously, our sentinel should not look like the rest of our input data.

    6. Example of Sentinel Controlled Repetition Let’s look at another example to determine the average points scored of a basketball team in which we do not know the number of players or even if they have any players. We will ask the user for the number of points scored or to enter a -1 if there are not any players on the team.

    7. Nested-Control Structures Do you think we can nest an if…else statement within our repetition structure for counting certain conditions? Let’s look at another example. This time we will depart from our sports theme.

More Related