0% found this document useful (0 votes)
3 views5 pages

Experiment 3

The document provides an overview of loops in C++, detailing their types: for, while, and do-while loops, along with their characteristics as entry-controlled or exit-controlled. It explains the increment and decrement operators, their forms, and the syntax and functionality of each loop type. The conclusion summarizes the understanding gained from the experiment regarding the differences in loop types and operators.

Uploaded by

aryan jadhav
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)
3 views5 pages

Experiment 3

The document provides an overview of loops in C++, detailing their types: for, while, and do-while loops, along with their characteristics as entry-controlled or exit-controlled. It explains the increment and decrement operators, their forms, and the syntax and functionality of each loop type. The conclusion summarizes the understanding gained from the experiment regarding the differences in loop types and operators.

Uploaded by

aryan jadhav
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/ 5

Experiment 3

Aim: Study of loops in C++

Theory:
Loops in C++

Loops in C++ allow code to execute repeatedly based on a condition. Loops are fundamental in
programming for tasks where operations need to be repeated a certain number of times or until a
specific condition is met. There are three main types of loops in C++:

1. For Loop
2. While Loop
3. Do-While Loop

Each of these loops can be classified as entry-controlled or exit-controlled, depending on when


the condition is checked.

Entry-Controlled Loops

These loops check the loop condition at the start of each iteration. If the
condition is true, the loop body executes; if false, it skips. Examples in C++
include for and while loops.

Exit-Controlled Loops

These loops check the loop condition at the end of each iteration. This
guarantees that the loop executes at least once, regardless of the condition.
In C++, the do-while loop is exit-controlled.

Increment Operator

Increment Operator (++): This operator increases the value of a variable by 1. It can be used in
two forms:

 Pre-increment (++var): Increments the variable first, then returns the


incremented value.
 Post-increment (var++): Returns the current value, then increments
the variable.
Decrement Operator

Decrement Operator (--): This operator decreases the value of a variable by 1. It also has two
forms:

 Pre-decrement (--var): Decrements the variable first, then returns


the decremented value.
 Post-decrement (var--): Returns the current value, then decrements
the variable.

For Loop

 A for loop is generally used when the number of iterations is known


ahead of time.
 Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute
}
 Explanation:
1. Initialization runs once at the start.
2. Condition is checked before each iteration.
3. Increment/decrement adjusts the counter after each loop cycle
(e.g., i++).
4. The loop continues as long as condition is true.

While Loop

 A while loop is used when the number of iterations is unknown and


depends on a condition.
 Syntax:
While (condition) {
// Code to execute
}
 Explanation:
1. Condition is evaluated before each iteration.
2. If condition is true, the loop body executes; if false, the loop
exits.
3. This loop might not execute at all if the initial condition is false.
Do-while loop

 A do-while loop executes the loop body first, then checks the condition.
This ensures it runs at least once.
 Syntax:
Do {
// Code to execute
} while (condition);
 Explanation:
1. The loop body executes once before checking condition.
2. After the first run, condition is evaluated. If true, the loop
continues; if false, it exits.

Algorithm:
1. Start
2. Declare and Initialise variable num1=0,num2=1,num,number
3. Get numbers to be printed (num)
4. num = num - 2
5. Print num1 and num2
6. For i = 1, i++, while i < num
number = num1 + num2
num1 = num2
num2 = number
Print number
7. Stop
Flowchart:
Output

Conclusion
Thus from above experiment we understood the difference between entry
controlled and exit controlled loops, difference between types of increment
and decrement operators and how for loop, while loop & do-while loop works.

You might also like