Lab 4
Lab 4
Lab Report 4
Loops in C++
Loops in programming are control flow structures that allow a set of instructions to be repeatedly
executed based on a specified condition. Here are some general concepts about loops:
1. Iteration:
Loops are used for performing iteration, which is the process of executing a set of
statements or a block of code repeatedly.
Iteration is useful when you need to perform a task multiple times without
duplicating the code.
3. Control Flow:
The loop condition is evaluated before each iteration, and if it evaluates to true,
the loop body is executed. If it evaluates to false, the loop exits.
4. Infinite Loops:
Care should be taken to avoid infinite loops, where the loop condition is always
true, and the loop continues indefinitely.
Infinite loops can lead to the program becoming unresponsive and may need to be
terminated forcefully.
5. Nested Loops:
Loops can be nested inside one another, allowing for more complex iteration
patterns.
Nested loops are common when working with multi-dimensional data structures
or performing multi-level iterations.
Types of Loops
There are three Types of Loops;
For Loop
While Loop Do-While Loop
Lab # 4
Abdullah Awan # 606
For loop:
The for loop in C++ is a control flow statement that allows you to repeatedly execute a
block of code.
It consists of three parts: initialization, condition, and update. The initialization is executed
once at the beginning, the condition is checked before each iteration, and the update is
performed after each iteration.
Examples:
1. Write a program to print first 10 natural number.
Output:
Lab # 4
Abdullah Awan # 606
Output:
while loop:
The while loop is another control flow statement used for repetitive execution of a block of
code.
It has a single condition, and the loop continues to execute as long as the condition is true.
The condition is checked before each iteration, and if it evaluates to true, the loop body is
executed.
while (condition) {
Lab # 4
Abdullah Awan # 606
Examples:
1. Write a program to print the “I love Pakistan” 5 times.
Output:
Lab # 4
Abdullah Awan # 606
Output:
do-while loop:
The do-while loop is similar to the while loop, but with a key difference: the condition is
checked after the execution of the loop body.
This ensures that the loop body is executed at least once, even if the condition is initially
false.
do {
// code to be executed
} while (condition);
Examples:
1. Print First 10 natural numbers
Output:
Lab # 4
Abdullah Awan # 606
Output:
continue Statement:
The continue statement is used to skip the rest of the code inside a loop and
proceed to the next iteration.
When a continue statement is encountered, the program jumps directly to
the loop's update or condition check, bypassing the remaining code in the
loop body for the current iteration.
Example:
Output
Lab # 4
Abdullah Awan # 606
break Statement:
The break statement is used to terminate the entire loop
prematurely, regardless of the loop condition.
When a break statement is encountered, the program exits the loop
immediately, and the control is transferred to the statement
following the loop.
Example:
Output
Nested Loops:
Loops can be nested inside one another, allowing for more complex
iteration patterns.
Nested loops are common when working with multi-dimensional
data structures or performing multi-level iterations.
Lab # 4
Abdullah Awan # 606
Example
Write a Program to print Output as shown. Use setw manipulator.
Output
Lab # 4