COP3014 Lecture 5: Loops

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is an expression?

A test condition that is evaluated to decide whether a loop should repeat or not. True means run the loop body again False means quit

How is the statement portion handled?

Also the same as in if/else. IT can be a single statement or a compound statement/block {}

How can this be avoided?

By declaring the variable before the loop itself.

What are repetition statements called, and what are they used for?

Called loops, they are used to repeat the same code multiple times in succession.

What do break statements do?

Causes an immediate exit from any loop (and switch blocks)

What is a for loop most convenient with?

Counting loops; i.e. loops that are based on a counting variable, usually a known number of iterations.

Do loops have to count one-by-one and upward?

No, they can go up and down with any arithmetic expression/order.

What does the initialCondition do?

Runs once, at the start of the loop.

What is the scope of a variable declared inside a for loop header?

Scope only through the for loop's execution. Once the loop is finished, the variable is out of scope.

How is the expression handled?

The expression in these formats is handled the same as in the if/else statements discussed previously (0 means false anything else means true).

How many times does a loop repeat?

The number or repetitions is based on criteria defined in the loop structure, usually a true/false statement.

What happens next?

The testExpression is checked (just like a while loop). If its false, quit. If its true 1. run the loop body 2. run the iterativeStatement 3. go back to the testExpression step and repeat

What is the difference between the while and do/while loop formats?

They follow the same basic flowchart. The only exception is that in a while loop, the expression is tested first, and in a do/while loop the loop's "body" is executed first. So a do/while loop will execute at least once, even if the initial expression value is false. In a while loop the condition is checked one more time than the number of runs, and in a do/while loop the runs and condition checks are equivalent

What does continue do?

When used in a loop, this statement causes the current loop iteration to end, but the loop then moves onto the next step. - in a while or do-while loop, the rest of the loop body is skipped and execution moves on to the test condition. - in a for loop, the rest of the loop body is skipped and execution moves on to the iterative statement.

What is the format of a while loop?

While (expression) { statement }

What are the three loop structures in C++?

While, do while, and for loops. Note: all three types aren't actually needed, but having the different forms is convenient.

What statements can be used to alter the flow of control in loops (even though they aren't specifically needed)

break and continue

What is the format of a do/while loop?

do statement while (expression);

What is the format of a for loop?

for (initialCondition; booleanExpression; iterativeStatement) { statement 1; statement 2; ... statement n; }


Set pelajaran terkait

Impressionism/ Post Impressionism

View Set

Chapter 6: Consumer Purchasing Strategies & Wise Buying of Motor Vehicles

View Set

preoperative care practice questions

View Set

Chapter 26: Management of Patients With Dysrhythmias and Conduction Problems

View Set

Quiz 8: Prejudice, Discrimination, & Stereotyping; Conformity & Obedience

View Set