Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1 — Control Structures

Lesson 6 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Control Structures in C

Control structures determine the flow of execution of a program.

---

1. if Statement

if (condition) {
    /* executes if condition is true */
}

2. if-else Statement

if (condition) {
    /* true block */
} else {
    /* false block */
}

3. if-else-if Ladder

if (marks >= 90) {
    printf("Grade A");
} else if (marks >= 75) {
    printf("Grade B");
} else if (marks >= 60) {
    printf("Grade C");
} else {
    printf("Fail");
}

4. Nested if

if (a > 0) {
    if (b > 0) {
        printf("Both positive");
    }
}

---

5. switch Statement

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

Example:

int day = 3;
switch (day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    case 3: printf("Wednesday"); break;
    default: printf("Other day");
}
Note: Without break, execution falls through to the next case.

---

6. while Loop

while (condition) {
    /* loop body */
}
int i = 1;
while (i <= 5) {
    printf("%d ", i);
    i++;
}
/* Output: 1 2 3 4 5 */

---

7. do-while Loop

do {
    /* loop body — executes at least once */
} while (condition);
int i = 1;
do {
    printf("%d ", i);
    i++;
} while (i <= 5);

---

8. for Loop

for (initialisation; condition; update) {
    /* loop body */
}
for (int i = 1; i <= 5; i++) {
    printf("%d ", i);
}

---

Comparison: while vs do-while vs for

Featurewhiledo-whilefor
Condition checkBefore loopAfter loopBefore loop
Minimum executions010
Best used whenUnknown iterationsAt-least-onceKnown iterations

---

Nested Loops Example — Multiplication Table

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        printf("%d\t", i * j);
    }
    printf("\n");
}