Условный оператор. Циклы. (C)

Материал из Информационная безопасностя
Перейти к навигации Перейти к поиску

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

 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 }

Задачник