CalcSimIA
ยทยท
Programming LogicBeginnerยท 10 min read

Conditional Statements

Conditional statements allow a program to make decisions. Understand if, else, else-if, logical operators, and how to avoid the most common mistakes with conditionals.

RF

Renato Freitas

Updated on May 5, 2026

Decisions in daily life and in code

'If it is raining, I will take an umbrella; otherwise, I will leave it at home.' This IF-THEN-ELSE structure is the heart of conditional statements in programming.

In pseudocode, this logic reads: IF (raining == true) THEN (take umbrella) ELSE (leave at home). The computer evaluates the condition in parentheses โ€” it is always true or false โ€” and executes the corresponding block.

Without conditional statements, a program would be just a linear sequence of instructions, with no ability to adapt to different situations. All the intelligence in modern software โ€” from streaming recommendations to medical diagnoses โ€” is built on this simple concept.

๐Ÿงฎ Try it yourself โ€” CalcSim

Want more features? Download CalcSim IA app

The if-then structure and its complements

The simplest structure is the standalone 'if': IF the condition is true, execute this block; IF false, simply skip it and continue the program. This pattern is used when there is something optional to do.

The 'else' adds an alternative path: IF true, execute block A; ELSE, execute block B. One of the two will always execute. The 'else-if' (or 'elif') chains multiple conditions: IF condition1... ELSE IF condition2... ELSE IF condition3... ELSE (default).

For example, a grading system: IF grade >= 7 THEN 'passed'; ELSE IF grade >= 5 THEN 'retake'; ELSE 'failed'. Conditions are evaluated in order โ€” when the first true one is found, its block executes and the rest are not tested.

Nested conditions and logical operators

Sometimes a decision depends on multiple simultaneous conditions. For this, there are logical operators: AND, OR, and NOT. With AND, both conditions must be true. With OR, just one needs to be true. NOT inverts the result.

Practical example: to grant premium access, the condition might be 'user has paid AND subscription has not expired'. In pseudocode: IF (paid == true AND expirationDate > today) THEN grant access.

Nested conditions are 'if' statements inside 'if' statements. They are powerful but can make code hard to read if overused. A good practice is to try to 'flatten' the logic using logical operators before resorting to deep nesting.

  • AND: true only if BOTH conditions are true
  • OR: true if AT LEAST ONE condition is true
  • NOT: inverts the value โ€” true becomes false and vice versa
  • Parentheses define precedence: (A AND B) OR C is different from A AND (B OR C)

Comparison operators

Conditions inside an 'if' use comparison operators: equal (==), not equal (!=), greater than (>), less than (<), greater than or equal (>=), less than or equal (<=). Each returns true or false.

A very common error is confusing the assignment operator '=' with the comparison operator '=='. In C, Java, and many other languages, 'if (x = 5)' does not test whether x equals 5 โ€” it assigns 5 to x and always evaluates to true. The correct form is 'if (x == 5)'. Languages like Python treat this as a syntax error, but others silently accept it, creating hard-to-find bugs.

Examples in pseudocode

Here is a complete login validation example: IF (username == 'admin' AND password == 'correct') THEN display 'Welcome' ELSE IF (attempts >= 3) THEN lock account ELSE increment attempts AND display 'Wrong password'.

Another example โ€” discount calculation: IF (purchaseAmount > 500) THEN discount = 15% ELSE IF (purchaseAmount > 200) THEN discount = 10% ELSE IF (purchaseAmount > 100) THEN discount = 5% ELSE discount = 0%. This chain of else-if is called a 'decision ladder' and is very common in commercial systems.

Frequently asked questions

What is the difference between using else-if and several separate if statements?

With else-if, when one condition is true, the remaining ones are not evaluated. With separate if statements, all are evaluated independently. Use else-if when conditions are mutually exclusive (passed/retake/failed). Use separate if statements when each condition is independent and can be true at the same time.

What is short-circuit evaluation in logical operators?

In expressions with AND, if the first condition is false, the computer does not evaluate the second (the result is already false regardless). With OR, if the first is true, the second is not evaluated. This matters when the second condition has side effects or could cause errors.

How many levels of if nesting are acceptable?

More than 3 levels generally indicates the code can be refactored. Techniques such as early return, use of logical operators, and extracting functions help reduce nesting and make the code more readable.

What is switch/case and when should I use it?

Switch/case is an alternative structure to if-else-if for when you compare a variable against multiple fixed possible values. 'SWITCH (dayOfWeek) CASE monday: ... CASE tuesday: ...' is more readable than a long else-if chain when the values are discrete and known.

Was this article helpful?

Rate with stars to help us improve the content.

Sign in to rate this article.

Still have questions?

The AI Professor explains step by step

Ask a question in natural language and get a personalised explanation about Programming Logic โ€” or any other topic.

Prefer to solve it on your phone?

Download the free app โ†’

Keep learning