05.Looping

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

What are the two pieces contained in a while loop?

A condition (a boolean expression) and a block (one or more statements).

2
New cards

What is an iteration in the context of loops?

An iteration is a single execution of the loop body.

3
New cards

What does a while loop do when the condition is false?

It skips the block of code inside the loop.

4
New cards

What is the main purpose of sentinel values in looping?

They act as special values to indicate the end of a data set.

5
New cards

What is the difference between pre-test loops and post-test loops?

Pre-test loops check the condition before each iteration (like while loops), while post-test loops check after each iteration (like do-while loops).

6
New cards

What should you typically do within a loop to prevent infinite loops?

Modify the loop condition by changing the variable used in the condition.

7
New cards

What keyword can be used to exit a loop prematurely?

The break keyword.

8
New cards

What is the initializer in a for loop?

It is executed once before the loop starts and is used to initialize a loop control variable.

9
New cards

When should you use a for loop instead of a while loop?

When you know the exact number of iterations beforehand.

10
New cards

What does the increment operator (++) do in loops?

It adds one to the variable and stores the updated value.

11
New cards

How does a do-while loop differ from a while loop?

A do-while loop executes the block at least once before checking the condition.

12
New cards

What is input validation in the context of looping?

It is the process of ensuring that user inputs are valid before proceeding.

13
New cards

What type of loop structure is suitable for reading lines from a file?

A while loop can be used to read each line until the end of the file.

14
New cards

What happens if you attempt to read past the end of a file?

The stream sets its EOF (end-of-file) flag.

15
New cards

In a nested loop, what must you ensure regarding control variables?

Each loop should have separate control variables to avoid conflicts.

16
New cards

What does the .fail() function check for in file streams?

It checks if a prior read operation has failed.

17
New cards

What is the main benefit of using file stream objects?

They allow for input/output operations with files in a similar way to standard input/output.

18
New cards

What does a while loop syntax typically look like?

while (boolean_expression) { // block of statements }.

19
New cards

What happens when you encounter a continue statement in a loop?

It ends the current iteration and returns to the top of the loop.

20
New cards

Why is it important to close file streams after use?

It releases system resources and ensures that all data is written to disk.