2. Operators and conditional statement in C++

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

  1. Arithmetic :- +, – , / ,*, % , ++ , —
  2. Logical :- && , || , !
  3. Bitwise :- & , | , ^
  4. 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

OperatorNameDescriptionExampleOutput
+AdditionIt adds 2 variables or valuesa + b15
SubtractionIt will subtract one value from othera – b5
/DivideIt will Divide one value by anothera / b2
*MultiplyIt will multiply both valuesa * b50
%ModulusIt will return the remainder of divisionb % a5
++IncrementIt will increment value by 1a++11
decrement It will decrement value by 1a–9

Logical Operators

let assume , a=10

OperatorNameDescriptionExampleOutput
&&Logical ANDIt will return true only if both conditions are truea<15 && a >51 (True)
||Logical ORIt will true if any one of the condition is truea<15 || a>101 (True)
!NOTIt will reverse the result!(a < 15||a > 10)0 (False)

Bitwise Operators

  • & :- Bitwise AND
  • | :- Bitwise OR
  • ^ :- Bitwise XOR

aba&b a|b a^b
00000
01011
10011
11110

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.

  1. The switch expression is evaluated once
  2. The expression’s value is compared with each case’s value.
  3. 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

Zebronics War Gaming Keyboard and Mouse Combo,Gold Plated USB, Braided Cable,Multicolour LEDs/Gaming Mouse with breathing LEDs and 3200 DPI



Leave a Comment