0% found this document useful (0 votes)
84 views11 pages

C Loop Control Structures Explained

The document explains three types of control structures in programming: sequence, selection, and repetition, focusing on loops in the C language. It details the syntax and functionality of while, for, and do-while loops, including how they execute code blocks based on conditions. Additionally, it highlights the differences between these loop types and provides examples to illustrate their use.

Uploaded by

hashijannat1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views11 pages

C Loop Control Structures Explained

The document explains three types of control structures in programming: sequence, selection, and repetition, focusing on loops in the C language. It details the syntax and functionality of while, for, and do-while loops, including how they execute code blocks based on conditions. Additionally, it highlights the differences between these loop types and provides examples to illustrate their use.

Uploaded by

hashijannat1000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1/31/2025

Three types of program control structure:


sequence, selection, repetition

Loop: a control structure that repeats a group of


steps in a program. In C language loop control
statements are
while, for, and do-while

loop body: the statements that are repeated in


the loop

while loop
The most basic loop in C is the while loop.
A while statement is like a repeating if
statement. Like an If statement, if the test
condition is true: the statments get
executed. The difference is that after the
statements have been executed, the test
condition is checked again. If it is still true
the statements get executed again.This
cycle repeats until the test condition
evaluates to false.

1
1/31/2025

while ( expression )
{

Single statement or

Block of statements;
}

for loop

2
1/31/2025

for loop
for loop is similar to while, it's just written differently. for statements are often used
to process lists such a range of numbers:
Basic syntax of for loop is as follows:

for( expression1; expression2; expression3)


{
Single statement
or
Block of statements;
}

for loop
A for loop is a type of control structure in programming that allows you to repeat a
block of code a specific number of times.
Basic syntax of for loop is as follows:

for (initialization; condition; increment/decrement)


{
// Code to execute
}

3
1/31/2025

Initialization: This is executed once at the start of the loop. It sets the starting
point for the loop variable.

Condition: This is checked before each iteration of the loop. If the condition
is true, the loop runs. If the condition is false, the loop stops.

Increment/Decrement: This is executed at the end of each loop iteration. It


updates the loop variable (e.g., increases or decreases its value).

Body: The code inside the { } is executed for every iteration where the
condition is true.

#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++)
{ // Initialization: i=1, Condition: i<=5, Increment: i++
printf("Number: %d\n", i); // Print the current value of i
}
return 0;
}

4
1/31/2025

Initialization: int i = 1; (Start with i = 1).


Condition: i <= 5 (Run the loop as long as i is less than or equal to 5).
Increment: i++ (Increase i by 1 after each iteration).
Output: The numbers 1, 2, 3, 4, and 5 are printed one by one.

How the Loop Runs


First Iteration:

i = 1 (initialized).
Condition i <= 5 is true.
Print 1.
Increment i to 2.

Second Iteration:

i = 2. Final Iteration:
Condition i <= 5 is true.
Print 2. i = 5.
Increment i to 3. Condition i <= 5 is true.
... and so on. Print 5.
Increment i to 6.
Stop:

Stop:

Now i = 6 and condition i <= 5 is false.


The loop stops.

5
1/31/2025

#include <stdio.h>

main()
{
int i;
int j = 10;

for( i = 0; i <= j; i ++ )
{
printf("Hello %d\n", i ); Hello 0
} Hello 1
} Hello 2
Hello 3
Out Put Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

You can make use of break to come out of for loop at any time.

#include <stdio.h>

main()
{
int i,j=10;

for( i = 0; i <= j; i ++ )
{
printf("Hello %d\n", i );
Hello 0 if( i == 6 )
Hello 1 {
Hello 2
Hello 3
break;
Hello 4 }
Hello 5 }
}

6
1/31/2025

do...while loop

do ... while is just like a while loop except that the test condition is checked at the end of the
loop rather than the start. This has the effect that the content of the loop are always executed
at least once.
Basic syntax of do...while loop is as follows:

