Lab 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

LAB # 05: for loop and while loop

Tool used: Microsoft Visual Studio

While loop:

A loop is part of a program that repeats. The while loop has two important parts: (1) an
expression that is tested for a true or false value, and (2) a statement or block that is repeated as
long as the expression is true.

For loop:

A for loop is a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times. When you know exactly how many times you want to
loop through a block of code, use the for loop instead of a while loop.

Syntax

The syntax of a for loop in C++ is −

for ( init; condition; increment/decrement)


{
statement(s);
}
Figure 1: the loop’s logic.

Example
int main (){

}
Other Forms of the Update Expression :
You are not limited to using increment statements in the update expression. Here is a loop that
displays all the even numbers from 2 through 100 by adding 2 to its counter:

And here is a loop that counts backward from 10 down to 0:

Defining a Variable in the for Loop’s Initialization Expression:


Not only may the counter variable be initialized in the initialization expression, it may be defined
there as well. The following code shows an example.

Using Multiple Statements in the Initialization and Update Expressions:


It is possible to execute more than one statement in the initialization expression and the update
expression. When using multiple statements in either of these expressions, simply separate the
statements with commas. For example

Omitting the for Loop’s Expressions:


The initialization expression may be omitted from inside the for loop’s parentheses if it has
already been performed or no initialization is needed. Here is an example
Lab Tasks

1. Grocery Shopping List and Bill Calculator. The program should accomplish the
following tasks:
 Display a menu of grocery items with their respective prices.
 Ask the user to choose items they want to buy from the menu. For each selected
item, prompt the user to enter the quantity they wish to purchase. Ensure that the
quantity entered is greater than 0.
 Then display the final bill amount.
2. Write a C++ program to input basic salary of an employee and calculate its Gross salary
according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
3. Program to print natural numbers in reverse from n to 1 using while loop.
Note: check whether the number is positive or not.
4. Program to Generate Fibonacci Sequence up to a Certain Number. The Fibonacci
sequence is a series of numbers where a number is found by adding up the two
numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
and so forth.
5. Write a method with a while loop that computes the sum of first n positive integers: sum
= 1 + 2 + 3 + … + n.
6. Write a C++ program to calculate the approximate values of sin(x) and cos(x) using the
Taylor series expansion. You should allow the user to input the value of 'x' and the
number of terms in the series to use. The program should then display the calculated
values of sin(x) and cos(x).

You might also like