freeCodeCamp/guide/russian/c/short-circuit-evaluation/index.md

64 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: Short-Circuit Evaluation
localeTitle: Оценка короткого замыкания
---
# Оценка короткого замыкания
Оценка короткого замыкания заключается в проверке или выполнении второго аргумента, только если первый аргумент недостаточен для определения значения выражения.
Вы можете выполнить оценку короткого замыкания с помощью && и || операторы.
## Пример с оператором &&:
```c
canOpenFile(filename) && openFile(filename); // If you can open the file then open it.
```
Приведенный выше пример эквивалентен:
```c
if ( canOpenFile(filename) ) {
openFile(filename);
}
```
## Пример с || оператор:
```c
isServerOn || startServer(); // If the server is not on then start it.
```
The example above is equivalent to:
```c
if ( !isServerOn ) {
startServer();
}
```
## Keep it all together in real example
```c
#include <stdio.h>
#include <stdlib.h>
char *getName();
int main(int argc, char *argv[]) {
// Get first argument passed via terminal
char *name = argv[1];
// If name is not passed via terminal then print message and then get the name
name || printf("Please give me your name:") && (name = getName());
printf("Hello %s\n", name);
}
char *getName() {
// Allocate memory
char *name = malloc(30);
scanf("%s", name);
return name;
}
```