ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP1112Arithmetic Comparison …
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    3 min read
    Topic 8 of 19
    Topic Study Note Illustration
    08

    Arithmetic, Comparison and Logical Operators

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    531words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals