Course Topics
C Basics Introduction and Setup Syntax and Program Structure Comments and Documentation Compiling and Running C Programs Exercise Variables and Data Types Variables and Declaration Data Types (int, float, char, double) Constants and Literals Type Conversion and Casting Exercise Operators Arithmetic Operators Comparison Operators Logical Operators Assignment Operators Bitwise Operators Exercise Input and Output Standard Input/Output (scanf, printf) Format Specifiers File Input/Output Exercise Control Flow - Conditionals If Statements If-Else Statements Switch Statements Nested Conditionals Exercise Control Flow - Loops For Loops While Loops Do-While Loops Loop Control (break, continue) Nested Loops Exercise Functions Defining Functions Function Parameters and Arguments Return Statements Scope and Variables Recursion Exercise Arrays One-Dimensional Arrays Multi-Dimensional Arrays Array Operations Strings as Character Arrays Exercise Pointers Introduction to Pointers Pointer Arithmetic Pointers and Arrays Pointers and Functions Dynamic Memory Allocation Exercise Strings String Handling String Functions (strlen, strcpy, strcmp) String Manipulation Exercise Structures Defining Structures Structure Members Arrays of Structures Pointers to Structures Exercise File Handling Opening and Closing Files Reading from Files Writing to Files File Positioning Exercise Memory Management Static vs Dynamic Memory malloc() and free() Memory Leaks Best Practices Exercise Advanced Topics Preprocessor Directives Macros Header Files Modular Programming Exercise Final Project Project Planning Building Complete Application Code Organization Testing and Debugging Exercise

If-Else Statements

Introduction

Make yourself comfortable as we delve into the world of structured decision making in C programming—If-Else Statements. This topic is essential to understand conditional logic and write efficient code. You'll learn how to compare values, make decisions based on those comparisons, and structure your code accordingly. Let's get started!

Core Concepts

An If Statement in C allows you to test a condition and execute a block of code if that condition is true. Here's the basic syntax:

if (condition) {
   // Code to be executed if condition is true
}

To handle multiple possibilities, we can use Else-If Statements. The structure looks like this:

if (condition1) {
   // Code for condition 1
} else if (condition2) {
   // Code for condition 2
} ... else if (conditionN) {
   // Code for condition N
} else {
   // Default code when all conditions are false
}

Note: The conditions are tested from top to bottom, and the first one that evaluates to true will be executed. If none of the conditions is true, the default code in the else clause will run.

Practical Examples

Let's consider a simple example where we check if an age is eligible for voting:

#include <stdio.h>

int main() {
   int age;
   printf("Enter your age: ");
   scanf("%d", &age);

   if (age >= 18) {
      printf("You are eligible to vote.\n");
   } else {
      printf("Sorry, you're not old enough to vote.\n");
   }

   return 0;
}

In this example, we ask the user for their age and check if it is equal to or greater than 18. If so, we inform them they can vote. Otherwise, we let them know that they are not old enough.

Common Issues and Solutions (CRITICAL SECTION)

Compile Error

What causes it: Typographical errors in syntax

# Bad code example that triggers the error
if(age >= 18)
   printf("You are eligible to vote.\n");

Error message:

example.c: In function 'main':
example.c:7:5: error: expected ';' before 'printf'
   printf("You are eligible to vote.\n");
     ^

Solution: Make sure to include semicolons at the end of each statement, even inside if-else blocks.

# Corrected code
if (age >= 18) {
   printf("You are eligible to vote.\n");
}

Why it happens: The compiler expects a semicolon at the end of every statement, and omitting it leads to an error.
How to prevent it: Always include a semicolon at the end of each C statement.

Logic Error

What causes it: Mixing up if and else-if conditions

# Bad code example that triggers the logic error
if (age >= 18) {
   printf("You are not eligible to vote.\n");
} else if (age < 18) {
   printf("You are eligible to vote.\n");
}

Solution: Swap the conditions in the if and else-if statements.

# Corrected code
if (age >= 18) {
   printf("You are eligible to vote.\n");
} else if (age < 18) {
   printf("You are not old enough to vote.\n");
}

Why it happens: The conditions were tested in reverse order, leading to the wrong result.
How to prevent it: Always test the easiest or most likely condition first, followed by less probable ones.

Best Practices

  • Use consistent indentation to make your code easier to read and understand.
  • Avoid nesting if statements excessively—use functions or other structuring techniques when necessary.
  • Keep conditions as simple as possible for clarity and maintainability.

Key Takeaways

  • If-Else Statements allow you to test conditions and execute different blocks of code based on the result.
  • Use Else-If Statements to handle multiple possibilities in an ordered manner.
  • Be mindful of syntax errors, logic mistakes, and code readability when using If-Else Statements.
  • Practice writing clear, concise, and efficient code for better maintainability.

Next steps: Learn about Switch Statements and Loops to further enhance your understanding of control structures in C programming! Happy coding!