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 PortalCC-112Decision Making Relati…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-112

    Programming Fundamentals

    Progress0 / 39 done
    Topics39
    1Introduction to Problem Solving, Algorithms, Programming, and C Language2Problem Solving, a brief review of Von-Neumann Architecture3The C Programming Language, Pseudo-code, Concept of Variable4Data types in Pseudo-code, The C Standard Library and Open Source5Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6Concept of Integer division, Flowchart and its notations7Typical C Program Development Environment, Role of Compiler and Linker8Test Driving C Application
    9A Simple C Program: Printing Text, Adding Two Integer10Memory Concepts, Arithmetic in C, Operators
    11Equality and Relational Operators
    12The if, if...else, while Nested Control Statements
    13for, switch, do...while, break, continue, Logical Operators
    14Modularizing Program in C, Math Library Functions15Function Definitions and Prototypes, Function-Call Stack and Stack Frames16Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration
    18Defining Arrays, Character Arrays, Static and Automatic Local Arrays19Passing Arrays to Function, Sorting and Searching Arrays20Multidimensional and Variable Length Arrays
    21Pointer Definitions and Initialization, Pointer Operators22Passing Arguments to Function by Reference, Using the const and sizeof Operator23Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24Function Pointers
    25Strings and Characters, Character Handling Library26String Functions, Library Functions
    27Streams, Formatted Output with printf, Formatted Input with scanf
    28Defining Structures, Accessing Structure Member, Structures and Functions29typedef, Unions
    30Bitwise Operators, Bit Fields, Enumeration Constants
    31Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File
    32#include, #define, Conditional Compilation, #error and #pragma33# and ## Operators, Predefined Symbolic Constants, Assertions
    34Variable Length Argument List, Using Command Line Arguments35Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36Suffixes for Integer and Floating-Point Literals, Signal Handling
    37calloc and realloc, goto
    38Self-Referential Structures, Linked Lists39Efficiency of Algorithms, Selection and Insertion Sort
    7 min read
    Topic 11 of 39
    Topic Study Note Illustration
    11

    Decision Making: 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...

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

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals