Knowlet

Unit 2: Operators and Expressions

Simple Computer Programs

A "simple computer program" in C typically follows a basic structure. It's the skeleton into which you place your logic.

The most basic C program ("Hello, World!") is:

// 1. Preprocessor Directive: Includes the Standard I/O library #include <stdio.h> // 2. The main function: Entry point of every C program int main() { // 3. Program Logic: A function call to print to screen printf("Hello, World!\n"); // 4. Return Statement: Informs the OS that the program finished successfully return 0; } 

All programs in this course will be built inside this main() { ... } block.

Relational Operators

Relational operators are used to **compare** two values. They are the foundation of decision-making in C. The result of a comparison is always a Boolean value: 1 (for true) or 0 (for false).

Operator Meaning Example Result (if x=5, y=10)
== Equal to x == y 0 (false)
!= Not equal to x != y 1 (true)
< Less than x < y 1 (true)
> Greater than x > y 0 (false)
<= Less than or equal to x <= 5 1 (true)
>= Greater than or equal to x >= y 0 (false)
CRITICAL: Do not confuse the assignment operator (=) with the equality operator (==).
  • x = 5; Assigns the value 5 to x.
  • x == 5; Compares x to 5, resulting in 1 (true) or 0 (false).
Writing if (x = 5) instead of if (x == 5) is a very common bug.

Logical Operators

Logical operators are used to combine or invert the results of relational expressions. They are used to build complex logical statements.

Operator Name Meaning Example (A=1, B=0)
&& Logical AND True only if both operands are true (non-zero). A && B is 0 (false)
|| Logical OR True if at least one operand is true (non-zero). A || B is 1 (true)
! Logical NOT Inverts the truth value of its operand. True becomes false, false becomes true. !A is 0 (false)
!B is 1 (true)

Logical Expression and Statements

A logical expression is any expression that evaluates to either true (1) or false (0). We create them by combining relational and logical operators.

A logical statement is a programming construct that uses a logical expression to make a decision. The most common is the if statement (covered in Unit 3).

Examples of Logical Expressions:

1. Check if age is between 18 and 65:

(age >= 18) && (age <= 65)

2. Check if grade is 'A', 'B', or 'C':

(grade == 'A') || (grade == 'B') || (grade == 'C')

3. Check if number is NOT zero:

number != 0 (This is equivalent to !(number == 0))

Short-Circuit Evaluation:
C is efficient. When it evaluates a logical expression, it stops as soon as it knows the answer.

  • For expr1 && expr2: If expr1 is false, C doesn't even look at expr2, because the whole expression must be false.
  • For expr1 || expr2: If expr1 is true, C doesn't even look at expr2, because the whole expression must be true.

Example Program (Unit 2)

This program demonstrates relational and logical operators by checking if a number is positive and even.

#include <stdio.h> int main() { int num; int is_positive, is_even; printf("Enter an integer: "); scanf("%d", &num); // Relational operators  is_positive = (num > 0); // Will be 1 (true) or 0 (false) is_even = (num % 2 == 0); // Will be 1 (true) or 0 (false) printf("Number is positive: %d\n", is_positive); printf("Number is even: %d\n", is_even); // Logical expression  if (is_positive && is_even) { printf("Result: The number is positive AND even.\n"); } else if (is_positive || is_even) { printf("Result: The number is positive OR even (or both).\n"); } else { printf("Result: The number is NOT positive and NOT even.\n"); } // Logical NOT  if (!(num == 0)) { printf("The number is not zero.\n"); } return 0; } 

Unit 2: Exam Quick Tips

  • = vs ==: This is the #1 mistake. = is for assignment, == is for comparison. Always double-check this in your if statements.
  • Truth Values: In C, 0 is false. Any other number (positive or negative) is true. The operators always *return* 1 for true and 0 for false.
  • Operator Precedence: Relational operators (<, >) are evaluated *before* equality operators (==, !=), which are evaluated *before* logical operators (&&, ||).
    Example: x > 5 && y == 10 is grouped as (x > 5) && (y == 10). Use parentheses ( ) to be safe and clear.

Did this resource help you study?

Share feedback or report issues to help improve this resource.