Beginner · C PROGRAMMING

Conditions and logical operators

What you will learn

Choose a branch using if, else and logical expressions.

Understand the concept

An if statement executes its body when its expression is nonzero. Use comparison operators such as >= and <= to produce a true or false result. The && operator requires both conditions to be true; || accepts either condition.

Read the example

The complete program appears below. Read it first, predict the output, then run it.

Expected output

Check temperature

Practise

Test the values -1, 0, 40 and 41. Explain why the boundary values matter.

Common mistake

Use == to compare equality. A single = assigns a value and can silently change a condition.

Lesson discussion

Log in to participate.

#include <stdio.h>

int main(void) {
    int temperature = 42;
    if (temperature >= 0 && temperature <= 40) {
        puts("Normal range");
    } else {
        puts("Check temperature");
    }
    return 0;
}
PRACTICE LAB

Read. Edit. Run.

C source · .c

Experiment with the program below. Your code and input are sent to the selected compiler only when you run it.

Tab indents · Shift+Tab unindents · Esc then Tab leaves the editor · Ctrl/Cmd+Enter runs · Ctrl/Cmd+S saves a draft

Changes save automatically to your account.

Input / output example

Example input
No input required.
Expected output for the original program
Check temperature
Output will appear here.

Log in to save your progress

Ready when you are

Need help with your laptop or computer?

Send us the model and fault details. Start your repair request online or contact TCS Pune directly.

WA