Module 2: Chapter 3 — Statements

True and False in C

💡 Key Concept

Conditional statements evaluate expressions that return true (non-zero) or false (zero). Based on this evaluation, different code paths are executed.

Example:

#include <stdio.h>
int main() {
  if (5) printf("True! \n");
  if (0) printf("False! \n");
  if (-3) printf("Also True! \n");
  return 0;
}

True!
Also True!

Selection Statements

Introduction to Decision Control / Branching Statements

Types of Branching Statements

(i) Conditional Branch Statements

These statements transfer control based on a specific condition. If the condition is true, one block executes; otherwise, another may.

(ii) Unconditional Branch Statements

These statements transfer control unconditionally, without checking any condition.

if Statement

if flow chart

(if flow chart)

Example 1: Voting Eligibility

#include <stdio.h> void main() {
int age;
printf("Enter the age:");
scanf("%d", &age);
if(age < = 18)
  printf("The person is eligible to vote \n");
if(age < 18)
  printf("The person is not eligible to vote \n");
}

Example 2: Check Even or Odd

#include <stdio.h>
void main() {
int n;
printf("Enter a number:");
scanf("%d", &n);
if(n % 2 = = 0)
  printf("Number is even \n");
if(n % 2 ! = 0)
  printf("Number is odd \n");
}

Example 3: Largest of Two Numbers

#include <stdio.h>
void main() {
int a, b;
printf("Enter two numbers:");
scanf("%d%d", &a, &b);
if(a > b)
 printf("a is greater than b \n");
if(b > a)
 printf("b is greater than a \n");
}

if-else Statement

if else flow chart

(if else flow chart)

Example 1: Voting Eligibility

#include <stdio.h>
void main() {
int age;
printf("Enter the age:");
scanf("%d", &age);
if(age >= 18)
 printf("The person is eligible to vote \n");
else
 printf("The person is not eligible to vote \n");
}

Example 2: Even or Odd

#include <stdio.h>
void main() {
int n;
printf("Enter a number:");
scanf("%d", &n);
if(n % 2 == 0)
 printf("Number is even \n");
else
 printf("Number is odd \n");
}

Example 3: Vowel or Consonant

