Menu
Coddy logo textTech

Logical Operators Part 3

Part of the Fundamentals section of Coddy's C journey — lesson 23 of 63.

When checking multiple conditions, the program stops checking as soon as it knows the final answer. This is called short-circuit evaluation.

For example:

int x = 0;
int y = 5;
int result = (x != 0) && (y / x > 2);

Here x equals 0, therefore, it will not evaluate y / x > 2. If we were to reverse the order:

int result = (y / x > 2) && (x != 0);

This will result in an error because y will be divided by 0, which is undefined in C. So it is a good practice to think operations through, so you can get the correct and effective, but errorless code.

This technique is used to optimize the evaluation of logical expressions. For example:

int a = 0;
int b = 2;
int c = 3;
int d = 5;
int result = (a > 0 && b < 2) || (c < -5 && d < 10);

In this example, b < 2 and d < 10 will not be evaluated because a > 0 and c < -5 are both false.

challenge icon

Challenge

Easy

Create a program to decide if it's a good day for solar panel energy production

Initialize these variables:

  • isSunny with the value 1 (true)
  • windSpeed with the value 5.4
  • temperature with the value 23
  • solarPanelOutput with the value 9
  • isCloudy with the value 0 (false)

Create one logical expression that checks ALL of these conditions:

  • It's sunny
  • The wind speed is less than 10
  • The solar panel output is less than 15
  • The temperature is above 20 OR there are no clouds

Print "Good day for solar energy" if all conditions are met, otherwise print "Not ideal for solar energy".

Cheat sheet

Short-circuit evaluation stops checking conditions as soon as the final result is determined, optimizing logical expression evaluation.

With && (AND), if the first condition is false, the second won't be evaluated:

int x = 0;
int result = (x != 0) && (y / x > 2);  // y/x won't be evaluated

With || (OR), if the first condition is true, the second won't be evaluated:

int result = (a > 0 && b < 2) || (c < -5 && d < 10);
// If a > 0 is false, b < 2 won't be evaluated
// If the first part is true, the second part won't be evaluated

Order matters - place conditions that might cause errors (like division by zero) after safety checks:

// Safe: checks x != 0 first
int result = (x != 0) && (y / x > 2);

// Unsafe: might divide by zero
int result = (y / x > 2) && (x != 0);

Try it yourself

#include <stdio.h>

int main() {
    // Initialize variables
    int isSunny = 1;
    float windSpeed = 5.4;
    int temperature = 23;
    int solarPanelOutput = 9;
    int isCloudy = 0;
    
    // Create the logical expression
    int isGoodDay = 
    
    // Don't change below
    if (isGoodDay) {
        printf("Good day for solar energy\n");
    } else {
        printf("Not ideal for solar energy\n");
    }
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals