Условный оператор. Циклы. (C): различия между версиями

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску
(Новая страница: «== Условный оператор == <syntaxhighlight lang="c#" line> printf("Please, enter your age:"); string ageString = Console.ReadLine(); int age = Conve...»)
 
 
(не показано 7 промежуточных версий этого же участника)
Строка 1: Строка 1:
 
== Условный оператор ==
 
== Условный оператор ==
 +
* [https://docs.microsoft.com/ru-ru/cpp/c-language/if-statement-c?view=msvc-160 Условный оператор (справка Микрософт)]
 +
* [https://docs.microsoft.com/ru-ru/cpp/c-language/switch-statement-c?view=msvc-160 Оператор выбора (справка Микрософт)]
 +
* [https://docs.microsoft.com/ru-ru/cpp/c-language/conditional-expression-operator?view=msvc-160 Условный тернарный оператор (справка Микрософт)]
 
<syntaxhighlight lang="c#" line>
 
<syntaxhighlight lang="c#" line>
printf("Please, enter your age:");
+
#include <stdio.h>
string ageString = Console.ReadLine();
+
 
int age = Convert.ToInt32(ageString);
+
int main() {
if (age > 65)
+
    int temp;
{
+
    printf("Please enter water temperature in degrees celsius:");
    printf("Here is your pension.");
+
    scanf_s("%d", &temp);
}
+
 
else
+
    if (temp >= 100)
{
+
    {
    printf("Good morning people!");
+
        printf("Water is boiling.");
 +
    }
 +
    else
 +
    {
 +
        printf("Not hot enough.");
 +
    }
 +
 
 +
    char* result = (temp % 2 == 0) ? "Temperature is even" : "Temperature is odd";
 +
    printf("%s", result);
 +
 
 +
    int class;
 +
    printf("Please enter ticket class:");
 +
    scanf_s("%d", &class);
 +
    switch(class)
 +
    {
 +
        case 1:
 +
            printf("This is first class.");
 +
            break;
 +
        case 2:
 +
            printf("This is second class.");
 +
            break;
 +
        case 3:
 +
            printf("This is third class.");
 +
            break;
 +
        default:
 +
            printf("This is unknown class.");
 +
            break;
 +
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Циклы ==
 
== Циклы ==
 +
* [https://docs.microsoft.com/ru-ru/cpp/c-language/while-statement-c?view=msvc-160 while (справка Микрософт)]
 +
* [https://docs.microsoft.com/ru-ru/cpp/c-language/do-while-statement-c?view=msvc-160 do-while (справка Микрософт)]
 +
* [https://docs.microsoft.com/ru-ru/cpp/c-language/for-statement-c?view=msvc-160 for (справка Микрософт)]
 
<syntaxhighlight lang="c#" line>
 
<syntaxhighlight lang="c#" line>
printf("while loop\n");
+
#include <stdio.h>
int i = 1;
+
 
while (i < 11)
+
int main() {
{
+
    printf("while loop\n");
    printf("%d\n", i);
+
    int i = 1;
    i++;
+
    while (i < 11)
}
+
    {
 +
        printf("%d\n", i);
 +
        i++;
 +
    }
  
printf("do while loop\n");
+
    printf("do while loop\n");
i = 1;
+
    i = 1;
do
+
    do
{
+
    {
    printf("%d\n", i);
+
        printf("%d\n", i);
    i++;
+
        i++;
} while (i < 11);
+
    } while (i < 11);
  
printf("for loop\n");
+
    printf("for loop\n");
for (int j = 1; j < 11; j++)
+
    for (int j = 1; j < 11; j++)
{
+
    {
    printf("%d\n", j);
+
        printf("%d\n", j);
 +
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== Задачник ==
 +
* [http://ptaskbook.com/ru/tasks/if.php Условный оператор]
 +
* [http://ptaskbook.com/ru/tasks/case.php Оператор выбора]
 +
* [http://ptaskbook.com/ru/tasks/for.php Цикл с параметром]
 +
* [http://ptaskbook.com/ru/tasks/while.php Цикл с условием]

Текущая версия на 15:26, 9 июня 2021

Условный оператор

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int temp;
 5     printf("Please enter water temperature in degrees celsius:");
 6     scanf_s("%d", &temp);
 7 
 8     if (temp >= 100)
 9     {
10         printf("Water is boiling.");
11     }
12     else
13     {
14         printf("Not hot enough.");
15     }
16 
17     char* result = (temp % 2 == 0) ? "Temperature is even" : "Temperature is odd";
18     printf("%s", result);
19 
20     int class;
21     printf("Please enter ticket class:");
22     scanf_s("%d", &class);
23     switch(class)
24     {
25         case 1:
26             printf("This is first class.");
27             break;
28         case 2:
29             printf("This is second class.");
30             break;
31         case 3:
32             printf("This is third class.");
33             break;
34         default:
35             printf("This is unknown class.");
36             break;
37     }
38 }

Циклы

 1 #include <stdio.h>
 2 
 3 int main() {
 4     printf("while loop\n");
 5     int i = 1;
 6     while (i < 11)
 7     {
 8         printf("%d\n", i);
 9         i++;
10     }
11 
12     printf("do while loop\n");
13     i = 1;
14     do
15     {
16         printf("%d\n", i);
17         i++;
18     } while (i < 11);
19 
20     printf("for loop\n");
21     for (int j = 1; j < 11; j++)
22     {
23         printf("%d\n", j);
24     }
25 }

Задачник