• No results found

CSL 113 Lab04

N/A
N/A
Protected

Academic year: 2020

Share "CSL 113 Lab04"

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

Semester 02 (Spring 2016)

Instructor: Ms. Fasiha /Ms. Sumeera Lab Engineer: Mr. M. Talha/Mr.Tarwan

Lab 04: Loops

Objective(s) : Upon completion of this lab session, learners will be able to: 1. Apply Loops in C++

2. Use of While Loop in C++ 3. Use of For Loop in C++ 4. Use of Do-While Loop in C++ 5. Use of Pretest and Post-test loop

6. Use of Event and Counter Controlled Loop

Loops

Loops are basically means to do a task multiple times, without actually writing the repeated statements over and over again. There two types of loops. Pretest Loop and Post-test Loop.

Pretest Loop: In each iteration, the loop control expression is tested first. If it is true, the loop action(s) is executed. If it is false, the loop is terminated.

(2)

1. While Loop

The condition is evaluated, and if it is true, the code within the block is executed. This repeats until the condition becomes false. We can think of a while loop as a repeating if statement.

The format of a while loop is as follows:

while (condition)

{

Statements;

}

If the block of statements to be repeated is one, then the brackets { } are not required.

Example:

// Output

Enter an integer between 1 and 100: 15

15 14 13 12 11 10 9 8 7 6

5 4 3 2 1

(3)

For loop is another type of loop which allows for initialization and iteration control. The initialization part is executed first followed by the condition. If the condition is true, the code within the block is executed. Once at the end of the loop, we go to update, then condition. If the condition is true the statements are executed again and so on. Note that the execution part is executed only one time when the for loop starts.

The format of a for loop is as follows:

for (initialization; condition; update) {

statements; }

// Output

Enter an integer between 1 and 9: 5

1

12

123

1234

12345

(4)

Do-while is useful for things that we want to perform at least once because here the loop is executed at least one time. The cause for this is the condition being tested at the end and not at the beginning like for and while loops. Don’t forget to include the semi-colon after the condition of while.

The format of a do-while loop is as follows:

do {

statements;

} while (condition) ;

// Output

Enter an integer between 1 and 100: 0

0

// Output

(5)

EXERCISES

Exercise 1

Write a C++ program to print the following pattern on the screen. Ask user to enter a integer value from 1 to 10. Use while loop.

#Sample Program Run#1

Enter a integer value: 7 X X X X X X X

X X X X X X X X X X X X X X X X X X X X X

Exercise 2

By applying for loop, write a C++ program that prints

a) A sequence of numbers in ascending order from 1 to 100 (inclusive).

b) Modify your program in part (a) to make it prints odd numbers within 1 to 100.

c) Write a C++ Program that receives a positive integer (N) and prints a series of numbers from 1 to N (inclusive) in ascending order.

d) Write a C++ Program that displays a series of alphabets in descending order from ‘Z’ to ‘A’.

e) Modify your program in part (d) so that the program will display consonants only, no vowels.

f) Write a C++ program that receives start value and end value. Then, your program will display a series of numbers from the start value to the end value inclusive.

Exercise 3

Write a C++ program that take two numbers from user, min and max. Use do-while to make sure that the user enters the smaller value first. Then your program will compute the sum of all integer numbers from min to max. Use do While loop.

Your Program should display output as follows:

#Sample Program Run#1

(6)

Exercise 4

Write a C++ program that takes a positive integer from user and store it in variable

posNumber. Follow these conditions;

If the number is less than 1, print wrong input. If it is 1, Print its value.

If value is greate than 1, check the value is it Even or Odd. If it is Even, half it and print.

If it is Odd, multiply it by 3 and print result. Repeat the whole process until user enter 1.

#Sample Program Run#1

Enter a integer value > = to 1: 0 Wrong Input, please try again. Enter a integer value > = to 1: 1 Value is : 1. Thank You

#Sample Program Run#2

Enter a integer value > = to 1: 6 Value is 3

Enter a integer value > = to 1: 11 Value is 33

Enter a integer value > = to 1: 56 Value is 23

Enter a integer value > = to 1: 1 Value is 1. Thank You.

Exercise 5

Write a C++ program that receives the total number of integers (N). Then, the program will ask for N real numbers. By applying for loop, your program should find and display the largest and the lowest of the numbers. Give a proper message for invalid user input.

#Sample Program Run#1

How many numbers do you have? > -3

Sorry, you have entered an invalid input. Thank you.

#Sample Program Run#2

(7)

Opps, you don't have any number for me to process. Thank you.

#Sample Program Run#3

How many numbers do you have? > 5 Please enter a number_1 --> 12.5 Please enter a number_2 --> -3.2 Please enter a number_3 --> 0 Please enter a number_4 --> 5 Please enter a number_5 --> 8.5

(8)

References

Related documents

Sealing Pavers - Around Salt Water & Chlorinated Water Pools Natural Finish Sealer, Multi Surface Waterproofer, Thompson’s Water Seal, Thompson’s Water Seal Ultra, Pool

The health communication literature relates to the opioid crisis as it provides health care providers tools and suggestions on how to best discuss sensitive and personal

Result in white v ferguson final verdict defendant in question of the challenge of race equality of education four years.. Questioned is not the plessy final decision of did the

The model determines MSB population using either an annual growth rate (which can be changed by the user to vary over time), or by using a components-of- change demographic

We analyze how companies which have Total Quality Management (TQM) and implanting a Reverse Logistics System (RLS) improve their recovery activities and reusability of materials and

14 When black, Latina, and white women like Sandy and June organized wedding ceremonies, they “imagine[d] a world ordered by love, by a radical embrace of difference.”

For the discrete coupon collector's process on Sn with sample probabilities given by vector p - 1 the stopping time TD can be represented as a sum of n

Mdivani’s professional work and leadership have been recognized by the Missouri Bar Association’s President Award, Association for Women Lawyers of Greater Kansas City