C doesn’t just execute code linearly. It makes choices. At the heart of every decision a C program makes lies the Boolean expression. Whether you are using an if statement or a while loop, the logic boils down to one thing: is this condition true or false?
Let’s look at a basic example.
Here, the program asks for input. It stores that number in variable b. Then it checks a condition. The Boolean expression in question is (b < 0). C evaluates this. If the result is true, the program prints "The value is negative". If it is false, the program does nothing. It stays silent.
The rule is simple. If the expression evaluates to True, C executes the single line immediately following the if. Or, if you use braces {}, it executes the entire block inside. If the expression is False, C skips that line or block entirely.
But programs rarely deal with just one outcome. You often need to handle zero or positive numbers too.
This structure uses else if and else to cover the remaining cases. It tests for zero next. If that fails, it assumes the value is positive.
Sometimes, decisions get complicated. You might need to check multiple conditions at once.
This statement reads like natural language. If x equals y AND j is greater than k, then set z to 1. Otherwise, set q to 10. You will use these kinds of statements throughout your C career. Most decisions will be simple. Occasionally, they will get complex.
Pay close attention to syntax. A common pitfall for beginners is confusing assignment with comparison.
C uses == to test for equality.
C uses = to assign a value to a variable.
Mixing them up causes bugs that are hard to find. One sets a value. The other checks a condition. They are not interchangeable.
C also relies on specific operators for logical operations. The && symbol represents a Boolean AND operation. It ensures both sides of the condition must be true.
Here is a quick reference for all Boolean operators in C:
- == : equality
- != : inequality
- < : less than
- > : greater than
- <= : less than or equal to
- >= : greater than
Looping logic in C: while, do-while, and for
Using a while loop is straightforward. It mirrors the simplicity of a standard if statement. Consider this snippet:
while (a < b) { printf("%d\n", a); a = a + 1; }
This structure executes the two lines inside the braces repeatedly. It stops only when a is greater than or equal to b. The logic is linear. Check condition. Execute body. Repeat.
C also offers a do-while construct. Unlike the standard while loop, this checks the condition after the first execution. This guarantees the code block runs at least once.
Note: The example above uses an if statement within the function. A true do-while would wrap the body and check the condition at the end.
The for loop as a shorthand
The for loop is essentially a compressed while statement. It bundles initialization, testing, and incrementing into one line.
Look at this standard while loop:
x=1;
while (x<10) {
blah blah blah
x++; // x++ is identical to x=x+1
}
You can rewrite this as a for loop:
for(x=1; x<10; x++) { blah blah blah }
The components are clear. x=1 initializes. x<10 tests. x++ increments. The for loop just condenses them. You aren’t limited to simple variables. You can pack complex logic into those slots.
Take this example:
a=1;
b=6;
while (a < b) {
a++;
printf("%d\n",a);
}
It becomes:
for (a=1,b=6; a < b; a++,printf("%d\n",a));
It works. It is also confusing. The comma operator allows multiple statements in the initialization and increment sections. It does not work in the test section. Many C developers love this compactness. Others find it unreadable. They break it up. Both approaches are valid. Choose based on who will maintain the code later.
The danger of = vs. == in C
The == operator causes headaches. Developers frequently type a single = by mistake in Boolean expressions. The compiler accepts both. The behavior differs wildly.
In C, Boolean expressions evaluate to integers. Zero is False. Any non-zero integer is True. This means any integer can exist inside a Boolean context.
Consider this legal C code:
if (a) { printf("Non-zero"); }
If a is anything but 0, the block runs.
Now look at if (a=b). This does not compare values. It assigns b to a, then tests the new value of a. If b is 0, the condition is False. If b is anything else, it is True. The variable a changes. This is likely not what you intended. You probably meant == for comparison.
This feature isn't useless. It has valid use cases. But it is a trap. Be careful with your = and == usage. One character changes logic from comparison to assignment.


































