Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lecture_3

.pdf
Скачиваний:
9
Добавлен:
25.02.2016
Размер:
394.09 Кб
Скачать

Week 3

Control Statements

Jumagaliyev Assylbek

asylbek.jumagaliyev@gmail.com

Introduction

Presenting the theory and principles of structured programming.

Demonstration of the for, do...while and switch statements.

Essentials of Counter-Controlled

Repetition

Counter-controlled repetition requires

the name of a control variable (or loop counter)

the initial value of the control variable

the loop-continuation condition that tests for the final value of the control variable (i.e., whether looping should continue)

the increment (or decrement) by which the control variable is modified each time through the loop.

Counter-controlled repetition.

1. name

2. initial value

3. loop-continuation conditio

4. final value

5. increment (or decrement)

1 2 3 4 5 6 7 8 9 10

for Repetition Statement

The while statement can be used to implement any counter-controlled loop.

for repetition statement specifies the counter-controlled repetition details in a single line of code.

To illustrate the power of for, we rewrite the previous program.

for Repetition Statement

1 2 3 4 5 6 7 8 9 10

for Statement Header Components

Notice that previous example uses the loop-continuation condition counter <= 10. If the programmer incorrectly wrote counter < 10, then the loop would execute only 9 times. This is a common off-by-one error.

while vs. for

The general form of the for statement is

for ( initialization; loopContinuationCondition; increment )

{

Statement;

}

The general form of the while statement is

initialization;

while ( loopContinuationCondition )

{

Statement;

Increment;

}

for Repetition Statement

The initialization, loop-continuation condition and increment expressions of a for statement can contain arithmetic expressions. For example, if x = 2 and y = 10, and x and y are not modified in the loop body, the for header

for ( int j = x; j <= 4 * x * y; j += y / x )

is equivalent to

for ( int j = 2; j <= 80; j += 5 )

Examples Using the for Statement

Vary the control variable from 1 to 100 in increments of 1.

for ( int i = 1; i <= 100; i++ )

Vary the control variable from 100 down to 1 in increments of -1 (that is, decrements of 1).

for ( int i = 100; i >= 1; i-- )

Vary the control variable from 7 to 77 in steps of 7. for ( int i = 7; i <= 77; i += 7 )

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]