freeCodeCamp/guide/russian/c/if/index.md

62 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: If
localeTitle: Если
---
# Если
Оператор if выполняет различные блоки кода на основе условий.
```
if (condition) {
// Do something when `condition` is true
}
else {
// Do something when `condition` is false
}
```
Когда `condition` истинно, код внутри , `if` это секция выполняется, в противном случае `else` выполняется. Иногда вам нужно добавить второе условие. Для удобочитаемости вы должны использовать `else if` не вложенные инструкции `if` .
```
if (condition) {
// Do something if `condition` is true
}
else if (anotherCondition) {
// Do something if `anotherCondition` is ture
}
else {
// Do something if `condition` AND `anotherCondition` is false
}
```
Обратите внимание, что разделы `else` и `else if` не требуются, а `if` это обязательно.
## пример
```
#include <stdio.h>
int main () {
// Local variable definition
int a = 10;
// Check the boolean condition
if(a < 5) {
// If condition is true then print the following
printf("a is less than 5!\n" );
}
else {
// If condition is false then print the following
printf("a is not less than 5!\n" );
}
printf("Value of a is : %d\n", a);
return 0;
}
```
## Вывод
```
-> a is not less than 5!
-> Value of a is : 100
```