If Expression
To perform different actions depending on the different decisions we can use already mentioned logical operators and the conditional statement if.
if executes the block of code if the condition is true. In C++, it's implemented as follows:
if (condition) {
// code here will be executed if the condition is true
}
For example, we know how to check if the variable x is greater than y. Let’s write the message (with the statemant if) if this condition is true (x is bigger than y):
#include <iostream>
using namespace std;
int main() {
// Define variables
int x = 7;
int y = 5;
// Check the condition
if (x > y) {
cout << "x is bigger than y";
}
return 0;
}
As x is 7, and y is 5, x is greater than y, so our condition is true and the code will print the message "x is bigger than y".
The statement
ifis case sensitive. It means that usage of uppercase letters (IforIF) will cause an error.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Still meg spørsmål om dette emnet
Oppsummer dette kapittelet
Vis eksempler fra virkeligheten
Awesome!
Completion rate improved to 3.7
If Expression
Sveip for å vise menyen
To perform different actions depending on the different decisions we can use already mentioned logical operators and the conditional statement if.
if executes the block of code if the condition is true. In C++, it's implemented as follows:
if (condition) {
// code here will be executed if the condition is true
}
For example, we know how to check if the variable x is greater than y. Let’s write the message (with the statemant if) if this condition is true (x is bigger than y):
#include <iostream>
using namespace std;
int main() {
// Define variables
int x = 7;
int y = 5;
// Check the condition
if (x > y) {
cout << "x is bigger than y";
}
return 0;
}
As x is 7, and y is 5, x is greater than y, so our condition is true and the code will print the message "x is bigger than y".
The statement
ifis case sensitive. It means that usage of uppercase letters (IforIF) will cause an error.
Takk for tilbakemeldingene dine!