C++: различия между версиями
Перейти к навигации
Перейти к поиску
м (Безуглов Сергей переименовал страницу C-plus-plus в C++) |
|||
| (не показана 1 промежуточная версия этого же участника) | |||
| Строка 2: | Строка 2: | ||
== Hello World == | == Hello World == | ||
| − | * | + | * [[Hello - C++]] |
== Объявление переменных == | == Объявление переменных == | ||
<syntaxhighlight lang="C++" line> | <syntaxhighlight lang="C++" line> | ||
| + | int i, j, k = 10; | ||
| + | char c, ch = 'a'; | ||
| + | float f, salary = 4.5f; | ||
| + | double d = 100.3; | ||
| + | bool busy = true; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Ввод и вывод == | == Ввод и вывод == | ||
<syntaxhighlight lang="C++" line> | <syntaxhighlight lang="C++" line> | ||
| + | int age; | ||
| + | std::cout << "Please enter your age:"; | ||
| + | std::cin >> age; | ||
| + | std::cout << "Your age = " << age << std::endl; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Условный оператор == | == Условный оператор == | ||
<syntaxhighlight lang="C++" line> | <syntaxhighlight lang="C++" line> | ||
| + | int age; | ||
| + | std::cout << "Please enter your age:"; | ||
| + | std::cin >> age; | ||
| + | if (age > 65) | ||
| + | { | ||
| + | std::cout << "Here is your pension."; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | std::cout << "Good morning people!"; | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Циклы == | == Циклы == | ||
<syntaxhighlight lang="C++" line> | <syntaxhighlight lang="C++" line> | ||
| + | std::cout << "while loop" << std::endl; | ||
| + | int i = 1; | ||
| + | while (i < 11) | ||
| + | { | ||
| + | std::cout << i << std::endl; | ||
| + | i++; | ||
| + | } | ||
| + | |||
| + | std::cout << "do while loop" << std::endl; | ||
| + | i = 1; | ||
| + | do | ||
| + | { | ||
| + | std::cout << i << std::endl; | ||
| + | i++; | ||
| + | } while (i < 11); | ||
| + | |||
| + | printf("for loop\n"); | ||
| + | for (int j = 1; j < 11; j++) | ||
| + | { | ||
| + | std::cout << j << std::endl; | ||
| + | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Текущая версия на 15:27, 27 ноября 2020
Hello World
Объявление переменных
1 int i, j, k = 10;
2 char c, ch = 'a';
3 float f, salary = 4.5f;
4 double d = 100.3;
5 bool busy = true;
Ввод и вывод
1 int age;
2 std::cout << "Please enter your age:";
3 std::cin >> age;
4 std::cout << "Your age = " << age << std::endl;
Условный оператор
1 int age;
2 std::cout << "Please enter your age:";
3 std::cin >> age;
4 if (age > 65)
5 {
6 std::cout << "Here is your pension.";
7 }
8 else
9 {
10 std::cout << "Good morning people!";
11 }
Циклы
1 std::cout << "while loop" << std::endl;
2 int i = 1;
3 while (i < 11)
4 {
5 std::cout << i << std::endl;
6 i++;
7 }
8
9 std::cout << "do while loop" << std::endl;
10 i = 1;
11 do
12 {
13 std::cout << i << std::endl;
14 i++;
15 } while (i < 11);
16
17 printf("for loop\n");
18 for (int j = 1; j < 11; j++)
19 {
20 std::cout << j << std::endl;
21 }