Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Prog 1-10.docx
Скачиваний:
4
Добавлен:
01.05.2025
Размер:
33.98 Кб
Скачать

Compound Statements (Blocks)

A compound statement consists of zero or more statements enclosed in curly braces ({ }). A compound statement can be used anywhere a statement is expected. Compound statements are commonly called "blocks."

  1. Operators of cylces, cycle with a precondition, cycle with a postcondition , loop controled

Iteration structures (loops)

Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.

The while loop

Its format is: while (expression) statement and its functionality is simply to repeat statement while the condition set in express

ion is true. For example, we are going to make a program to countdown using a while-loop:

// custom countdown using while

#include <iostream>

using namespace std;

Int main ()

{

int n;

cout << "Enter the starting number > ";

cin >> n;

while (n>0) {

cout << n << ", ";

--n;

}

cout << "FIRE!\n";

return 0;\

The do-while loop

Its format is: do statement while (condition); Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled. For example, the following example program echoes any number you enter until you enter 0.

#include <iostream>

using namespace std;

Int main ()

{

unsigned long n;

do {

cout << "Enter number (0 to end): ";

cin >> n;

cout << "You entered: " << n << "\n";

} while (n != 0);

return 0;

The for loop

Its format is: for (initialization; condition; increase) statement; and its main function is to repeat statement while condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increase statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration. It works in the following way:

  1. initialization is executed. Generally it is an initial value setting for a counter variable. This is executed only once.

  2. condition is checked. If it is true the loop continues, otherwise the loop ends and statement is skipped (not executed).

  3. statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.

  4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.

#include <iostream>

using namespace std;

Int main ()

{

for (int n=10; n>0; n--) {

cout << n << ", ";

}

  1. Working with one dimentional arrays

one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.

Here is the syntax template of a one-dimensional array declaration:

DataType ArrayName [ConstIntExpression];

In the syntax template, DataType describes what is stored in each component of the array. Array components may be of any type, but for now we limit our discussion to simple data types (e.g. integral and floating types).ConstIntExpression indicates the size of the array declared. That is, it specifies the number of array components in the array. It must have a value greater than 0. If the value is n, the range of the index values is 0 to n-1. For example, the declaration

int number[50];

creates the number array, which has 50 components, each capable of holding one int value. In other words, the number array has a total of 50 components, all of type int.

  1. Multidimensional array

In some cases, you may want to divide the list in delimited sections. For example, if you create a list of names, you may want part of the list to include family members and another part of the list to include friends. Instead of creating a second list, you can add a second dimension to the list. In other words, you would create a list of a list, or one list inside of another list, although the list is still made of items with common characteristics.

A multidimensional array is a series of arrays so that each array contains its own sub-array(s)

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