LAB # 03: Loops in C++ (for, while & do-while)
Lab Objective:
The primary objective of this lab is to provide students with a comprehensive understanding of
loops in C++. Students will learn how to use different types of loops, including for, while, and
do-while loops, to implement repetitive tasks efficiently. By the end of this lab, students should
be able to write C++ programs that utilize loops for tasks such as counting, iterating over arrays,
and handling user-defined conditions. Additionally, students will develop problem-solving skills
by implementing loops in various practical scenarios, ensuring optimized and structured code
development.
Hardware/Software Tools:
Hardware: Desktop/Computer
Software Tool: MS Visual Studio 2013
Lab Description:
Loops are a fundamental concept in programming that allow the execution of a block of code
multiple times based on a specified condition. In this lab, students will explore three primary
looping constructs in C++, the for loop, which is used when the number of iterations is known;
the while loop, which runs as long as a given condition remains true; and the do-while loop,
which ensures the code executes at least once before checking the condition. Through hands-on
exercises, students will practice writing C++ programs that perform repetitive tasks such as
generating number sequences, calculating sums, and processing arrays. The lab will also cover
nested loops, demonstrating how multiple loops can work together to solve complex problems.
By experimenting with different loop structures, students will gain confidence in writing
efficient and well-structured C++ programs.
Loops:
Loops are used in programming to repeat a set of instructions multiple times without writing the
same code again and again. Suppose we want to display Hello five times, instead of writing five
cout statements, we can use a loop to do it automatically. Loops keep running until a condition is
met, making them useful for counting numbers, processing lists, and solving repetitive tasks
efficiently.
There are three main types of loops in C++:
1. for loop
2. while loop
3. do-while loop
1
, Each loop works in a slightly different way, depending on how and when the condition is
checked.
for Loop:
A for loop is used when we know exactly how many times we want to repeat a task. It consists
of three parts: initialization (starting point), condition (when to stop), and update (how to move
forward). The loop runs repeatedly until the condition becomes false.
Syntax:
for (initialization; condition; update)
{
// Code to execute
}
Where;
initialization: This is where we set a starting point (e.g., int i = 1; means i starts at 1).
condition: The loop runs as long as this condition is true (e.g., i <= 5; means keep going
until i reaches 5).
update: This tells how to change/update the value in each loop (e.g., i++ increases i by 1
each time).
Example No. 01: (Displays numbers from 1 to 5)
2
Lab Objective:
The primary objective of this lab is to provide students with a comprehensive understanding of
loops in C++. Students will learn how to use different types of loops, including for, while, and
do-while loops, to implement repetitive tasks efficiently. By the end of this lab, students should
be able to write C++ programs that utilize loops for tasks such as counting, iterating over arrays,
and handling user-defined conditions. Additionally, students will develop problem-solving skills
by implementing loops in various practical scenarios, ensuring optimized and structured code
development.
Hardware/Software Tools:
Hardware: Desktop/Computer
Software Tool: MS Visual Studio 2013
Lab Description:
Loops are a fundamental concept in programming that allow the execution of a block of code
multiple times based on a specified condition. In this lab, students will explore three primary
looping constructs in C++, the for loop, which is used when the number of iterations is known;
the while loop, which runs as long as a given condition remains true; and the do-while loop,
which ensures the code executes at least once before checking the condition. Through hands-on
exercises, students will practice writing C++ programs that perform repetitive tasks such as
generating number sequences, calculating sums, and processing arrays. The lab will also cover
nested loops, demonstrating how multiple loops can work together to solve complex problems.
By experimenting with different loop structures, students will gain confidence in writing
efficient and well-structured C++ programs.
Loops:
Loops are used in programming to repeat a set of instructions multiple times without writing the
same code again and again. Suppose we want to display Hello five times, instead of writing five
cout statements, we can use a loop to do it automatically. Loops keep running until a condition is
met, making them useful for counting numbers, processing lists, and solving repetitive tasks
efficiently.
There are three main types of loops in C++:
1. for loop
2. while loop
3. do-while loop
1
, Each loop works in a slightly different way, depending on how and when the condition is
checked.
for Loop:
A for loop is used when we know exactly how many times we want to repeat a task. It consists
of three parts: initialization (starting point), condition (when to stop), and update (how to move
forward). The loop runs repeatedly until the condition becomes false.
Syntax:
for (initialization; condition; update)
{
// Code to execute
}
Where;
initialization: This is where we set a starting point (e.g., int i = 1; means i starts at 1).
condition: The loop runs as long as this condition is true (e.g., i <= 5; means keep going
until i reaches 5).
update: This tells how to change/update the value in each loop (e.g., i++ increases i by 1
each time).
Example No. 01: (Displays numbers from 1 to 5)
2