do
{
Single statement
or
Block of statements;
}while(expression);

7
1/31/2025

How It Works

Execute the Code Block:


The statements inside the do block are executed once before the condition is
checked.

Check the Condition:


If the condition is true, the loop continues to execute.
If the condition is false, the loop stops.

Repeat:
The loop repeats until the condition becomes false.

#include <stdio.h>

main()
{
int i = 10;

do{
printf("Hello %d\n", i ); Hello 10
i = i -1; Hello 9
}while ( i > 0 ); Hello 8
} Hello 7
Hello 6
Out Put Hello 5
Hello 4
Hello 3
Hello 2
Hello 1

8
1/31/2025

You can make use of break to come out of do...while loop at any time.

Hello 10 #include <stdio.h>


Hello 9
Hello 8 main()
Hello 7 {
Hello 6 int i = 10;

do{
printf("Hello %d\n", i );
i = i -1;
if( i == 6 )
{
break;
}
}while ( i > 0 );
}

Execution Step-by-Step
First Iteration:
i = 1.
The do block runs and prints: Iteration: 1.
i is incremented to 2.
#include <stdio.h> The condition i <= 3 is checked. It's true, so the loop
repeats.
int main() {
int i = 1; // Initialize variable Second Iteration:
i = 2.
do { The do block runs and prints: Iteration: 2.
printf("Iteration: %d\n", i); // Execute the block i is incremented to 3.
i++; // Increment i The condition i <= 3 is checked. It's true, so the loop
} while (i <= 3); // Condition check repeats.

return 0; Third Iteration:


} i = 3.
The do block runs and prints: Iteration: 3.
i is incremented to 4.
The condition i <= 3 is checked. It's now false, so the
loop stops.

9
1/31/2025

What is the output?

#include <stdio.h>
int main()
{
int i = 10;
do {
printf("This will print once even if the condition is false.\n");
} while (i < 5);
return 0;
}

Feature for Loop while Loop do-while Loop


Execution Executes the block only Checks the condition before Executes the block at least once,
Condition if the condition is true. executing the block. even if the condition is false.
Used when the loop should
Suitable when the Useful when the loop must run at
run based on a condition,
When to Use number of iterations is least once, regardless of the
but the number of iterations
known beforehand. condition.
isn't predefined.
Condition is checked
Condition Condition is checked before Condition is checked after the
before the first
Check each iteration. first iteration.
iteration.
for (initialization;
do
condition; while(condition)
{
increment/decrement) {
Syntax Code
{ code
}
Code }
while (condition);
}
Execution May not execute if the May not execute if the Always executes once, even if
Guarantee condition is false initially. condition is false initially. the condition is false initially.

10
1/31/2025

বিশ for লুপ while লুপ do-while লুপ


শত সত হেল কাড ক শত থেম চক কের, শত িমথ া হেলও অ ত
কাড চালােনার শত
চালায়। তারপের কাড চালায়। একবার কাড চালায়।
যখন লুপ শেতর উপর িনভর
যখন কাড অ ত একবার
যখন লুেপর পুনরাব ৃি সংখ া কের চলেব, িক ু পুনরাব ৃি
কখন ব বহার করেবন চালােনা দরকার, শত যাই
আেগই জানা থােক। সংখ া আেগই জানা থােক
হাক।
না।
শত থম পুনরাব ৃি র আেগ িতিট পুনরাব ৃি র আেগ শত থম পুনরাব ৃি র পের শত
শত চক করার সময়
চক হয়। চক হয়। চক হয়।
for (initialization;
do
condition; while(condition)
{
increment/decrement) {
িসনট া Code
{ code
}
Code }
while (condition);
}
শত িমথ া হেল লুপিট কখেনা শত িমথ া হেল লুপিট কখেনা শত িমথ া হেলও লুপিট
কাড চালােনার িন য়তা
চলেব না। চলেব না। অ ত একবার চালােব।

for loop while loop do..while loop

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

11

You might also like