Foundations
Conditionals
An if statement lets C++ choose between branches.
Conditionals
conditionals.cpp
#include <iostream>
#include <string>
int main() {
int temperature = ;
std::string status = "";
if (temperature >= 80) {
status = "warm";
} else {
status = "comfortable";
}
std::cout << "temperature=" << temperature << std::endl;
std::cout << "status=" << status << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
int temperature = ;
std::string status = "";
if (temperature >= 80) {
status = "warm";
} else {
status = "comfortable";
}
std::cout << "temperature=" << temperature << std::endl;
std::cout << "status=" << status << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
int temperature = ;
std::string status = "";
if (temperature >= 80) {
status = "warm";
} else {
status = "comfortable";
}
std::cout << "temperature=" << temperature << std::endl;
std::cout << "status=" << status << std::endl;
return 0;
}
if statement
An `if` statement runs one block when its condition is true and can use `else` for the other case.