Boolean Expressions
You can also combine boolean statements. For example, the player is alive in a game while his health_value > 0 AND stamina_value > 0. You can write an expression like this in C++ using the and operator:
main.cpp
12345678910#include <iostream> int main() { short health_value = 50; short stamina_value = 70; if (health_value > 0 and stamina_value > 0) std::cout << "Player is alive!"; }
Let's look at the and operator in greater detail. It returns true if both statements are true. If at least one statement is false, the and operator will return false.
And if you want the player to have health_value > 0 OR stamina_value > 0 you can use the or operator.
main.cpp
12345678910#include <iostream> int main() { short health_value = 0; short stamina_value = 70; if (health_value > 0 or stamina_value > 0) std::cout << "There is still health or stamina left!"; }
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Can you explain the difference between the `and` and `or` operators with more examples?
How do I use these boolean operators in a real C++ program?
What happens if I use more than two conditions with `and` or `or`?
Awesome!
Completion rate improved to 4.35
Boolean Expressions
Deslize para mostrar o menu
You can also combine boolean statements. For example, the player is alive in a game while his health_value > 0 AND stamina_value > 0. You can write an expression like this in C++ using the and operator:
main.cpp
12345678910#include <iostream> int main() { short health_value = 50; short stamina_value = 70; if (health_value > 0 and stamina_value > 0) std::cout << "Player is alive!"; }
Let's look at the and operator in greater detail. It returns true if both statements are true. If at least one statement is false, the and operator will return false.
And if you want the player to have health_value > 0 OR stamina_value > 0 you can use the or operator.
main.cpp
12345678910#include <iostream> int main() { short health_value = 0; short stamina_value = 70; if (health_value > 0 or stamina_value > 0) std::cout << "There is still health or stamina left!"; }
Obrigado pelo seu feedback!