Table of Contents
Programming’s core idea of conditional statements enables you to base decisions on specific criteria. These statements allow your code to run different code blocks based on the presence or absence of certain conditions. We’ll go over the fundamentals of conditional statements in this blog post, starting with the common if-else statement and working our way up to more complicated situations.
Operators
- Arithmetic :- +, – , / ,*, % , ++ , —
- Logical :- && , || , !
- Bitwise :- & , | , ^
- Comparison :- > , < , != , ==
Comparison Operators
- Less than: a < b
- Equal to or less than: a <= b
- Greater than: a > b
- More than or equal to: a >= b
- equal to: a == b
- Not Equal to: a!= b
Arithmetic Operators
let assume , a=10 and b=5
Operator | Name | Description | Example | Output |
+ | Addition | It adds 2 variables or values | a + b | 15 |
– | Subtraction | It will subtract one value from other | a – b | 5 |
/ | Divide | It will Divide one value by another | a / b | 2 |
* | Multiply | It will multiply both values | a * b | 50 |
% | Modulus | It will return the remainder of division | b % a | 5 |
++ | Increment | It will increment value by 1 | a++ | 11 |
— | decrement | It will decrement value by 1 | a– | 9 |
Logical Operators
let assume , a=10
Operator | Name | Description | Example | Output |
&& | Logical AND | It will return true only if both conditions are true | a<15 && a >5 | 1 (True) |
|| | Logical OR | It will true if any one of the condition is true | a<15 || a>10 | 1 (True) |
! | NOT | It will reverse the result | !(a < 15||a > 10) | 0 (False) |
Bitwise Operators
- & :- Bitwise AND
- | :- Bitwise OR
- ^ :- Bitwise XOR
a | b | a&b | a|b | a^b |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
Conditional Statements
- Use if statement to declare a code block that will run if a given condition is met.
- If the same condition is false, use else statement to designate a block of code to be executed.
- If the first condition is false, use else if statement to specify a new condition to test.
- To specify numerous alternative code blocks to be executed, use the switch statement.
If – else statement
A block of code is only executed using a “if statement” if a specific condition is satisfied. It enables us to run code conditionally according to whether or not the given condition is true.
Conversely, the “else statement” is an optional addition to the if statement. It indicates what code should be run in the event that the if statement’s condition is not satisfied, or if it is false.
A block of code inside the if block will be executed if the test condition in the if statement is true. The code contained in the else block, if it exists, will be executed if the test condition is false.
#include<iostream>
using namespace std;
int main() {
int age=25;
if (age >= 18) {
cout << "You are an adult." << endl;
} else {
cout << "You are not an adult." << endl;
}
return 0;
}
Output :- You are an adult.
If – else If – else Statement
You’ll frequently come across situations where you need to check multiple conditions as your code gets more complex. Using else if statements will simplify your code rather than having to write numerous independent if statements.
#include <iostream>
using namespace std;
int main() {
int marks = 67;
if (marks < 35) {
cout << "Grade: E" << endl;
} else if (marks >= 35 && marks <= 54) {
cout << "Grade: D" << endl;
} else if (marks >= 55 && marks <= 69) {
cout << "Grade: C" << endl;
} else if (marks >= 70 && marks <= 79) {
cout << "Grade: B" << endl;
} else if (marks >= 80) {
cout << "Grade: A" << endl;
} else {
cout << "Invalid marks entered." << endl;
}
return 0;
}
Output :- C
Short Hand If…Else (Ternary Operator)
Additionally, there is the shorthand if else, which is also referred to as the ternary operator due to its three operands. It can be applied to substitute one line of code for several lines of code. It frequently takes the place of basic if else statements:
#include<iostream>
using namespace std;
int main() {
int age = 25;
string result = (age < 18) ? "You are not an adult." : "You are an adult.";
cout << result;
return 0;
}
Output :- You are an adult.
switch statement
To choose which of several code blocks should be run, use the switch statement.
- The switch expression is evaluated once
- The expression’s value is compared with each case’s value.
- The related block of code is run if there is a match.
#include<iostream>
using namespace std;
int main() {
int day = 6;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
return 0;
}
Output :- Saturday
break Keyword
- C++ exits the switch block when it encounters the break keyword.
- This will put an end to case testing and additional code execution within the block.
- Break statement Can also be used to jump out of a loop that we will learn in upcoming chapter .
Default Keyword
If there is no case match, the default keyword indicates what code should be executed.
You can download C++ for windows Click here