Unit 6: Control Statements in C | B. Sc Year: III

 

🧭 Introduction

By now, you know how to write C programs, use operators, and handle input/output.
But programs become truly powerful only when they can:

  • Make decisions

  • Repeat tasks

  • Control the flow of execution

This is exactly what control statements do.

Unit 6 teaches you how a C program decides what to execute, how many times, and when to stop. Almost every real-world program uses control statements, making this unit very important for exams.


🔑 Key Concepts (Explained Simply)


1️⃣ What are Control Statements?

Control statements are used to change the normal flow of program execution.

Instead of executing statements one by one, control statements allow a program to:

  • Choose between alternatives

  • Repeat instructions

  • Jump to different parts of code


2️⃣ Types of Control Statements in C

C control statements are divided into three main categories:

  1. Decision statements

  2. Loop (iterative) statements

  3. Jump statements


✅ 1. Decision Statements

Decision statements allow a program to take decisions based on conditions.


🔹 (a) if Statement

Executes a block only if the condition is true.

if (condition) { statements; }

✅ Example:

if (age >= 18) { printf("Eligible to vote"); }

🔹 (b) if–else Statement

Executes one block if true, another if false.

if (condition) { statements; } else { statements; }

✅ Example:

if (num > 0) { printf("Positive number"); } else { printf("Negative number"); }

🔹 (c) switch–case Statement

Used when many choices depend on the value of one variable.

switch (expression) { case value1: statements; break; case value2: statements; break; default: statements; }

✅ Example:

switch (day) { case 1: printf("Sunday"); break; case 2: printf("Monday"); break; default: printf("Invalid day"); }

✅ Difference from if–else:

  • Clearer for multiple conditions

  • Faster execution

  • Requires integer expressions only


✅ 2. Loop / Iterative Statements

Loops are used to repeat a block of code.


🔹 (a) for Loop

Used when the number of iterations is known.

for (initialization; condition; increment) { statements; }

✅ Example:

for (int i = 1; i <= 5; i++) { printf("%d ", i); }

🔹 (b) while Loop

Checks condition before executing the loop body.

while (condition) { statements; }

✅ Example:

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

🔹 (c) do–while Loop

Executes loop body at least once, then checks condition.

do { statements; } while (condition);

✅ Example:

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

✅ Main difference:

  • while → condition first

  • do–while → execution first


✅ 3. Jump Statements

Jump statements transfer control immediately.


🔹 (a) break Statement

Used to terminate a loop or switch.

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

✅ Stops loop when i == 5.


🔹 (b) continue Statement

Skips the current iteration and moves to the next one.

for (int i = 1; i <= 5; i++) { if (i == 3) continue; printf("%d ", i); }

✅ Skips printing 3.


🔹 (c) goto Statement

Transfers control to a labelled statement.

start: printf("Hello\n"); goto start;

⚠️ Not recommended due to poor readability.
✅ Mostly asked as a theory question.


🧩 Step-by-Step Exam Programs


✅ Program 1: Count Odd Numbers from 1 to 50

#include <stdio.h> int main() { int count = 0; for (int i = 1; i <= 50; i++) { if (i % 2 != 0) count++; } printf("Odd numbers count = %d", count); return 0; }

✅ Program 2: Factorial Using while Loop

#include <stdio.h> int main() { int n, fact = 1, i = 1; scanf("%d", &n); while (i <= n) { fact *= i; i++; } printf("Factorial = %d", fact); return 0; }

✅ Program 3: Sum of First n Numbers (do–while)

#include <stdio.h> int main() { int n, i = 1, sum = 0; scanf("%d", &n); do { sum += i; i++; } while (i <= n); printf("Sum = %d", sum); return 0; }

✅ Program 4: Simple Interest Calculation

#include <stdio.h> int main() { float p, r, t, si; scanf("%f %f %f", &p, &r, &t); si = (p * r * t) / 100; printf("Simple Interest = %.2f", si); return 0; }

📝 Important Exam Questions

  1. What are control statements? Explain their types

  2. Differentiate for, while, and do–while loops

  3. What is the use of break and continue?

  4. Difference between if–else and switch

  5. Write a program using:

    • for loop

    • while loop

    • do–while loop

  6. Explain goto statement with example


🎯 Exam Tips / Key Points

  • For theory: define → explain → example

  • For loops: mention initialization, condition, increment

  • Always write proper indentation

  • Use comments when asked

  • do–while executes at least once (very common MCQ)


✅ Short Summary

In Unit 6, you learned how C programs control execution flow using:

  • Decision statements (if, switch)

  • Looping statements (for, while, do–while)

  • Jump statements (break, continue, goto)

This unit is key for logic building, problem-solving, and exam programming questions.

Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers