0% found this document useful (0 votes)
13 views2 pages

Loops

loops in c and c++

Uploaded by

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

Loops

loops in c and c++

Uploaded by

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

Loops:

- What is loop?
A loop is a fundamental control flow construct in programming that allows
you
to repeatedly execute a block of code while a certain condition is true.
Loops are used to perform repetitive tasks efficiently.

- Why loops?
Loops are essential in programming because they allow you to automate
repetitive tasks, iterate through data structures, and perform operations a
specific number of times. They help in writing cleaner and more concise
code.

- Types of loops?
There are three common types of loops in both C and C++:
1. For Loop:
Executes a block of code a specified number of times.
Similar in both languages:
for (initialization; condition; increment/decrement) {
// Code to be repeated
}

2. While Loop:
Executes a block of code as long as a specified condition is true.
Similar in both languages:
while (condition) {
// Code to be repeated
}

3. Do-While Loop:
Similar to a while loop but guarantees that the code block executes
at least once.
Similar in both languages:
do {
// Code to be repeated
} while (condition);

- continue and break keywords?


* continue:
In loops, continue is used to skip the current iteration and proceed to
the next iteration.
* break:
break is used to exit the loop prematurely, immediately terminating the
loop's execution.

- Use of loops?
Loops are used for various purposes, including:
* Iterating through arrays, lists, or collections of data.
* Implementing repetitive calculations.
* Creating menu-driven programs.
* Handling input validation.
* Implementing game loops in game development.
* Repeating actions until a specific condition is met.

- Questions:
* How can you exit a for loop prematurely before it reaches the end?

- Examples?
* sum.c for for loop
* factorial.cpp for while loop
* validate.c for do while loop

You might also like