Docsity

Lab Experiment 7: Introduction to Programming Loops in C++, Lab Reports of Programming Languages

Toggle sidebar
bg1
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1ST SEMESTER, 1ST Year) LAB EXPERIMENT # 7
________________________________________________________________________
Name: _____________________________________________ Roll No: _____________
Score: ____________Signature of the Lab Tutor: _____________ Date: 17/01/2017
________________________________________________________________________
PERFORMANCE OBJECTIVE
Upon successful completion of this experiment, the student will be able to learn:
CONTROL STRUCTUERE, WHILE, DO-WHILE AND FOR LOOPS IN C++
A loop is a control structure that causes a statement or group of statements to repeat. C++ has
three looping control structures: the while loop, the do-while loop, and the for loop. The
difference between each of these is how they control the repetition.
The While Loop:
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.
Let’s look at each part of the while loop. The first line, sometimes called the loop header,
consists of the key word while followed by a condition to be tested enclosed in parentheses. The
condition is expressed by any expression that can be evaluated as true or false. Next comes the
body of the loop. This contains one or more C++ statements.
Here’s how the loop works. The condition expression is tested, and if it is true, each statement
in the body of the loop is executed. Then, the condition is tested again. If it is still true, each
statement is executed again. This cycle repeats until the condition is false.
To introduce Control Structure in C++ (for loop, while loop, Do while loop,
Nested for Loop
while (condition)
{
statement;
statement;
// Place as many statements
// here as necessary
}
pf3
pf4
pf5

Partial preview of the text

Download Lab Experiment 7: Introduction to Programming Loops in C++ and more Lab Reports Programming Languages in PDF only on Docsity!

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 7

________________________________________________________________________

Name: _____________________________________________ Roll No: _____________

Score: ____________Signature of the Lab Tutor: _____________ Date: 17/01/ ________________________________________________________________________

PERFORMANCE OBJECTIVE

Upon successful completion of this experiment, the student will be able to learn: CONTROL STRUCTUERE, WHILE, DO-WHILE AND FOR LOOPS IN C++

A loop is a control structure that causes a statement or group of statements to repeat. C++ has three looping control structures: the while loop, the do-while loop, and the for loop. The difference between each of these is how they control the repetition.

The While Loop: 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.

Let’s look at each part of the while loop. The first line, sometimes called the loop header, consists of the key word while followed by a condition to be tested enclosed in parentheses. The condition is expressed by any expression that can be evaluated as true or false. Next comes the body of the loop. This contains one or more C++ statements. Here’s how the loop works. The condition expression is tested, and if it is true, each statement in the body of the loop is executed. Then, the condition is tested again. If it is still true, each statement is executed again. This cycle repeats until the condition is false.

To introduce Control Structure in C++ (for loop, while loop, Do while loop,

Nested for Loop

while (condition) { statement; statement; // Place as many statements // here as necessary }

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 7

________________________________________________________________________

Program Output Hello Hello Hello Hello Hello That's all!

Each execution of a loop is known as an iteration. This loop will perform five iterations before the expression number <= 5 is tested and found to be false, causing the loop to terminate. The program then resumes execution at the statement immediately following the loop. A variable that controls the number of time a loop iterates is referred to as a loop control variable. In the example we have just seen, number is the loop control variable.

While loop can also be used for input validation. For example we know that marks of a subject have to be between 1 and 100. This is how while loop can be used in this case:

cout << "Enter a number in the range 1 - 100: "; cin >> number; while ((number < 1) || (number > 100))

// This program demonstrates a simple while loop.

void main() { int number = 1;

while (number <= 5) { cout << "Hello "; number++; } cout << "\nThat's all!\n"; return 0; }

While is a Pretest Loop which means it tests the condition before every iteration

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 7

________________________________________________________________________

In essence, the loop counts to twelve and asks the user to enter a sales amount each time it makes a count. A count-controlled loop must possess three elements:

  1. It must initialize a counter variable to a starting value.
  2. It must test the counter variable by comparing it to a final value. When the counter variable reaches its final value, the loop terminates.
  3. It must update the counter variable during each iteration. This is usually done by incrementing the variable.

for (initialization; test; update) { statement; statement; // Place as many statements // here as necessary. } As with the other loops you have used, if there is only one statement in the loop body, the braces may be omitted.

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO

INTRODUCTION TO PROGRAMMING

(1ST^ SEMESTER, 1 ST^ Year) LAB EXPERIMENT # 7

________________________________________________________________________

Program OutpuT Number Squared


1 1 2 4 3 9 4 16 5 25

EXERCISE

  1. Write a program using loops that displays numbers from 1 to 127 and their respective ASCII codes in front of them.
  2. Write a program that asks the user to input a number ‘n’. Then it displays sum of all numbers from 1 to n.
  3. Write a program that asks the user for a positive number less than 15. The number entered by the user will be length of each side of square as shown below:

Let’s say user entered 5: XXXXX XXXXX XXXXX XXXXX XXXXX

// This program uses a for loop to display the numbers 1- 5 // and their squares. #include #include

void main() { int num;

cout << "Number Square\n"; cout << "--------------\n";

for (num = 1; num <= 5; num++) cout << num << \t << (num * num) << endl; return 0; }