14
Computer Science Department LOOPS

Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Embed Size (px)

Citation preview

Page 1: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

LOOPS

Page 2: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

Loops

• Loops Cause a section of your program to be repeated a certain number of times.

• The repetition continues while a condition is true. When condition becomes false, the loops ends and control passes to the statements following the loop.

• Types of Loops– for loop– while loop– do while loop

Page 3: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

for Loop• for loop execute a section of code a fixed number of

times. Its usually used when you know, before entering the loop, how many times you want to execute the code.

• The for allows us to specify three things about a loop in a single line.

a) Setting a loop counter to an initial value.

b) Testing the loop counter to determine whether its value has reached the number of repetitions desired.

c) Increasing the value of loop counter each time the program segment within the loop has been executed.

Page 4: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

for Loop

• The general form of for statement is as under.

for ( initialization expression; test expression; increment expression)

{

statement 1;

statement 2;

.

.

statement n;

}

Page 5: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

for Loop Example

void main ( )

{

int j;

for ( j=0; j < 15; j++ )

cout<< j;

getche( );

}

Page 6: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

for Loop• The Initialization Expression

– The initialization expression is executed only once, when the loop first starts. It gives the loop variable an initial value.

• The Test Expression– The test expression usually involves a relational

operator. It is evaluated each time through the loop, just before the body of the loop is executed. It determine whether the loop will be executed again or not.

• The Increment Expression– The increment expression changes the value of the

loop variable, often by incrementing it. It is always executed at the end of the loop, after the loop body has been executed.

Page 7: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

Nesting of LoopsLoops can also be nested like if statements.

void main ( )

{

int r, c, sum;

for ( r = 1; r<= 3; r++)

{

for( c =1; c <= 2; c++)

{

sum = r + c;

cout<<“r =”<< r;

cout<<“ c =”<< c;

cout<<“sum =”<<sum<<endl;

}

}

}

Page 8: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

Variable Visibility

• The loop body, which consists of braces delimiting several statements, is called a block of code.

• Variable defined inside the block is not visible outside it.

• Visible means that program statements can access the variable.

Page 9: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

Variable Visibility Example

void main ( )

{

for ( int i=0; i < 10; i++)

{

int j=i;

cout<< “hello world”<<j<<endl;

}

cout<< j; //Error: j is undefined.

}

Page 10: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

while Loop

• The while statement, like if statement, test a condition.

• The syntax of while loop is

while ( expression )

{

statement 1;

statement 2;

}

Page 11: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

while Loop Example

void main ( )

{

int n = 99;

while ( n != 0 )

{

cout<<“Enter any number or 0 to quit the program”;

cin >> n;

}

getche( );

}

Page 12: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

Tips

• The statement within the while loop would keep on getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop.

• The condition being tested may use relational or logical operators as shown in the following example.– while ( i <= 10 )– while ( i <= 10 && j <= 15 )– while ( j > 10 && ( b < 15 || c < 20))

Page 13: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

Tips• It is not necessary that a loop counter must be an int. It

can even be a float.

void main ( )

{

float a=10.0;

while ( a <= 10.5 )

{

cout<<a;

a = a + 0.1;

}

}

Page 14: Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The

Computer Science Department

Exercise

• The process of finding the largest number (i.e., the maximum of a group of numbers) is used frequently in computer applications. For example, a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write a pseudocode program, then a C++ program that inputs a series of 10 numbers, and determines and prints the largest of the numbers.