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
    CC-112
    Progress0 / 39 topics
    Topics
    1. Introduction to Problem Solving, Algorithms, Programming, and C Language2. Problem Solving, a brief review of Von-Neumann Architecture3. The C Programming Language, Pseudo-code, Concept of Variable4. Data types in Pseudo-code, The C Standard Library and Open Source5. Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6. Concept of Integer division, Flowchart and its notations7. Typical C Program Development Environment, Role of Compiler and Linker8. Test Driving C Application9. Introduction to C Programming: A Simple C Program: Printing Text, Adding Two Integer10. Memory Concepts, Arithmetic in C, Operators11. Decision Making: Equality and Relational Operators12. Structured Program Development: The if, if...else, while Nested Control Statements13. Program Control: for, switch, do...while, break, continue, Logical Operators14. Functions: Modularizing Program in C, Math Library Functions15. Function Definitions and Prototypes, Function-Call Stack and Stack Frames16. Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17. Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration18. Arrays: Defining Arrays, Character Arrays, Static and Automatic Local Arrays19. Passing Arrays to Function, Sorting and Searching Arrays20. Multidimensional and Variable Length Arrays21. Pointers: Pointer Definitions and Initialization, Pointer Operators22. Passing Arguments to Function by Reference, Using the const and sizeof Operator23. Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24. Function Pointers25. Characters and Strings: Strings and Characters, Character Handling Library26. String Functions, Library Functions27. Formatted Input/Output: Streams, Formatted Output with printf, Formatted Input with scanf28. Structures: Defining Structures, Accessing Structure Member, Structures and Functions29. typedef, Unions30. Bit Manipulation and Enumeration: Bitwise Operators, Bit Fields, Enumeration Constants31. File Processing: Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File32. Preprocessor: #include, #define, Conditional Compilation, #error and #pragma33. # and ## Operators, Predefined Symbolic Constants, Assertions34. Other Topics: Variable Length Argument List, Using Command Line Arguments35. Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36. Suffixes for Integer and Floating-Point Literals, Signal Handling37. Dynamic Memory Allocation: calloc and realloc, goto38. Advance Topics: Self-Referential Structures, Linked Lists39. Efficiency of Algorithms, Selection and Insertion Sort
    CC-112›Decision Making: Equality and Relational Operators
    Programming FundamentalsTopic 11 of 39

    Decision Making: Equality and Relational Operators

    7 minread
    1,183words
    Intermediatelevel

    Decision Making in C: Equality and Relational Operators

    In C programming, decision making allows you to execute certain blocks of code based on conditions. This is typically achieved using conditional statements like if, else, and switch. The condition used in these decision-making constructs is generally based on the relational and equality operators that compare values and return either true (non-zero) or false (zero).

    Equality Operators in C

    Equality operators are used to compare two values for equality or inequality. They are commonly used in conditional statements to determine whether two expressions have the same value or not.

    Equality Operators

    1. == (Equal to):

      • The == operator checks if two values are equal.
      • It returns true (1) if the values are equal, and false (0) if they are not.
    2. != (Not equal to):

      • The != operator checks if two values are not equal.
      • It returns true (1) if the values are not equal, and false (0) if they are equal.

    Example of Equality Operators in C:

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 10;
    
        if (a == b) {
            printf("a and b are equal.\n");
        } else {
            printf("a and b are not equal.\n");
        }
    
        if (a != b) {
            printf("a and b are not equal.\n");
        } else {
            printf("a and b are equal.\n");
        }
    
        return 0;
    }
    

    Explanation:

    • The first if statement checks whether a is equal to b. Since a is 5 and b is 10, the condition a == b evaluates to false, so the else block executes, printing "a and b are not equal.".
    • The second if statement checks if a is not equal to b. Since a is 5 and b is 10, the condition a != b evaluates to true, and the program prints "a and b are not equal.".

    Output:

    a and b are not equal.
    a and b are not equal.
    

    Relational Operators in C

    Relational operators are used to compare two values to determine their relationship (greater than, less than, greater than or equal to, or less than or equal to).

    Relational Operators

    1. > (Greater than):

      • Returns true (1) if the left operand is greater than the right operand.
      • Returns false (0) if the left operand is not greater than the right operand.
    2. < (Less than):

      • Returns true (1) if the left operand is less than the right operand.
      • Returns false (0) if the left operand is not less than the right operand.
    3. >= (Greater than or equal to):

      • Returns true (1) if the left operand is greater than or equal to the right operand.
      • Returns false (0) if the left operand is less than the right operand.
    4. <= (Less than or equal to):

      • Returns true (1) if the left operand is less than or equal to the right operand.
      • Returns false (0) if the left operand is greater than the right operand.

    Example of Relational Operators in C:

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 10;
    
        if (a > b) {
            printf("a is greater than b.\n");
        } else {
            printf("a is not greater than b.\n");
        }
    
        if (a < b) {
            printf("a is less than b.\n");
        } else {
            printf("a is not less than b.\n");
        }
    
        if (a >= b) {
            printf("a is greater than or equal to b.\n");
        } else {
            printf("a is less than b.\n");
        }
    
        if (a <= b) {
            printf("a is less than or equal to b.\n");
        } else {
            printf("a is greater than b.\n");
        }
    
        return 0;
    }
    

    Explanation:

    • The first if statement checks if a is greater than b. Since a is 5 and b is 10, the condition a > b evaluates to false, so the program prints "a is not greater than b.".
    • The second if statement checks if a is less than b. Since a is 5 and b is 10, the condition a < b evaluates to true, and the program prints "a is less than b.".
    • The third and fourth if statements check whether a is greater than or equal to b, and whether a is less than or equal to b, respectively. Given a is 5 and b is 10, both of these conditions will evaluate to false for greater than or equal, and true for less than or equal.

    Output:

    a is not greater than b.
    a is less than b.
    a is less than b.
    a is less than or equal to b.
    

    Combining Relational and Equality Operators

    You can combine relational and equality operators to form more complex conditions in C. Often, you'll combine them with logical operators (&&, ||, !) to create compound conditions.

    Example of Combining Operators:

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 10, c = 15;
    
        // Check if 'a' is less than 'b' and 'b' is less than 'c'
        if (a < b && b < c) {
            printf("a is less than b, and b is less than c.\n");
        }
    
        // Check if 'a' is equal to 'b' or 'b' is less than 'c'
        if (a == b || b < c) {
            printf("Either a is equal to b, or b is less than c.\n");
        }
    
        return 0;
    }
    

    Explanation:

    • The first if condition checks if a is less than b and b is less than c. Since a = 5, b = 10, and c = 15, this condition evaluates to true, and the program prints "a is less than b, and b is less than c.".
    • The second if condition checks if a is equal to b or b is less than c. Since a != b but b < c, the condition evaluates to true, and the program prints "Either a is equal to b, or b is less than c.".

    Output:

    a is less than b, and b is less than c.
    Either a is equal to b, or b is less than c.
    

    Summary of Equality and Relational Operators in C

    • Equality Operators:

      • ==: Checks if two values are equal.
      • !=: Checks if two values are not equal.
    • Relational Operators:

      • >: Greater than.
      • <: Less than.
      • >=: Greater than or equal to.
      • <=: Less than or equal to.
    • Decision Making: These operators are commonly used in conditional statements like if, else, and switch to decide which code block should be executed based on certain conditions.

    By mastering equality and relational operators, you can create programs that make intelligent decisions based on conditions and data comparisons.

    Previous topic 10
    Memory Concepts, Arithmetic in C, Operators
    Next topic 12
    Structured Program Development: The if, if...else, while Nested Control Statements

    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 time7 min
      Word count1,183
      Code examples0
      DifficultyIntermediate