#include <stdio.h>
void main() {
char ch;
printf("Enter any character:");
scanf("%c", &ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
 printf("Character is a vowel \n");
else
 printf("Character is a consonant \n");
}

Example 4: Leap Year Check

#include <stdio.h>
void main() {
int year;
printf("Enter any year:");
scanf("%d", &year);
if(((year % 4 == 0) && (year % 100 ! = 0)) || (year % 400 = = 0))
 printf("%d is a leap year \n", year);
else
  printf("%d is not a leap year \n", year);
}

Cascaded if-else (if-else-if) Statement
if-else ladder

if-else-if-ladder flowchart

(if-else-if-ladder flowchart)

Example: Grading System

#include <stdio.h>
void main() {
  int marks;
  printf("Enter marks: ");
  scanf("%d", &marks);

  if(marks >= 90)
   printf("Grade: A \n");
 else if(marks >= 75)
   printf("Grade: B \n");
  else if(marks >= 60)
   printf("Grade: C \n");
  else if(marks >= 50)
   printf("Grade: D \n");
  else
  printf("Grade: F \n");
}

Switch Statement

switch-case flowchart

(switch-case flowchart)

Important: The Break Statement

⚠️ Warning: Forgetting Break

If you forget the break statement, execution will "fall through" to the next case, which is usually not desired!

Example 1: Display Grade

#include <stdio.h>
void main() {
 char grade;
 printf("Enter the grade: ");
 scanf("%c", &grade);
 switch(grade) {
  case 'O': printf("Outstanding"); break;
  case 'A': printf("Excellent"); break;
  case 'B': printf("Good"); break;
  case 'C': printf("Fair"); break;
  case 'F': printf("Fail"); break;
  default: printf("Invalid grade");
 }
}

Example 2: Display Day of the Week

#include <stdio.h>
void main() {
 int day;
 printf("Enter any number (1-7): ");
 scanf("%d", &day);

  switch(day) {
   case 1: printf("Sunday"); break;
   case 2: printf("Monday"); break;
   case 3: printf("Tuesday"); break;
   case 4: printf("Wednesday"); break;
   case 5: printf("Thursday"); break;
   case 6: printf("Friday"); break;
   case 7: printf("Saturday"); break;
   default: printf("Invalid input");
 }
}

When to Use Which Conditional Statement?

Statement Best Use Case Advantages Limitations
if Single condition check Simple, straightforward Only handles true case
if-else Two mutually exclusive paths Handles both true and false cases Only two choices
if-else ladder Multiple conditions to check in order Flexible, can handle ranges Can be inefficient with many conditions
switch Single variable with multiple constant values Fast, clean, readable for many choices Only works with integers/chars, constant cases

Iterative Statements (Loops)

for Loop (Counter-Controlled Loop)

flowchart-for-Loop-Flow-Diagram

(flowchart-for-Loop-Flow-Diagram)

Examples:

while Loop

while-Loop

(while-Loop)

while Loop - Syntax

while (condition) {
  Statement(s);
}

Examples:

do-while Loop

Syntax:

do {
  Statements;
} while (test expression);
Statement x;

Working Principle:

flowchart-do-while-Loop

(do-while-Loop)

while-Loop

(Flowchart-while-Loop)

Examples:

Difference between while Loop and do-while Loop

do-while Syntax:
do {
  Statements;
} while(test expression);
Statement x;
Aspect while Loop do-while Loop
1. Type of Control It is an entry-controlled loop (top-testing). It is an exit-controlled loop (bottom-testing).
2. Test Type It is a pre-test loop (condition checked before executing). It is a post-test loop (condition checked after executing).
3. Syntax
while(expression) {
  Statements;
}
do {
  Statements;
} while(expression);
4. Working Principle If the expression is true, the loop body executes.
If the expression is false, the body is skipped and control moves out of the loop.
The body executes at least once, regardless of the condition.
After that, if the expression is true, it repeats; otherwise, it exits.
5. Semicolon Usage The while statement does not end with a semicolon (;). The while statement in do-while ends with a semicolon (;).
6. Example
int i = 0, sum = 0;
while(i <= n) {
  sum = sum + i;
  i = i + 1;
}
int i = 0, sum = 0;
do {
  sum = sum + i;
  i = i + 1;
} while(i <= n);

Nested Loops

Flow chart of nested for loop

(Flow chart of nested for loop)

Example:

Write a C program to print the following pattern:

#include <stdio.h>
void main() {
  int i, j;
  for(i = 1; i <= 5; i++) {
    for(j = 1; j <= i; j++) {
      printf("* ");
    }
    printf("\n");
  }
}

Output:

*
* *
* * *
* * * *
* * * * *

In the above example:

Unconditional Branch Statements: break

c-break-statement

(c-break-statement)

Syntax Examples:

Example Program:

Print numbers 0 to 4 using break:

#include <stdio.h>
void main() {
  int i=0;
  while(i <= 10) {
    if(i==5) break;
    printf("%d\n", i);
    i=i+1;
  }
}

Output:

0
1
2
3
4

Unconditional Branch Statements: continue

c-continue-statement

(c-continue-statement)

Example Program:

Print numbers from 1 to 5, skipping 2:

#include <stdio.h>
void main() {
  int i;
  for(i=1; i<=5; i++) {
    if(i==2) continue;
    printf("%d ", i);
  }
}

Output:

1 3 4 5

Unconditional Branch Statements: goto Statement

Syntax:

goto label;
... statements ...
label:
... statements ...
goto_statement

(goto_statement)

Forward Jump: When control jumps to a label defined below the goto statement.

Backward Jump: When control jumps to a label defined above the goto statement.

Example Program:

Write a C program to calculate the sum of all numbers entered by the user.

#include <stdio.h>
void main() {
  int n, i, sum = 0;
  printf("Enter the number:\\n");
  scanf("%d", &n);
  sum = i = 0;
  top: sum = sum + n;
  i = i + 1;
  if(i <= n) goto top;
  printf("Sum of Series = %d", sum);
}

Expression Statements (skip)

Example Statements:

func();   /* function call */
a = b + c;   /* assignment */
b + f();   /* valid but odd */
;   /* null statement */

Block Statements (skip)

Example Program:

Demonstration of a free-standing block statement:

#include <stdio.h>
int main(void) {
  int i;
  {   /* a free-standing block statement */
    i = 120;
    printf("%d", i);
  }
  return 0;
}