Decision Making and Loops in C Programming: Complete Beginner to Advanced Guide with Examples

Author: Er. Mujtaba Ansari

Want to make your C programs and another programs smarter and more efficient?
Decision-making-and-looping helps your program choose what to do, while loops handle repetitive tasks with ease.
Learn if, switch, for, while, and do-while with simple examples.
By the end of this guide, controlling the flow of a C program will feel easy.

🧑‍💻 Chapter 1: C Programming Fundamentals
📜 Chapter 2: History and Evolution of C Language
⚙️ Chapter 3: Installing a C Compiler
💻 Chapter 4: Writing Your First C Program
🏗️ Chapter 5: Structure of a C Program
📦 Chapter 6: Variables and Data Types
🔢 Chapter 7: Operators in C
⌨️ Chapter 8: Input and Output Functions
🔀 Chapter 9: Decision Making and Branching – Conditional Statements
🔄 Chapter 10: Decision Making and Loops in C Programming – Current Chapter
📊 Chapter 11: Arrays
🔤 Chapter 12: Strings
🛠️ Chapter 13: Functions
👉 Chapter 14: Pointers
🏛️ Chapter 15: Structures and Unions
📁 Chapter 16: File Handling

A loop is a control structure that allows a program to execute the same block of statements repeatedly as long as a specified condition is satisfied.

Suppose you want to print numbers from 1 to 100.

Without a loop, you would have to write:

printf("1");
printf("2");
printf("3");

and continue until 100.

This would make the program unnecessarily long.

With a loop, the same task can be done with just a few lines:

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

This is the main purpose of loops: to reduce repetitive code and make programs shorter, cleaner, and easier to maintain.

C provides three primary looping statements:

  • while
  • for
  • do-while

The C language reference also classifies these as iteration statements.

Loops are one of the most important concepts in any programming languages.

They are used when the same operation needs to be performed multiple times.

For example, loops can be used to:

  • print numbers,
  • calculate factorials,
  • generate Fibonacci series,
  • find prime numbers,
  • process arrays,
  • print multiplication tables,
  • reverse numbers,
  • check palindrome numbers,
  • check Armstrong numbers,
  • print patterns,
  • process user input,
  • perform repeated calculations.
Simple Example
for (int i = 1; i <= 5; i++) {
    printf("Hello\n");
}

Output:

Hello
Hello
Hello
Hello
Hello

Instead of writing printf() five times, we use a loop.

Loops can be classified into two categories based on when the condition is checked.

1. Entry-Controlled Loops

The condition is checked before the loop body executes.

C has two entry-controlled loops:

  • while
  • for

2. Exit-Controlled Loop

The condition is checked after the loop body executes.

C has one exit-controlled loop:

  • do-while

This classification is also present in your original notes.

An entry-controlled loop is a type of loop in which the condition is checked before the loop body is executed.

The loop body is executed only when the condition is true. The loop continues to execute repeatedly as long as the condition remains true. As soon as the condition becomes false, the loop terminates.

Examples of Entry-Controlled Loops:
  • while loop
  • for loop

OR

In an entry-controlled loop, the condition is checked before executing the loop body.

If the condition is false at the beginning, the loop body will not execute even once.

For example:

int i = 10;

while (i < 5) {
    printf("%d", i);
}

Here:

10 < 5

is false.

Therefore, the loop body executes zero times.

The while loop checks its controlling expression before each iteration.

A while loop is an entry-controlled loop. In this type of loop, the condition is checked first, and then the loop body is executed only if the condition is satisfied.

The loop body executes repeatedly as long as the condition remains true. The loop terminates as soon as the condition becomes false.

Syntax
Initialization;

while (test condition)
{
    --------------------
    Loop Body
    --------------------
    Updation
    --------------------
}

Statement X;

Note: In a while loop, the condition is checked before the loop body is executed. Therefore, if the test condition is false initially, the loop body will not execute even once.

Flow Chart:

Consider:

int i = 1;

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

The execution works like this:

Step 1
i = 1

Check:

1 <= 5 → true

Print 1.

Step 2
i = 2

Check:

2 <= 5 → true

Print 2.

The process continues until:

i = 6

Now:

6 <= 5 → false

The loop stops.

This is one of the basic while loop programs from your original notes.

Program
#include <stdio.h>

