ProgramGuru

C Conditions, if and Logical Operators

C Conditions, if and Logical Operators

A student passes when a mark is at least 40 and attendance is sufficient. Both requirements must hold before printing Passed.

An if statement chooses a branch according to whether its condition is zero or nonzero. Relational operators compare values; && requires both conditions and || accepts either condition.

  1. Explain

    Combine two requirements and predict which branch executes.

  2. Trace

    Keep 45 marks but lower attended days to 12. Predict the result.

  3. Apply

    Choose conditions that include the intended boundary mark.

Conditions: follow the data

  1. Start

    A student passes when a mark is at least 40 and attendance is sufficient. Both requirements must hold before printing Passed.

  2. Operation

    An if statement chooses a branch according to whether its condition is zero or nonzero. Relational operators compare values; && requires both conditions and || accepts either condition.

  3. Result

    Passed

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

C treats zero as false and nonzero as true in a condition. Comparisons such as marks >= 40 produce an integer truth value. && and || evaluate left to right and short-circuit when the right side cannot change the result.

Use == to compare and = to assign. Braces make the branch boundaries clear even when each branch currently contains one statement. && is a logical test, while & is a bitwise operation with different behavior.

Read the complete C program

This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks = 45;
  int attended = 18;
  if (marks >= 40 && attended >= 15) {
    puts("Passed");
  } else {
    puts("Needs review");
  }
  return 0;
}

Expected output

Passed
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks = 45;
  int attended = 18;
  if (marks >= 40 && attended >= 15) {
    puts("Passed");
  } else {
    puts("Needs review");
  }
  return 0;
}

45 >= 40 is true, so the attendance test is evaluated.

18 >= 15 is also true. The program enters the first branch and prints Passed.

Follow the worked calculation

  1. Read the starting values

    45 >= 40 is true, so the attendance test is evaluated.

  2. Perform the operation

    18 >= 15 is also true. The program enters the first branch and prints Passed.

  3. Check the observable result

    Passed

    Passed
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks = 45;
  int attended = 18;
  if (marks >= 40 && attended >= 15) {
    puts("Passed");
  } else {
    puts("Needs review");
  }
  return 0;
}

Keep 45 marks but lower attended days to 12. Predict the result.

Worked answer

The second requirement fails, so && produces false even though the mark is sufficient. The else branch explains that the combined rule was not met.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks = 45;
  int attended = 12;
  if (marks >= 40 && attended >= 15) {
    puts("Passed");
  } else {
    puts("Needs review");
  }
  return 0;
}

Expected output

Needs review
Run in C compiler →

Recognize the important boundaries

A true comparison does not automatically validate that the original mark was in its allowed range. Reject invalid values before applying a pass rule.

Avoid writing 0 <= marks <= 100 in C. Use marks >= 0 && marks <= 100, because comparisons are evaluated as separate expressions.

Check your understanding

Choose the answer, then compare the reasoning for every option.

When does marks >= 40 && attended >= 15 pass?
Choose one answer. You can try again.

Answer and explanation

Only when both comparisons are true.

Correct. && expresses a conjunction of the two requirements.

Use the idea independently

An if statement chooses a branch according to whether its condition is zero or nonzero. Relational operators compare values; && requires both conditions and || accepts either condition.

Continue with repeating operations.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C for, while and Loop Control →