lect3_2

lect3_2 - Control Structures Repetition Statements 1...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Control Structures - Repetition Statements 1 Control Structures: Repetition Statements Most programs require some action that is repeated a number of times. For example, our rectangle area.cpp program computes the area once per one run of the program. If we want to compute the area for different sets of length and width values, we need to rerun the program again and again. A better approach than this is to modify the program in a way that the program repeats a group of statements. A section of a program that repeats a statement or group of statements is called a loop . There are many ways to create loops in C++. The while statement One of these ways is called a while statement or while loop . The general form of a while loop is as follows. while (condition) { // a group of statements } The portion between the braces, { and } , is called the body of the while loop. The statements in the loop body are repeated as long as the condition (logical expression) in the parentheses is true . When the condition is found false the execution of the loop body is terminated and the program transfers control to the next statement immediately following the closed cruly brace } at the end of the loop body.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Control Structures - Repetition Statements 2 We now modify our rectangle area program so that it repeatedly prompts the user for input of length and width and displays the area of the rectangle, as long as the current inputs of length > 0 and width > 0. #include <iostream> using namespace std; int main() { int length, width, area; cout << "Enter values for length & width seperated by a space:"; cin >> length >> width; { area = length * width; cout << endl << "Area of a rectangle with "; cout << "length = " << length << " and width = " << width; cout << " is " << area << endl; cout << "Enter values for length & width seperated by a space:"; cin >> length >> width; } cout << endl << "Good Bye!" << endl; return 0; } Output of a sample run of the program looks like this. Area of a rectangle with length = 12 and width = 6 is 72 Area of a rectangle with length = 8 and width = 4 is 32 Good Bye! In the above program run, the body of the loop is executed two times, because in the third set of inputs, the value for the width variable is 0 and therefore the loop terminates and the control is transferred to the next statement following the loop body, which is
Background image of page 2
Control Structures - Repetition Statements 3 cout << endl << "Good Bye!" << endl; Now, consider the following sample run.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

lect3_2 - Control Structures Repetition Statements 1...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online