int main(void) {
    int n, i = 1;

    printf("Enter the value of N: ");
    scanf("%d", &n);

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

    return 0;
}
Example Output

For:

N = 5

Output:

1 2 3 4 5
Explanation
  • i = 1 initializes the counter.
  • i <= n checks the condition.
  • printf() prints the current value.
  • i++ increases the value by 1.
  • The loop stops when i becomes greater than n.

We can use a while loop to print the multiplication table of any number.

Program
#include <stdio.h>

int main(void) {
    int n, i = 1;

    printf("Enter the number: ");
    scanf("%d", &n);

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

    return 0;
}
Example

For n = 8:

8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80

This is based on the multiplication-table exercise in your notes, with its original syntax corrected.

A nested loop means one loop is placed inside another loop.

For example:

while (condition1) {

    while (condition2) {
        // inner loop
    }

}

The outer loop controls one repetition, while the inner loop performs another set of repetitions.

Nested loops are especially useful for:

  • multiplication tables,
  • patterns,
  • matrices,
  • two-dimensional data.

Your original notes include a nested while loop for a multiplication table from 1 to 10.

Here is the corrected version:

#include <stdio.h>

int main(void) {
    int i = 1;

    while (i <= 10) {
        int j = 1;

        while (j <= 10) {
            printf("%5d", i * j);
            j++;
        }

        printf("\n");
        i++;
    }

    return 0;
}

How it works

The outer loop controls:

1 to 10

The inner loop also runs:

1 to 10

Therefore, the program calculates:

1 × 1
1 × 2
...
10 × 10

The Fibonacci series is:

0 1 1 2 3 5 8 13 21 34 ...

In this series, each new term is obtained by adding the previous two terms.

For example:

0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5

Your original notes include Fibonacci as a while loop exercise.

#include <stdio.h>

int main(void) {
    int n;
    int i = 3;
    long long a = 0, b = 1, c;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    if (n <= 0) {
        printf("Please enter a positive number.");
    } else if (n == 1) {
        printf("0");
    } else {
        printf("0 1");

        while (i <= n) {
            c = a + b;
            printf(" %lld", c);

            a = b;
            b = c;
            i++;
        }
    }

    return 0;
}
Example

Input:

7

Output:

0 1 1 2 3 5 8

In this type of loop, first of all, the initialization is performed, and then the test condition is checked. The loop body is executed only if the condition is true.

The loop body is executed repeatedly as long as the condition remains true. The loop terminates as soon as the condition becomes false.

Syntax
for (initialization; test condition; updation)
{
    -----------------------
        Loop Body
    -----------------------
}
Flow Chart:

Consider:

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

Runs once at the beginning.

2. Condition
i <= 5

Checked before each iteration.

3. Update
i++

Runs after each iteration.

#include <stdio.h>

int main(void) {

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

    return 0;
}

Output:

1 2 3 4 5

The factorial of a positive integer is the product of all positive integers from 1 to that number.

For example:

5! = 5 × 4 × 3 × 2 × 1
   = 120

Your original notes include factorial using a for loop.

#include <stdio.h>

int main(void) {
    int n;
    unsigned long long fact = 1;

    printf("Enter a non-negative integer: ");
    scanf("%d", &n);

    if (n < 0) {
        printf("Factorial is not defined for negative numbers.");
        return 0;
    }

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

    printf("Factorial = %llu", fact);

    return 0;
}
Example

Input:

5

Output:

Factorial = 120
Important

By mathematical convention:

0! = 1

The program also produces 1 for 0.

A prime number is a number greater than 1 that has exactly two positive divisors:

  • 1
  • the number itself

Examples:

2, 3, 5, 7, 11, 13, 17...

Your original notes use a flag and break to determine whether a number is prime.

#include <stdio.h>

int main(void) {
    int n;
    int isPrime = 1;

    printf("Enter the number: ");
    scanf("%d", &n);

    if (n <= 1) {
        isPrime = 0;
    }

    for (int i = 2; i * i <= n && isPrime; i++) {
        if (n % i == 0) {
            isPrime = 0;
        }
    }

    if (isPrime) {
        printf("%d is a prime number.", n);
    } else {
        printf("%d is not a prime number.", n);
    }

    return 0;
}
Example

Input:

17

Output:

17 is a prime number.

Ans:

#include <stdio.h>

