C++ Loops Syntax

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

What is a nested loop

A loop inside another loop is a nested loop Eg a program to list all of the rivers in Ireland could have a loop to go through all of the counties and within that loop another loop that would process all of the rivers in a given county.

do While Loop concept

Do something, and then check a condition. It will do what is under the do loop while the expression is true

Scope of a loop control variable declared in the for statement

If a loop control variable is declared before the loop then it can be referred to outside of the loop but if the variable is declared inside the initialization portion of the for, then it can only be used inside the loop. I.e. the scope is restricted to the loop only. The variable ceases to exist outside of the loop. Eg for(int i = 1; i <= 5; i++) { }

Differenes between the 3 types of Loop

In a for loop, the conditional expression is always tested at the top of the loop. This mean that the code inside the loop may not be executed at all if the condition is false to begin with. E.g. for(count=10; count<5; count++) statement; A while loop also checks the conditional expression at the top of the loop and so may not execute at all. The do while loop checks its condition at the bottom of the loop and so it will always execute at least once.

How do you force an early iteration of a loop

The continue statement forces the next iteration of the loop to take place, skipping any code between itself and the conditional expression that controls the loop. In the case of a for loop, the continue statement causes the increment prt of the loop to be performed, then the conditional expression is executed, and then the loop continues.

What is the increment operator

The increment operator is ++. It increases its operand by 1. It performs the operation more efficiently than by writing out a statement like count=count+1;

How do you terminate a loop

When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. When loops are nested (i.e. one inside another), the break will cause an exit from the loop it is in [ie the innermost of ot os on the innermost]

Can the body of a do while loop be empty

Yes, the body of any loop can be empty

Can you leave out sections of the for definition

Yes, you can leave out sections e.g. the initialisation or increment exression. E.g. if the initial value is calculated in a complex way, it might be better to do that first and then to have the loop E.g. x=0; for( ; x<10; ) statement; This is valid. Note the semicolons still need to be there.

Do While syntax

do { <statement>; } while (expression);

Create an infinite loop

for ( ; ; ) { // This is a loop that never terminates }

For Loop Syntax

for (initialization; condition; increment) statement; initialization sets a loop control variable to an initial value. condition is an expression that is tested each time the loop repeats. As long as condition is true (nonzero), the loop keeps running. The condition does not need to involve the loop control variable - it can be any valid C++ expression. The increment is an expression that determines how the loop control variable is incremented each time the loop repeats. Each of the 3 sections is separated by semicolons.

While syntax

initialization; While (expression) statement; // statement may be a single statement or block

Give an example of a while loop with no statements in the body of the loop

while(rand() != 100); This loop iterates until the random number generated by rand() equals 100.


Set pelajaran terkait

SWIMMING The Four (4) Competition Strokes:

View Set

ABO and H blood group system study questions

View Set

Ch 24: International and Space Law

View Set

General insurance exam questions

View Set

PET3323C Lab 3 Special Senses pt 1 Smell, Taste, and Hearing

View Set