[go: up one dir, main page]

0% found this document useful (0 votes)
0 views13 pages

Loops

The document covers control structures in programming, specifically focusing on loops such as for, while, and do-while loops. It explains their syntax, use cases, and includes examples for each type, as well as discussing nested loops and loop control statements like break and continue. Additionally, it warns against infinite loops and provides an example of one.

Uploaded by

meynardmatibag
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)
0 views13 pages

Loops

The document covers control structures in programming, specifically focusing on loops such as for, while, and do-while loops. It explains their syntax, use cases, and includes examples for each type, as well as discussing nested loops and loop control statements like break and continue. Additionally, it warns against infinite loops and provides an example of one.

Uploaded by

meynardmatibag
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/ 13

Control Structures: Loops

Engr. John Dennis D. Perez


Instructor
Week 6-7
Objectives
Understand the concept of
Loops.
Master the for Loop.
Master the while Loop.
Master the do-while Loop.
Understand Loop Control
Statements.
Nested Loops.
Avoid infinite Loops.
Introduction
Loops are essential control structures in
programming that allow you to repeat a block
of code multiple times. They are particularly
useful when you need to perform repetitive
tasks, such as iterating over arrays,
processing user input, or performing
calculations. In C++, there are three main
for loop
1. types of loops:
2. while loop
3. do-while loop
Each type of loop has its own use cases and
syntax. Let's explore each one in detail.
The for Loop
The for loop is used when you know exactly
how many
times you want to repeat a block of code.

ialization: Executed once at the beginning of the loop.


s typically used to initialize a loop counter.
ndition: Evaluated before each iteration. If the condition is
loop continues; if false, the loop terminates.
date: Executed at the end of each iteration.
typically used to update the loop counter.
Example 1: Simple for Loop

Output: Explanation:
 Iteration: 1  The loop starts with i = 1.
 Iteration: 2  The condition i <= 5 is checked before each
 Iteration: 3 iteration.
 Iteration: 4  After each iteration, i is incremented by 1
 Iteration: 5 (i++).
 The loop stops when i becomes 6.
The while Loop
The while loop is used when you want to repeat a block of
code as long as
a condition is true. Unlike the for loop, the number of
iterations may not
be known in advance.

ondition: Evaluated before each iteration.


true, the loop continues; if false, the loop terminates.
Example 2: Simple while Loop

Output: Explanation:
 Iteration: 1  The loop starts with i = 1.
 Iteration: 2  The condition i <= 5 is checked before each
 Iteration: 3 iteration.
 Iteration: 4  The loop stops when i becomes 6.
 Iteration: 5
The do-while Loop
The do-while loop is similar to the while loop, but it
guarantees that the loop body is executed at least once,
even if the condition is false.

ondition: Evaluated before each iteration.


true, the loop continues; if false, the loop terminates.
Example 3: Simple do-while Loop

Output: Explanation:
 Iteration: 1  The loop body is executed first, and then the
 Iteration: 2 condition i <= 5 is checked.
 Iteration: 3  The loop stops when i becomes 6.
 Iteration: 4
 Iteration: 5
Comparison of Loops
Feature for Loop while Loop do-while Loop

Known number of Unknown number of At least one


When to Use
iterations iterations iteration
Condition Before each Before each
After each iteration
Check iteration iteration
Inside the loop
Initialization Outside the loop Outside the loop
syntax
Inside the loop
Update Inside the loop body Inside the loop body
syntax

Output:
 Iteration: 1
 Iteration: 2
 Iteration: 3
 Iteration: 4
 Iteration: 5
The Nested Loops
You can place one loop inside another loop to create nested loops.
This is useful for working with multi-dimensional data structures
like matrices.

Output: Explanation:
The outer loop runs 3 times.
i = 1, j =i = 2, j i = 3, j
For each iteration of the outer
1 =1 =1 loop, the inner loop runs 3
i = 1, j =i = 2, j i = 3, j times.
2 =2 =2
i = 1, j =i = 2, j i = 3, j
Loop Control Statements
C++ provides two important keywords to control the flow of
loops:
1. break: Exits the loop immediately.
2. continue: Skips the rest of the current iteration and
proceeds to the next iteration.
Example 5: Using break and
continue
Infinite Loops
An infinite loop occurs when the loop condition
never becomes false. This can happen accidentally
or intentionally.
Example 6: Infinite Loop

Explanation:
The condition true is always true, so the loop never ends.
Use Ctrl+C to terminate the program manually.

You might also like