A comparison gives a true-or-false answer. An if statement uses that answer to decide whether a block of instructions should run.
#include <iostream>
#include <string>
int main() {
int available = 4;
std::string notice = "No places";
if (available > 0) {
notice = "Booking available";
}
std::cout << notice << "\n";
return 0;
}
The condition goes inside parentheses after if. C++ evaluates it when execution reaches that statement. When it is true, the instructions inside the braces run. When it is false, execution skips that block and continues afterward.
Here the condition is true, so the assignment replaces notice with "Booking available". If available were zero, the initial notice would remain. In either case, the output statement after the closing brace runs. That statement is outside the conditional block.
Use braces even for one instruction. They show which work belongs to the decision and make later additions less error-prone. Do not put a semicolon immediately after if (...); the opening brace should follow the condition. The assignment inside the block does end with a semicolon.
Your program starts with the notice "Enough chairs". The if you add should replace that notice only when the count is below 10. Put the block at the TODO between the declaration and the output. Inside its braces, assign to the existing notice without repeating std::string; you want to update the value that the later output reads.
The diagram isolates the two routes through an if. Its separate example prints directly inside the body rather than updating a notice.
Here, seats is 3. The condition is true, so the program prints Space before continuing. With no seats, it skips that statement. Both routes reach the code after the if; only the true route enters the body.
if block that assigns "Bring more chairs" to notice when chairs < 10. The supplied output should print that notice.Edit main.cpp, press Run to inspect the Console, then Submit to check these results.