[go: up one dir, main page]

0% found this document useful (0 votes)
20 views8 pages

Lab 4

The document discusses different types of loops in C++ including for, while, do-while loops and provides examples of each. It also covers concepts like nested loops, continue and break statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Lab 4

The document discusses different types of loops in C++ including for, while, do-while loops and provides examples of each. It also covers concepts like nested loops, continue and break statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Abdullah Awan # 606

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.

2. Initialization, Condition, and Update:

 In many loop constructs, there are three main components:

 Initialization: Setting up initial values or conditions before the loop starts.

 Condition: A Boolean expression that is evaluated before each iteration. If


the condition is true, the loop continues; otherwise, it terminates.

 Update: Modifying variables or conditions within the loop, typically done


after each iteration.

3. Control Flow:

 Loops control the flow of a program by allowing certain blocks of code to be


repeated until a specified condition is met.

 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.

 The loop continues to execute as long as the condition is true.

The syntax of a for loop is:

for (initialization; condition; Increment)

Examples:
1. Write a program to print first 10 natural number.

Output:

Lab # 4
Abdullah Awan # 606

2. Write a program to print the table by given number

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.

The structure of a while loop in C++ is as follows:

while (condition) {

// body of the loop

Lab # 4
Abdullah Awan # 606

Examples:
1. Write a program to print the “I love Pakistan” 5 times.

Output:

2. Write a program to calculate sum of first 10 odd numbers and also


print numbers

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.

 The loop continues to execute as long as the condition is true.

The Syntax of Do-While loop is

do {

// code to be executed

} while (condition);

Examples:
1. Print First 10 natural numbers

Output:

Lab # 4
Abdullah Awan # 606

2. Print the Factorial of a number

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

You might also like