0% found this document useful (0 votes)
2 views26 pages

Loops Structure

The document provides an overview of loops in C programming, explaining their purpose, types, and advantages. It discusses three main types of loops: for loops, while loops, and do-while loops, along with their syntax and examples. The presentation emphasizes how loops help save time and simplify code by allowing repetitive tasks to be executed efficiently.

Uploaded by

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

Loops Structure

The document provides an overview of loops in C programming, explaining their purpose, types, and advantages. It discusses three main types of loops: for loops, while loops, and do-while loops, along with their syntax and examples. The presentation emphasizes how loops help save time and simplify code by allowing repetitive tasks to be executed efficiently.

Uploaded by

mzainsafdarjutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

RECAP

PRESENTATION
Loops structure
GROUP 3

Presented By:
1-Muhammad Zain
2-Saifullah Khan
Presented to:
Mam Kinza Noor
WHAT ARE LOOPS

• Loops in C are used to repeat a task multiple times without having to write the same
code again and again. They help in saving time, making the program shorter, and
easier to manage.
• For example:
If you want to print "Hello" 5 times then instead of writing:
• printf("Hello\n");
• printf("Hello\n");
• printf("Hello\n");
• printf("Hello\n");
• printf("Hello\n");
HOW THEY WORK ?
• You can use loop:

for (int i = 0; i < 5; i++)


{
printf("Hello\n");
}
• It helps to save time and eaiser to
understand and modify
WHY DO WE USE LOOPS IN C
• Save time: You write less code for repetitive task
• Repetitions: You can run the same instruction for multiple
number of times
• Easy to modify: if you want to execute a statement 5 times
instead of 10 you just need to change a single number
TYPES OF LOOPS

1.For Loop: Used when you know how many times to repeat.
2.while Loop: Used when you want to repeat until a condition is
true.
3.Do While Loop Similar to while, but ensures the code runs at least
once.
• Loops make Programing simple and efficient
UNDERSTANDING WHILE
LOOP
• A while loop in C is a way to repeat a group of instructions over and over, as long
as a certain condition is true.
• Think of it like this:
• If you have a task to do repeatedly, a while loop can help you do it automatically
• Syntax:
• while (condition)
• {
• // Body of the Loop
• }
FLOWCHART OF WHILE LOOP
HOW DOES IT WORK
1.The computer checks a condition (e.g., "Is the number less
than 5?").
2.If the answer is yes, it runs the code inside the loop.
3.After running the code, it checks the condition again.
4.This continues until the condition is false, and then the loop
stops.
PROGRAM
• #include <stdio.h>
• int main()
• {
• int i = 0;
• while (i < 5)
• {
• printf("Number: %d\n", i);
• i++;
• }
•}
ADVANTAGES OF WHILE
LOOP
• The while loop is advantageous when:
• You need repeated actions with undefined repetitions.
• You want to perform a task only if a condition is true.
• You prefer a clean and simple structure for repetitive tasks.
WHAT IS DO WHILE LOOP ?
• A do-while loop in C is a control structure that allows you to
execute a block of code at least once, and then repeat it as long as
a given condition is true.
• Syntax:
• do
•{
• // Body of the loop
• } while (condition);
FLOWCHART OF DO WHILE
LOOP
HOW DOES IT WORKS

A do-while loop works by first executing the block of code


inside the do section and then checking the condition in the
while part. If the condition is true, the loop repeats if it’s false
the loop stops. This ensures the code runs at least once
regardless of the condition
WELCOME BACK
WRITE PROGRAM USING DO
WHILE LOOP
• #include <stdio.h>
• int main() {
• int age;
• do {
• printf("Enter your age (must be greater than 0): ");
• scanf("%d", &age);
• if (age <= 0) {
• printf("Invalid age. Please try again.\n");
• }
• } while (age <= 0);
• printf("Thank you! Your age is: %d\n", age);
• }
CODE EXECUTION
FOR LOOP
• A for loop is a control structure in C used to execute a block of code
repeatedly for a specified number of times.
• Syntax:
• for (initialization ; condition ; increment/decrement)
{
// Body of the loop
}
• Example: for (int i = 0; i <= 5; i++)
{
printf("%d ", i);
}
Output:012345
FLOWCHART OF FOR LOOP
HOW DOES IT WORK
1.Initialization: Executes once at the beginning of the loop.
2.Condition: Evaluated before each repetition if true loop
executes if false exits the loop.
3.Increment/Decrement: Updates the loop variable after each
repetition.
4.Body: Executes as long as the condition is true.
PROGRAM
Write a program to calculate the sum of the first 10 natural
numbers using a for loop.
#include <stdio.h>
int main()
{
int sum = 0;
for (int i = 1; i <= 10; i++)
{
sum += i;
}
printf("The sum of the first 10 natural numbers is: %d", sum);
return 0;
} Output: The sum of the first 10 natural numbers is: 55
ADVANTAGES OF FOR LOOP
• To the point: Combines initialization, condition, and update in
one line.
• Easy to use for known repetition counts.
• Simplifies code readability.
THANKYO
U
F O R H A V I N G M e

You might also like