CFP11 - Repetition Structures

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/24

flashcard set

Earn XP

Description and Tags

examsssss

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

25 Terms

1
New cards

Repetitive section of code requires four elements:

  • Repetition statement

  • Condition

  • Statement that initially sets the condition

  • There must be a statement within the repeating section of code that allows the condition to become false

2
New cards

Both defines the boundaries containing the repeating section of code and controls whether the code will be executed or not.

Repetition statement

3
New cards

Forms of Repetition Structures:

  • while structure

  • for structure

  • do while structure

4
New cards
  • Each structure requires a condition that must be evaluated 

  • Are identical to selections statements, if condition is true, the code is executed; otherwise it is not.

Condition

5
New cards

Must always be placed before the condition is first evaluated to ensure correct loop execution the first time the condition is evaluated.

Statement that initially sets the condition

6
New cards

At some point, the repetitions stops.

There must be a statement within the repeating section of code that allows the condition to become false.

7
New cards
  • Condition is tested before any statements within the loop are executed.

  • If condition is true, the executable statements within the loop are executed, if initial value of the condition is false, the executable statements within the loop are never executed and control transfers to the first statement after the loop.

Pretest Loop

8
New cards

Also referred to as entrance-controlled loops.

Example: while & for loop

Pretest Loop

9
New cards
<p>Which loop is this?</p>

Which loop is this?

Pretest Loop

10
New cards
  • Referred to as exit-controlled loop

  • Loop that evaluates a condition at the end of the repeating section of code

  • Execute the loop statements at least once before the condition is tested. Since the executable statements within the loop are continually executed until the condition becomes false, there always must be a statement within the loop that updates the condition and permits it to become false.

• Example : Do while

Posttest Loop

11
New cards
<p>What loop?</p>

What loop?

Postest Loop

12
New cards

Types of Condition tested:

  • Fixed count loop

  • Variable condition loop

13
New cards
  • The condition is used to keep track of how many repetitions have occurred.

  • Fixed number of calculations is performed, or a fixed number of lines are printed, at which point the repeating section of code is exited.

Fixed count loop

14
New cards
  • The tested condition does not depend on a count being reached, but rather on a variable that can change interactively with each pass through the loop. When a specified value is encountered, regardless of how many iterations have occurred repetitions stop.

Variable condition loop

15
New cards
  • The statement following the expression is executed repeatedly as long as the expression evaluates to a nonzero value

  • Considering just the expression and the statement following the parentheses.

  • Literally loops back to itself to recheck the expression until it evaluates to zero (becomes false)

While Loops

16
New cards

Syntax of a While Loop:

while (expression)

       statement;

17
New cards

Process used for While:

1. Test the expression.

2. If the expression has a nonzero (true) value.

   a. Execute the statement following the parentheses.

   b. Go back to step 1.

else

Exit the while statement and execute the next executable

statement following the while statement.

18
New cards
<p>Anatomy of a While Loop:</p>

Anatomy of a While Loop:

(okay thanks)

19
New cards
  • Combining interactive data entry with the repetition capabilities

  • Used to accept and then display data.

Interactive While Loops

20
New cards
  • Data values used to signal either the start or end of data series.

  • Must be selected so as not to conflict

    with legitimate data values.

Sentinels

21
New cards
  • This type of loop is where a counter has been used to control the number of loop iterations by means of a while statement.

  • Variable condition loops may also be constructed. 

Fixed count loops

22
New cards

Two useful statements in connection with repetition statements :

  • Break

  • Continue

23
New cards

Forces an immediate break, or exit from switch, while, for and do while statements.

Break

24
New cards

Applies only to loops created with while and do-while and for statements. is encountered in a loop, the next iteration of the loop immediately begun.

Continue

25
New cards
  • All statements must be terminated by a semi-colon. A semi- colon with nothing preceding it is also a valid statement, called a null statement.

  • This is a do-nothing statement that is used where a statement is syntactically required, but no action is called for.

Null Statement