Lopp
Lopp
Conditional Structure:
Decision-making in C++ involves the usage of conditional
statements (also called decision control statements) to
execute specific blocks of code primarily based on given
situations and their results.
So basically, in decision-making, we evaluate the conditions
and make a decision about which part of the code should be
executed or not. It allows selective code execution which is
crucial for controlling the flow of a program and making it
more dynamic.
Types of Decision-Making Statements in
C++
In C++, the following decision-making statements are
available:
1. if Statement
2. if-else Statement
3. if-else-if
4. Nested if Statement
5. switch Statement
6. Conditional Operator
7. Jump Statements: break, continue, go, return
i go to school
if i go to school
1. if in C++:
In C++, the if statement is the simplest decision-making statement. It
allows the execution of a block of code if the given condition is true. The
body of the ‘if’ statement is executed only if the given condition is true.
Syntax of if in C++
if (condition) {
// code to be executed if the condition is true
}
Here if the condition is true then the code inside the if block
will be executed otherwise not.
Flowchart of if in C++
Example of if in C++
2. if-else in C++
if (condition) {
// Code to be executed if the condition is true
}
else {
// Code to be executed if the condition is false
}
Flowchart of if-else in C++
Example of if-else in C
The below example demonstrates the use of an if-else statement to find if the
given number is positive or nonpositive.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 5;
Output
number is positive.
3. if-else if in C++
The if-else-if statements allow us to include additional situations after the
preliminary if condition. The ‘else if’ condition is checked only if the above
condition is not true. , and the `else` is the statement that will be executed if none
of the above conditions is true. If some condition is true, then no only the
associated block is executed.
Syntax if-else-if Ladder
if (condition1) {
// code to be executed if condition1 is true
}
else if (condition2) {
// code to be executed if condition2 is true
}
else {
// code to be executed if both the condition is false
}
We can use multiple else if statements with an if-else pair to specify different
conditions.
Flowchart of if-else-if Ladder in C++
flow diagram of the if-else-if ladder
#include <iostream>
using namespace std;
int main()
{
int age = 18;
Output
Growing stage
int main()
{
// initialize variables
int age = 25;
bool isStudent = true;
return 0;
}
Output
You are eligible for a student discount.
Explanation: In the above code, we have used AND operator in the if condition to
check whether the age is greater than 18 and the person is a check. If both
conditions are true, the message “You are eligible for a student discount.” will be
printed. Otherwise, the else statement is executed.
2. Logical OR Operator ( || )
The C++ logical OR operator ( || ) is a binary operator that returns true if at least
one of its operands is true. It returns false only when both operands are false.
Here’s the truth table for the OR operator:
Operand 1 Operand 2 Result
Syntax of Logical OR
expression1 || expression2
int main()
{
int num = 7;
return 0;
}
Output
The number is between 0 to 10.
Explanation: In the above code, the condition num < 0 || num > 10 checks
whether the number is either less than equal to 0 or greater than equal to 10. If
either of these conditions is true, the message “The number is outside the range of
0 to 10.” will be printed otherwise else statement is printed.
3. Logical NOT Operator ( ! )
The C++ logical NOT operator ( ! ) is a unary operator that is used to negate the
value of a condition. It returns true if the condition is false, and false if the
condition is true. Here’s the truth table for the NOT operator:
Operand
1 Result
true false
false true
int main()
{
return 0;
}
Output
Please log in to access this feature.
Explanation: In the above code, the condition ‘!isLoggedIn’ checks whether the
user is not logged in. If the condition is true (i.e., the user is not logged in), the
message “Please log in to access this feature.” will be displayed otherwise else
statement will be printed.
#include <iostream>
using namespace std;
int main()
{
int number = 44;
// to check if number is positive
if (number > 0) {
Output
positive and even number
#include <iostream>
using namespace std;
int main()
{
char input = 'B';
switch (input) {
// if the input character is A then print GFG
case 'A':
cout << "GFG" << endl;
break;
Output
GeeksforGeeks
#include <iostream>
using namespace std;
int main()
{
int num1 = 10, num2 = 40;
int max;
// if the condition is true then num1 will be printed
// else num2 will printed
max = (num1 > num2) ? num1 : num2;
cout << max;
return 0;
}
Output
40
Example
The below example demonstrates the use of breaks to manage the control flow.
C++
// C++ program to use break statement to break the loop when
// i become 3
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++) {
// if i become 3 then break the loop and move to
// next statement out of loop
if (i == 3) {
break;
}
cout << i << endl;
}
// next statements
return 0;
}
Output
0
1
2
B) continue
The continue statement is used to skip the loop body for the current iteration and
continue from the next iteration. Unlike the break statement which terminates the
loop completely, continue allows us just to skip one iteration and continue with
the next iteration.
Syntax
continue;
Flowchart of continue
Flow diagram of continue
Example
The below example demonstrates the use of continue to manage the control flow.
C++
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++) {
// if i become 3 then skip the rest body of loop and
// move next iteration
if (i == 3) {
continue;
}
cout << i << endl;
}
return 0;
}
Output
0
1
2
4
C) goto
It is a jump statement that is used to transfer the control to another part of the
program. It transfers the control unconditionally to the labeled statement.
Syntax
goto label;
// ...
label:
// Statement or block of code
Flowchart of goto
Flow Diagram of goto
Example
The below example demonstrates the use of the goto statement.
C++
#include <iostream>
using namespace std;
int main()
{
int age = 17;
if (age < 18) {
goto Noteligible;
}
else {
cout << "You can vote!";
}
Noteligible:
cout << "You are not eligible to vote!\n";
return 0;
}
Output
You are not eligible to vote!
Note Use of goto is generally avoided in modern programming practices because
it may disturb the readability of the code and make the code error-prone,
although it is still valid and used occasionally.
D) return
The return statement is used to exit the function immediately and optionally
returns a value to the calling function. It returns to the function from where it was
called and if it is the ‘main’ function then it marks the end of the execution. So
basically, return is a mechanism used to communicate the results back to the
calling function.
Syntax
return expression;
Flowchart of return
Flow diagram of return
Example
The below example demonstrates the use of a return statement to return a value
from a function.
C++
#include <iostream>
using namespace std;
return 0;
}
Output
The sum is: 8
As the name suggests, the ternary operator works on three operands where
expression: Condition to be evaluated.
statement_1: Statement that will be executed if the expression evaluates
to true.
statement_2: Code to be executed if the expression evaluates to false.
// image
The above statement of the ternary operator is equivalent to the if-else statement
given below:
if ( condition ) {
statement1;
}
else {
statement2;
}
int main()
{
// creating a variable
int num, test = 40;
return 0;
}
Output
Num - Test = 10
In the above code, we have used the ternary operator to assign the value of the
variable num depending upon the value of another variable named test.
Note: The ternary operator have third most lowest precedence, so we need to use
the expressions such that we can avoid errors due to improper operator
precedence management.
int main()
{
// Initialize variable
int A = 39, B = 10, C = 23;
return 0;
}
Output
Largest number is 39