Mark As Completed Discussion

Control statements are an essential part of programming in C++. They allow you to make decisions in your project based on specific conditions. One such control statement is the if-else statement.

The if-else statement allows you to execute a block of code if a certain condition is true, and a different block of code if the condition is false. Here's an example:

TEXT/X-C++SRC
1#include <iostream>
2using namespace std;
3
4int main() {
5  int age;
6
7  cout << "Enter your age: ";
8  cin >> age;
9
10  if (age >= 18) {
11    cout << "You are eligible to vote.";
12  } else {
13    cout << "You are not eligible to vote.";
14  }
15
16  return 0;
17}

In this example, the user is prompted to enter their age. If the age is greater than or equal to 18, the program displays the message "You are eligible to vote." If the age is less than 18, the program displays the message "You are not eligible to vote.".

Control statements like the if-else statement allow you to add logic and make your program more intelligent and responsive to different situations. They play a crucial role in creating complex decision-making processes in your C++ projects.

CPP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment