C language chapter 3

🟩 Chapter 3: Control Statements (Decision Making & Looping)


🔹 1. What Are Control Statements?

They control the flow of execution of a program.

Types:

  1. Decision Making
  2. Looping (Iteration)
  3. Jumping

🔹 2. Decision-Making Statements

A. if Statement

if (a > b)
    printf("A is greater");

B. if–else

if (a > b)
    printf("A");
else
    printf("B");

C. if–else if–else Ladder

if (marks >= 90)
    grade = 'A';
else if (marks >= 75)
    grade = 'B';
else
    grade = 'C';

D. Nested if

if (a > 0) {
    if (a % 2 == 0)
        printf("Positive even");
}

E. switch Statement

switch(choice) {
    case 1: printf("Add"); break;
    case 2: printf("Subtract"); break;
    default: printf("Invalid");
}

🔹 3. Looping Statements

Used for repetition of code.

A. while Loop

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

B. do–while Loop

Executes at least once.

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

C. for Loop

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

D. Nested Loop

Loop inside another loop.

for (i = 1; i <= 3; i++) {
    for (j = 1; j <= 3; j++) {
        printf("* ");
    }
    printf("\n");
}

🔹 4. Jump Statements

  • break → exit from loop or switch
  • continue → skip current iteration
  • goto → jump to labeled statement (not recommended)

Example:

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

Leave a Comment

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