C Programming Tutorial 0/65 lessons ~6 min read Lesson 14
Conditional Statements
Branching chooses which block runs — chained if/else for ranges, switch when matching one integral value against many labels.
Course progress0%
Focus
10 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
Branching chooses which block runs — chained if/else for ranges, switch when matching one integral value against many labels.
Understanding the topic
if / else Execute block when condition is true (non-zero).
switch Match integral expression against case labels; use break and default.
- if / else — Execute block when condition is true (non-zero).
- switch — Match integral expression against case labels; use break and default.
Step-by-step explanation
- if / else — Execute block when condition is true (non-zero).
- switch — Match integral expression against case labels; use break and default.
Syntax reference
Syntax reference:
c
if (condition) { } else { }switch (expr) { case val: break; default: }
Informative example
Example program:
c
#include <stdio.h>int main(void) {int marks = 72;if (marks >= 90) printf("Distinction\n");else if (marks >= 60) printf("Pass\n");else printf("Retake\n");return 0;}
Output
Pass
Execution workflow
1Conditional Statements — step by step
1 / 2if / else
Execute block when condition is true (non-zero).
Best practices
- Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
- Give every variable a defined value before it is read.
- Stay inside array bounds — C will not stop you from over-running a buffer.
Common mistakes
- Reading uninitialized storage — behavior is undefined.
- Dismissing compiler warnings instead of fixing root causes.
- Ignoring NULL returns from malloc, fopen, and similar APIs.
Hands-on exercise
Practice problems:
- Grade calculator with if-else
- Menu driven program with switch
Summary
Conditional Statements in C — if/else chains and switch for branching.
Ready to mark this lesson complete?Track your journey across the entire course.