Skip to main content

C Decision Making

Decision making is a fundamental concept in programming that allows your code to make choices based on certain conditions. In C, decision making constructs help you execute different code blocks based on whether specified conditions evaluate to true or false.

The if Statement

The if statement is the most basic decision-making statement. It evaluates a condition and executes a block of code only if the condition is true.

Syntax

c
if (condition) {
// code to execute if condition is true
}

Example

c
#include <stdio.h>

int main() {
int number = 10;

if (number > 0) {
printf("The number is positive.\n");
}

return 0;
}

In this example, the message "The number is positive" is printed because the condition number > 0 evaluates to true.

tip

For single-statement blocks, the curly braces {} are technically optional. However, it's considered good practice to always include them for readability and to prevent bugs when adding more statements later.

The if-else Statement

The if-else statement allows you to execute one block of code if the condition is true, and another block if it's false.

Syntax

c
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}

Example

c
#include <stdio.h>

int main() {
int number = -5;

if (number >= 0) {
printf("The number is positive or zero.\n");
} else {
printf("The number is negative.\n");
}

return 0;
}

In this example, "The number is negative" is printed because the condition number >= 0 evaluates to false.

The if-else if-else Ladder

When you need to check multiple conditions, you can use the if-else if-else ladder.

Syntax

c
if (condition1) {
// code executed if condition1 is true
} else if (condition2) {
// code executed if condition1 is false but condition2 is true
} else if (condition3) {
// code executed if condition1 and condition2 are false but condition3 is true
} else {
// code executed if all conditions are false
}

Example

c
#include <stdio.h>

int main() {
int score = 75;

if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}

return 0;
}

In this example, "Grade: C" is printed because the score is 75, which falls within the 70-79 range.

Nested if Statements

You can place an if statement inside another if or else block. This is known as nesting.

Syntax

c
if (condition1) {
// outer if block
if (condition2) {
// nested if block
}
}

Example

c
#include <stdio.h>

int main() {
int age = 25;
int hasLicense = 1; // 1 for true, 0 for false

if (age >= 18) {
printf("You are an adult.\n");

if (hasLicense) {
printf("You can drive.\n");
} else {
printf("You need a license to drive.\n");
}
} else {
printf("You are a minor.\n");
}

return 0;
}

In this example, both "You are an adult" and "You can drive" are printed because both conditions evaluate to true.

Ternary Operator (? :)

C also provides a shorthand decision-making operator called the ternary operator, which can be used for simple if-else conditions.

Syntax

c
condition ? expression_if_true : expression_if_false;

Example

c
#include <stdio.h>

int main() {
int number = 7;

// Using if-else
if (number % 2 == 0) {
printf("Using if-else: Even\n");
} else {
printf("Using if-else: Odd\n");
}

// Using ternary operator
printf("Using ternary: %s\n", (number % 2 == 0) ? "Even" : "Odd");

return 0;
}

Common Logical Operators in Conditions

In C, you can use logical operators to combine multiple conditions:

  • && (AND): Both conditions must be true
  • || (OR): At least one condition must be true
  • ! (NOT): Inverts the condition

Example

c
#include <stdio.h>

int main() {
int age = 25;
int income = 50000;

// Using AND operator
if (age > 21 && income > 30000) {
printf("You qualify for the premium credit card.\n");
}

// Using OR operator
if (age < 18 || age > 65) {
printf("You get a discount.\n");
} else {
printf("No age-based discount available.\n");
}

// Using NOT operator
if (!(age < 18)) {
printf("You are an adult.\n");
}

return 0;
}

Best Practices

  1. Always use braces - Even for single-statement blocks, using braces improves readability and prevents bugs.
  2. Indent properly - Consistent indentation makes your code much easier to read.
  3. Avoid deep nesting - Too many nested if statements make code hard to understand. Consider refactoring or using functions.
  4. Simplify conditions - Use logical operators to make conditions clearer.
  5. Be careful with comparison operators - Remember that = is assignment, while == is comparison.

Summary

C's decision-making constructs allow your program to execute different code blocks based on conditions:

  • Use if when you want to execute code only when a condition is true
  • Use if-else when you have two alternative code blocks
  • Use if-else if-else for multiple conditions
  • Use nested if statements for complex decision making
  • Use the ternary operator for simple conditional assignments

Understanding these decision-making structures is essential for writing efficient and logical C programs.



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)