Chapter 5: Control Flow Statements

Introduction to Control Flow

Control flow statements in C are used to dictate the order in which statements are executed. They enable you to make decisions, repeat tasks, and jump to different parts of the code, making your programs more dynamic and efficient.

Types of Control Flow Statements

  • Decision Making Statements
    • if
    • if-else
    • nested if
    • if-else-if
    • switch
  • Looping Statements
    • for
    • while
    • do-while
  • Jump Statements
    • break
    • continue
    • goto

Decision Making Statements

Decision making statements allow the program to execute certain sections of code based on whether a condition is true or false.

The if Statement

The if statement executes a block of code if a specified condition is true.

#include <stdio.h>

int main() {
int a = 10;

if (a > 5) {
printf("a is greater than 5\n");
}

return 0;
}

The if-else Statement

The if-else statement executes one block of code if the condition is true and another block if the condition is false.

#include <stdio.h>

int main() {
int a = 10;

if (a > 5) {
printf("a is greater than 5\n");
} else {
printf("a is not greater than 5\n");
}

return 0;
}

The nested if Statement

The nested if statement allows multiple conditions to be evaluated by placing if statements inside other if statements.

#include <stdio.h>

int main() {
int a = 10;
int b = 20;

if (a > 5) {
if (b > 15) {
printf("a is greater than 5 and b is greater than 15\n");
}
}

return 0;
}

The if-else-if Ladder

The if-else-if ladder is used to evaluate multiple conditions sequentially.

#include <stdio.h>

int main() {
int a = 10;

if (a == 0) {
printf("a is zero\n");
} else if (a > 0) {
printf("a is positive\n");
} else {
printf("a is negative\n");
}

return 0;
}

The switch Statement

The switch statement selects one of many code blocks to be executed.

#include <stdio.h>

int main() {
int day = 2;

switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Other day\n");
break;
}

return 0;
}

Looping Statements

Looping statements repeatedly execute a block of code as long as a specified condition is true.

The for Loop

The for loop executes a block of code a specified number of times.

#include <stdio.h>

int main() {
for (int i = 0; i < 5; i++) {
printf("i = %d\n", i);
}

return 0;
}

The while Loop

The while loop executes a block of code as long as a specified condition is true.

#include <stdio.h>

int main() {
int i = 0;

while (i < 5) {
printf("i = %d\n", i);
i++;
}

return 0;
}

The do-while Loop

The do-while loop executes a block of code at least once, and then repeatedly executes it as long as a specified condition is true.

#include <stdio.h>

int main() {
int i = 0;

do {
printf("i = %d\n", i);
i++;
} while (i < 5);

return 0;
}

Jump Statements

Jump statements allow the program to jump to a different part of the code.

The break Statement

The break statement terminates the loop or switch statement and transfers control to the statement following the loop or switch.

#include <stdio.h>

int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("i = %d\n", i);
}

return 0;
}

The continue Statement

The continue statement skips the current iteration of the loop and proceeds to the next iteration.

#include <stdio.h>

int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
printf("i = %d\n", i);
}

return 0;
}

The goto Statement

The goto statement transfers control to the labeled statement. Use of goto is generally discouraged as it can make code hard to follow.

#include <stdio.h>

int main() {
int i = 0;

loop:
printf("i = %d\n", i);
i++;
if (i < 5) {
goto loop;
}

return 0;
}

Practical Examples in C

Here are some practical examples of using control flow statements in C.

Example 1: Find the Maximum of Three Numbers

#include <stdio.h>

int main() {
int a = 10, b = 20, c = 15;

if (a >= b && a >= c) {
printf("The largest number is %d\n", a);
} else if (b >= a && b >= c) {
printf("The largest number is %d\n", b);
} else {
printf("The largest number is %d\n", c);
}

return 0;
}

Example 2: Simple Calculator Using switch Statement

#include <stdio.h>

int main() {
char operator;
double firstNumber, secondNumber;

printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &firstNumber, &secondNumber);

switch (operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf\n", firstNumber, secondNumber, firstNumber * secondNumber);
break;
case '/':
if (secondNumber != 0) {
printf("%.2lf / %.2lf = %.2lf\n", firstNumber, secondNumber, firstNumber / secondNumber);
} else {
printf("Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid operator\n");
break;
}

return 0;
}

Example 3: Sum of Natural Numbers Using for Loop

#include <stdio.h>

int main() {
int n, sum = 0;

printf("Enter a positive integer: ");
scanf("%d", &n);

for (int i = 1; i <= n; ++i) {
sum += i;
}

printf("Sum = %d\n", sum);

return 0;
}

Conclusion

Control flow statements are essential for creating dynamic and responsive programs. They allow you to make decisions, repeat tasks, and control the flow of your program efficiently. Mastering these statements is crucial for any C programmer.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *