ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Arithmetic, Comparison and Logical Operators
    Programming FundamentalsTopic 8 of 19

    Arithmetic, Comparison and Logical Operators

    3 minread
    531words
    Beginnerlevel

    Arithmetic, Comparison, and Logical Operators in C++

    Operators are special symbols used in programming to perform operations on variables and values. In C++, operators are categorized into various types, including arithmetic, comparison, and logical operators. Here’s a detailed overview of each type.

    1. Arithmetic Operators

    Arithmetic operators are used to perform basic mathematical operations on numeric values.

    Operator Description Example
    + Addition a + b
    - Subtraction a - b
    * Multiplication a * b
    / Division a / b
    % Modulus (Remainder) a % b

    Examples:

    #include <iostream>
    using namespace std;
    
    int main() {
        int a = 10, b = 3;
        cout << "Addition: " << (a + b) << endl;      // 13
        cout << "Subtraction: " << (a - b) << endl;   // 7
        cout << "Multiplication: " << (a * b) << endl; // 30
        cout << "Division: " << (a / b) << endl;       // 3 (integer division)
        cout << "Modulus: " << (a % b) << endl;        // 1
        return 0;
    }
    

    2. Comparison Operators

    Comparison operators are used to compare two values. They return a boolean value (true or false) based on the result of the comparison.

    Operator Description Example
    == Equal to a == b
    != Not equal to a != b
    > Greater than a > b
    < Less than a < b
    >= Greater than or equal to a >= b
    <= Less than or equal to a <= b

    Examples:

    #include <iostream>
    using namespace std;
    
    int main() {
        int a = 10, b = 20;
        cout << "Equal: " << (a == b) << endl;          // false (0)
        cout << "Not Equal: " << (a != b) << endl;      // true (1)
        cout << "Greater: " << (a > b) << endl;         // false (0)
        cout << "Less: " << (a < b) << endl;            // true (1)
        cout << "Greater or Equal: " << (a >= b) << endl; // false (0)
        cout << "Less or Equal: " << (a <= b) << endl;   // true (1)
        return 0;
    }
    

    3. Logical Operators

    Logical operators are used to perform logical operations on boolean values. They are commonly used in conditional statements.

    Operator Description Example
    && Logical AND a && b
    ` `
    ! Logical NOT !a

    Examples:

    #include <iostream>
    using namespace std;
    
    int main() {
        bool a = true, b = false;
        
        cout << "Logical AND: " << (a && b) << endl;  // false (0)
        cout << "Logical OR: " << (a || b) << endl;   // true (1)
        cout << "Logical NOT: " << (!a) << endl;       // false (0)
        
        // Combining logical operators
        cout << "Combined: " << (a && !b) << endl;     // true (1)
        
        return 0;
    }
    

    Summary

    • Arithmetic Operators: Used for performing mathematical operations.
    • Comparison Operators: Used for comparing values, returning boolean results.
    • Logical Operators: Used for combining or negating boolean values, essential for control flow in conditions.

    Understanding these operators is crucial for controlling the flow of your program and performing necessary calculations, making them foundational elements in C++ programming.

    Previous topic 7
    Input/Output Constructs
    Next topic 9
    Conditional Statements and Execution Flow

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count531
      Code examples0
      DifficultyBeginner