int main(void)
{
    int N, i, flag;

    for (N = 11; N <= 100; N++)
    {
        flag = 0;

        for (i = 2; i <= N / 2; i++)
        {
            if (N % i == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
        {
            printf("%5d", N);
        }
    }

    return 0;
}
Output
   11   13   17   19   23   29   31   37   41   43
   47   53   59   61   67   71   73   79   83   89
   97
Explanation
  • The outer for loop checks every number from 11 to 100.
  • For each number, flag is initialized to 0.
  • The inner for loop checks whether the number is divisible by any number from 2 to N/2.
  • If a divisor is found, flag becomes 1 and break stops the inner loop.
  • If flag remains 0, the number is prime and is printed.

Answer:

#include <stdio.h>
#include <math.h>

int main(void)
{
    int N, x, i;
    int s = 0;

    printf("Enter the value of N and x: ");
    scanf("%d %d", &N, &x);

    for (i = 1; i <= N; i++)
    {
        s = s + (int)pow(x, i);
    }

    printf("Sum = %d", s);

    return 0;
}
Example

Suppose:

N = 3
x = 5

Then:S=51+52+53S = 5^1 + 5^2 + 5^3

= 5 + 25 + 125
= 155
Step-by-Step Calculation

When i = 1:

s = 0 + 5¹
  = 5

When i = 2:

s = 5 + 5²
  = 5 + 25
  = 30

When i = 3:

s = 30 + 5³
  = 30 + 125
  = 155
Output
Enter the value of N and x: 3 5
Sum = 155

Answer:

#include <stdio.h>
#include <math.h>

int main(void)
{
    int N, x, i, j, fact;
    float s = 0;

    printf("Enter the value of N and x: ");
    scanf("%d %d", &N, &x);

    for (i = 1; i <= N; i++)
    {
        fact = 1;

        for (j = 1; j <= 2 * i - 1; j++)
        {
            fact = fact * j;
        }

        s = s + pow(-1, i) * pow(x, 2 * i) / fact;
    }

    printf("Sum = %f", s);

    return 0;
}
Example

Suppose:

N = 3
x = 2

Then:S=221!+243!265!S = -\frac{2^2}{1!}+\frac{2^4}{3!}-\frac{2^6}{5!}=4+16664120= -4+\frac{16}{6}-\frac{64}{120} =−1.866667

Answer:

#include <stdio.h>

int main(void)
{
    int N, i, j, s = 0, R;

    printf("Enter the value of N: ");
    scanf("%d", &N);

    for (i = 1; i <= N; i++)
    {
        R = 0;

        for (j = 1; j <= i; j++)
        {
            R = R + j;
        }

        s = s + R;
    }

    printf("Result = %d", s);

    return 0;
}
Example

If N = 3:S=(1)+(1+2)+(1+2+3)S = (1) + (1+2) + (1+2+3)=1+3+6= 1 + 3 + 6=10= 10

Output
Enter the value of N: 3
Result = 10

Note: Why Do We Check i * i <= n?

Suppose a number is composite.

If it has a factor larger than its square root, it must also have a corresponding factor smaller than its square root.

Therefore, checking divisors up to the square root is sufficient for determining whether a number is prime.

This makes the program more efficient than checking every number up to n.

A for loop inside another for loop is called a nested for loop.

Syntax
for (initialization; condition; update) {

    for (initialization; condition; update) {
        // inner loop
    }

}

Nested loops are commonly used for:

  • patterns,
  • multiplication tables,
  • matrices,
  • rows and columns,
  • multidimensional data.

Flow Chart:

Answer:

#include <stdio.h>

int main(void)
{
    int i, j;

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

        printf("\n");
    }

    return 0;
}
Output
1 2 3
1 2 3
1 2 3
How it works
  • The outer for loop controls the rows.
  • The inner for loop controls the numbers printed in each row.
  • For every one iteration of the outer loop, the inner loop runs 3 times.

In an exit-controlled loop, the loop body executes first and the condition is checked afterward.

Therefore, the loop body executes at least once.

The do-while statement is the exit-controlled loop in C. Your original notes describe exactly this behavior.

Syntax
Initialization;

do
{
    ---------------------
    ---------------------
    Loop Body
    ---------------------
    Updation;
    ---------------------
}
while (test condition);

Statement X;

Note: The do-while loop is an exit-controlled loop. The loop body is executed at least once because the condition is checked after the loop body.

Flow Chart:

#include <stdio.h>

int main(void) {
    int i = 1;

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

    return 0;
}

Output:

1 2 3 4 5

Consider:

int i = 10;

do {
    printf("%d", i);
} while (i < 5);

The condition:

10 < 5

is false.

However, the output is:

10

because the body executes before the condition is tested.

This is the key difference between while and do-while.

To reverse a number, we repeatedly extract its last digit.

The last digit is obtained using:

digit = number % 10;

Then the last digit is removed using:

number = number / 10;

Your notes use this approach for reversing a number and checking whether it is a palindrome.

#include <stdio.h>

int main(void) {
    int num, n;
    int digit;
    int reverse = 0;

    printf("Enter the number: ");
    scanf("%d", &num);

    n = num;

    do {
        digit = n % 10;
        reverse = reverse * 10 + digit;
        n /= 10;
    } while (n > 0);

    printf("Original number = %d\n", num);
    printf("Reversed number = %d", reverse);

    return 0;
}

A number is called a palindrome number if it remains the same after its digits are reversed.

Examples:

121
131
111

Your notes specifically provide these examples.

#include <stdio.h>

int main(void) {
    int num, n;
    int digit;
    int reverse = 0;

    printf("Enter the number: ");
    scanf("%d", &num);

    n = num;

    do {
        digit = n % 10;
        reverse = reverse * 10 + digit;
        n /= 10;
    } while (n > 0);

    if (num == reverse) {
        printf("%d is a palindrome number.", num);
    } else {
        printf("%d is not a palindrome number.", num);
    }

    return 0;
}
Example

Input:

121

Output:

121 is a palindrome number.

Your notes list the following Armstrong numbers:

1, 153, 370, 371, 407

and demonstrate checking the digits of 153.

For a three-digit Armstrong number, the sum of the cubes of its digits equals the original number.

For example:

153 = 1³ + 5³ + 3³
    = 1 + 125 + 27
    = 153
#include <stdio.h>

int main(void) {
    int num, n;
    int digit;
    int sum = 0;

    printf("Enter a three-digit number: ");
    scanf("%d", &num);

    n = num;

    do {
        digit = n % 10;
        sum += digit * digit * digit;
        n /= 10;
    } while (n > 0);

    if (num == sum) {
        printf("%d is an Armstrong number.", num);
    } else {
        printf("%d is not an Armstrong number.", num);
    }

    return 0;
}
Output Example

If the user enters 153:

Enter a three-digit number: 153
153 is an Armstrong number.
Explanation

An Armstrong number is a number whose value is equal to the sum of the cubes of its digits for a three-digit number.

For 153:13+53+331^3 + 5^3 + 3^3=1+125+27= 1 + 125 + 27=153= 153

Therefore, 153 is an Armstrong number.

Your original notes include an exercise to print Armstrong numbers between 1 and 1000.

#include <stdio.h>

int main(void) {

    for (int num = 1; num <= 1000; num++) {

        int n = num;
        int sum = 0;

        do {
            int digit = n % 10;
            sum += digit * digit * digit;
            n /= 10;
        } while (n > 0);

        if (num == sum) {
            printf("%d ", num);
        }
    }

    return 0;
}

Output:

1 153 370 371 407

Nested loops are extremely useful for printing patterns.

Increasing Star Pattern

Output:

*
* *
* * *
* * * *
* * * * *
#include <stdio.h>

int main(void) {

    for (int i = 1; i <= 5; i++) {

        for (int j = 1; j <= i; j++) {
            printf("* ");
        }

        printf("\n");
    }

    return 0;
}
How it works

The outer loop controls the number of rows.

The inner loop controls the number of stars in each row.

For example:

Row 1 → 1 star
Row 2 → 2 stars
Row 3 → 3 stars

and so on.

Output:

#include <stdio.h>

int main(void) {

    for (int i = 5; i >= 1; i--) {

        for (int j = 1; j <= i; j++) {
            printf("* ");
        }

        printf("\n");
    }

    return 0;
}

Output:

#include <stdio.h>

int main(void) {
    int n;

    printf("Enter the number of rows: ");
    scanf("%d", &n);

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

        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}
Logic

Each row contains two parts:

  1. Spaces before the stars.
  2. Stars forming the pyramid.

These statements are used to jump or transfer control from one part of a program to another.

C provides four jump statements:

  1. break
  2. continue
  3. goto
  4. return

The C language reference also lists these four as jump statements.

The break statement immediately terminates the nearest enclosing loop or switch statement.

Syntax
break;
Example
#include <stdio.h>

int main(void) {

    for (int i = 1; i <= 5; i++) {

        if (i == 3) {
            break;
        }

        printf("%d ", i);
    }

    return 0;
}

Output:

1 2

When i becomes 3, the loop stops.

Always remember

break = Stop the loop

The continue statement skips the remaining statements of the current iteration and moves to the next iteration.

Syntax
continue;
Example
#include <stdio.h>

int main(void) {

    for (int i = 1; i <= 5; i++) {

        if (i == 3) {
            continue;
        }

        printf("%d ", i);
    }

    return 0;
}

Output:

1 2 4 5
Always remember

continue = Skip this iteration

breakcontinue
Terminates the loopSkips current iteration
Execution moves outside the loopExecution moves to the next iteration
Can be used with loops and switchUsed with loops
Stops further iterationsAllows later iterations to continue

Your notes include a practical program that accepts up to 20 numbers, counts positive and negative numbers, exits when 0 is entered, and displays square roots of positive numbers.

Here is the corrected modern C version:

#include <stdio.h>
#include <math.h>

int main(void) {
    int n;
    int positive = 0;
    int negative = 0;

    for (int i = 1; i <= 20; i++) {

        printf("Enter a number (0 to exit): ");
        scanf("%d", &n);

        if (n == 0) {
            break;
        }

        if (n > 0) {
            positive++;

            printf("Square root of %d = %.2f\n",
                   n, sqrt((double)n));
        } else {
            negative++;
            continue;
        }
    }

    printf("\nTotal positive numbers = %d\n", positive);
    printf("Total negative numbers = %d\n", negative);

    return 0;
}
What happens?
  • 0break stops the loop.
  • Positive number → positive counter increases and square root is displayed.
  • Negative number → negative counter increases and continue skips to the next iteration.

This statement is used to jump or transfer control from one part of a program to another labeled part of the program.

Syntax
goto label;

label:
    // statements

OR

Syntax of goto Statement

(1) Forward Jump:

label:
    --------------
    --------------
    goto label;

(2) Backward Jump:

goto label;

    ---------------
    ---------------

label:
    ------------
    ------------

Note: A label is an identifier followed by a colon (:), and the goto statement transfers control to that labeled statement.

#include <stdio.h>

int main(void) {
    int n;
    int i = 1;
    int sum = 0;

    printf("Enter the value of N: ");
    scanf("%d", &n);

sum_loop:

    sum += i * i;
    i++;

    if (i <= n) {
        goto sum_loop;
    }

    printf("Result = %d", sum);

    return 0;
}

For:

N = 3

the calculation is:

1² + 2² + 3²
= 1 + 4 + 9
= 14

The original notes use this same sum-of-squares goto exercise.

Should goto be used frequently?

Generally, no.

Although goto is a valid C statement, normal loops and structured control flow are usually easier to read and maintain.

The return statement terminates the execution of a function and can return a value to the calling function.

Syntax
return expression;
#include <stdio.h>

int square(int n) {
    return n * n;
}

int main(void) {
    int result = square(5);

    printf("Square = %d", result);

    return 0;
}

Output:

Square = 25
Featureforwhiledo-while
TypeEntry-controlledEntry-controlledExit-controlled
ConditionChecked before bodyChecked before bodyChecked after body
Minimum executions001
Best suited forKnown/repeated iterationsCondition-based repetitionAt-least-once execution
InitializationUsually in loopUsually before loopUsually before loop
UpdateUsually in loopUsually inside bodyUsually inside body

The key distinction is the position of the condition check: while and for test before the body, while do-while tests afterward.

Both for and while are entry-controlled loops.

for

Best when the number of iterations is known.

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

Best when repetition primarily depends on a condition.

while (number != 0) {
    // statements
}
while

Condition is checked first:

while (condition) {
    // body
}

The body can execute zero times.

do-while

Body executes first:

do {
    // body
} while (condition);

The body executes at least once.

One of the most common beginner mistakes is forgetting to update the loop variable.

Incorrect

int i = 1;

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

Here, i never changes.

Therefore, the condition remains true and the loop does not terminate normally.

Correct

int i = 1;

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

Always make sure that a loop has a clear path toward its termination condition.

Consider:

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

The initial condition:

1 >= 5

is false.

Therefore, the loop executes zero times.

Correct:

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

Remember: that do-while requires a semicolon after the condition.

Correct:

do {
    printf("Hello");
} while (condition);

Not Correct:

do {
    printf("Hello");
} while (condition)

When one loop is placed inside another, the inner loop normally completes all of its iterations for each iteration of the outer loop.

Example:

for (int i = 1; i <= 3; i++) {

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

}

The inner loop executes three times for every iteration of the outer loop.

So the total number of inner executions is:

3 × 3 = 9

This concept becomes especially important when working with arrays and matrices in later chapters.

Loops can be controlled using:

break
continue

break

Stops the loop completely.

continue

Skips the current iteration.

goto

Moves control to a label.

return

Exits the current function.

These are officially classified as C jump statements.


To write clean and reliable C programs:

1. Choose the right loop

Use for when the number of iterations is known.

Use while when the condition controls repetition.

Use do-while when the body must execute at least once.

2. Use meaningful variables

Instead of:

int x;

prefer:

int count;

when the variable represents a count.

3. Always update the loop variable

Especially with while loops.

4. Keep loop conditions clear

Avoid unnecessarily complicated conditions.

5. Use braces

Even for a single statement, braces make code easier to read:

while (i <= 10) {
    printf("%d", i);
}

6. Avoid unnecessary goto

Structured loops are usually easier to understand.

7. Test boundary conditions

Test values such as:

0
1
negative values
maximum expected values

After completing this chapter, practice the following programs.

Basic Loop Programs

  1. Print first N natural numbers.
  2. Print numbers from 1 to 100.
  3. Print even numbers.
  4. Print odd numbers.
  5. Print multiplication table.
  6. Find the sum of natural numbers.
  7. Find the factorial of a number.

Number Programs

  1. Print prime numbers in a range.
  2. Generate Fibonacci series.
  3. Check prime number.
  4. Reverse a number.
  5. Check palindrome number.
  6. Check Armstrong number.
  7. Print Armstrong numbers in a range.

Series Programs

  1. Calculate x + x² + x³ + ... + xⁿ.
  2. Calculate factorial-based mathematical series.
  3. Calculate nested series such as (1) + (1+2) + (1+2+3) + ....

Nested Loop Programs

  1. Multiplication table.
  2. Increasing star pattern.
  3. Decreasing star pattern.
  4. Pyramid pattern.
  5. Matrix-style programs.

Jump Statement Programs

  1. Practice break.
  2. Practice continue.
  3. Understand goto.
  4. Use return with functions.

Types of Loops

1. while
2. for
3. do-while

Entry-Controlled Loops

while
for

Exit-Controlled Loop

do-while

Jump Statements

break
continue
goto
return

Important Loop Concepts

Initialization
Condition
Loop Body
Updation
Iteration
Nested Loop
Infinite Loop

What is a loop in C?

A loop is a control structure that repeatedly executes a block of statements while a specified condition remains true.

How many types of loops are there in C?

C has three primary loops:

  • for
  • while
  • do-while

Which loops are entry-controlled?

for and while are entry-controlled loops.

Which loop is exit-controlled?

do-while is the exit-controlled loop.

Which loop executes at least once?

The do-while loop executes its body at least once because its condition is checked after the body.

What is the difference between for and while?

Both are entry-controlled loops. for is commonly convenient when initialization, condition, and update naturally belong together, while while is often clearer when repetition depends primarily on a condition.

What is a nested loop?

A nested loop is a loop placed inside another loop.

What is an infinite loop?

An infinite loop is a loop that does not reach a terminating condition.

Example:

while (1) {
    printf("Running...");
}

It should only be used intentionally.

What does break do in C?

break immediately terminates the nearest enclosing loop or switch.

What does continue do in C?

continue skips the remaining part of the current iteration and proceeds with the next iteration.

What is the use of goto?

goto transfers control to a labeled statement within the same function. It is generally best used sparingly.

What is the use of return?

return terminates the current function and can send a value back to the calling function.



Leave a Comment

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