Control structures control the flow of execution in a program or function. Control structures are used to repeat any block of code, transfer control to a specific block of code, and make a choice by selection. There are three basic control structures in C++
Selection / Decision Making Control Structure
Loops / Iteration Control Structure
Jumps
The selection control structure allows a number of conditions which lead to a selection of one out of several alternatives. There are three types of selection control structure:
If selection structure / statement
If-else selection structure / statement
Switch selection structure / statement
An if statement is a conditional statement that tests a particular condition. Whenever that condition evaluates as true, performs an action, but if it is not true, then the action is skipped.
Syntax:
The general syntax of if statement is:
if (condition)
{
Statement (s);
}
#include<iostream>
using namespace std;
int main( )
{
int num;
cout << “Enter an integer number: “;
cin >> num;
if(num > 0)
{
cout << “You entered a positive integer: ” << num << “\n”;
}
return 0;
}
Output:
Enter an integer number : 10
You entered a positive integer : 10
An if condition can be written as deeply as needed within the body of another statement.
This is called a nested if statement.
The general syntax of nested if statement is:
Copy code
if (condition 1)
{
if (condition 2)
{
statements;
}
}
Copy code
#include<iostream>
using namespace std;
int main( )
{
int exp, status;
cout << “Enter experience: “;
cin >> exp;
cout << “\n Enter status: “;
cin >> status;
if(exp >= 4)
{
if(status >= 2)
{
cout << “\n Bonus Given to Employee” << “\n”;
}
}
return 0;
}
Output:
Enter experience: 6
Enter status: 3
Bonus Given to Employee
An if-else selection structure performs a certain action when the condition is true and some different action when the condition is false.
The general syntax of if-else statement is:
Copy code
if (condition)
{
statement(s);
}
else
{
statement(s);
}
Copy code
#include<iostream>
using namespace std;
int main()
(Note: The text extraction ended mid-sentence, indicating there may be more text following this that was not captured.)
{
int number;
cout << “Enter an integer: “;
cin >> number;
if (number >= 0)
{
cout << “The number is a positive integer: ” << number << “\n”;
}
else
{
cout << “The number is a negative integer: ” << number << “\n”;
}
cout << “This line is always printed.”;
return 0;
}
Output:
Enter an integer: -5
The number is a negative integer: -5
This line is always printed.
Nested if-else statements test for multiple conditions by placing if-else statements inside if-else statements. When a condition is evaluated as true, the corresponding statements are executed and the rest of the structure is skipped. This structure is also referred to as the if-else-if ladder.
else-if SELECTION STATEMENT EXAMPLE
#include<iostream>
using namespace std;
int main()
{
int per;
cout << “\n Enter your percentage: “;
cin >> per;
if (per >= 80)
(Note: The text extraction seems to end mid-sentence, suggesting that there may be more text that was not captured.)
{
cout << “Your grade is A+:”;
}
else if (per >= 70)
{
cout << “Your grade is A:”;
}
else if (per >= 60)
{
cout << “Your grade is B:”;
}
else if (per >= 50)
{
cout << “Your grade is C:”;
}
else
{
cout << “Failed.”;
}
return 0;
}
Output:
Enter your percentage: 70
Your grade is A:
Switch statement is a control statement that allows to select only one choice among the many given choices. The expression in switch evaluates to return an integer or character value, which is then compared to the values present in different cases. It executes that block of codes which matches the case value. If there is no match, then the default block is executed (if present).
Syntax:
The general form of switch statement is,
switch(variable)
{
case constant 1:
{
(Note: The text extraction seems to end mid-sentence, indicating that there may be more text that was not captured.)
statement (s);
break;
}
case constant 2:
{
statement (s);
break;
}
default:
{
statement (s);
break;
}
}
(Note: The extracted text seems to be the latter part of the switch case syntax and does not include the initial ‘switch’ statement or the flow diagram.)
#include<iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout << “Enter an operator (+, -, *, /):”;
cin >> op;
cout << “Enter two numbers: ” << “\n”;
cin >> num1 >> num2;
switch (op)
{
case ‘+’:
cout << num1 <<” +” << num2 <<” = ” << num1 + num2;
break;
case ‘-‘:
cout << num1 <<” -” << num2 <<” = “<< num1 – num2;
break;
case ‘*’:
cout << num1 <<” *” << num2 <<” = ” << num1 * num2;
break;
case ‘/’:
cout << num1 <<“/” << num2<<” = ” << num1 / num2;
break;
default:
cout << “Error! The operator is not correct”;
}
return 0;
}
Output:
Enter an operator: +
Enter two numbers:
10 15
10 + 15 = 25
(Note: There may be some OCR errors in the text such as ‘numd’ which should be ‘num1’ and ‘num?’ which should be ‘num2’. These are corrected in the transcribed code above.)
If-else statement is used to select among two alternatives. The switch statement is used to select among multiple alternatives.
If-else can have values based on constraints. Switch can have values based on user choice.
Float, double, char, int, and other data types can be used in if-else condition. Only int and char data types can be used in switch block.
It is difficult to edit the if-else statement, if the nested if-else statement is used. It is easy to edit switch cases as they are recognized easily.
0 of 10 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 10 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Which control structure in C++ allows for multiple conditions to be tested by placing if-else statements inside each other?
What is the purpose of the if statement in C++?
Which statement accurately describes the nested if-else statement in C++?
What is the role of the switch statement in C++?
Which control structure in C++ is used to select among multiple alternatives based on user input?
What is the purpose of the default case in a switch statement in C++?
Which control structure in C++ is used to select among two alternatives based on a condition?
What is the difference between if-else and switch statement in C++?
Which control structure in C++ is used to perform an action only when a particular condition is true?
What is the primary purpose of using selection control structures in C++ programming?