5 loops (1)

27
Programming Fundamentals BSCS Semester1 Course Instructor: M. Nadeem Contact: [email protected]

Upload: remal-rana

Post on 11-Dec-2015

237 views

Category:

Documents


2 download

DESCRIPTION

not mine

TRANSCRIPT

Page 1: 5 Loops (1)

Programming Fundamentals

BSCS Semester1

Course Instructor: M. NadeemContact: [email protected]

Page 2: 5 Loops (1)

04/18/23 2

Loops & Its Logic

What is a Loop? An iterative statement is called loop. A type of control structure that

repeats a statement or set of statements is known as looping structures. It is also known as iterative or repetitive structure. There are two elements of the loop: the body of loop which is to be executed number of times and a loop condition which terminates the loop when a particular condition is met.

Repetition

A single statementA set of statements

Page 3: 5 Loops (1)

04/18/23 3

Types of Loop

Types of Loop Counter loop (for loop) Conditional loops (while, do while)

Types of Loop

For Loop While Loop Do While Loop

Page 4: 5 Loops (1)

04/18/23 4

For Loop Operation

Initialization Expression

Body of Loop

Increment Expression

ExitTest

Expression

True

False

Page 5: 5 Loops (1)

04/18/23 5

For Loop Structure

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

statement;

Initialization Expression

Increment Expression

Single Statement Loop Body

Test Expression

keyword

Page 6: 5 Loops (1)

04/18/23 6

For Loop Structure

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

{ statement; statement; statement;}

Initialization Expression

Test Expression

Increment Expression

Multiple Statement Loop Body

keyword

Page 7: 5 Loops (1)

04/18/23 7

For Loop

// demonstrate simple for loop

# include <iostream>

#include<conio.h>

void main()

{

int j;

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

printf(“%d ”, j * j;

getch();

}

Output: 0 1 4 9 16

Page 8: 5 Loops (1)

04/18/23 8

For Loop Variations

Multiple initialization and increment Expressions

for (x=0 , j=0 ; j < 5 ; j++ , x++)

Increment or Decrement

for ( j=5 ; j > 0 ; j-- )

for ( j=0 ; j <10 ; j+=2 )

controlling

Page 9: 5 Loops (1)

The Infinite loop

A loop that does not terminate is called

Infinite loop.

A loop who's condition always remain true is called infinite loop.

e.g.

for(int i=0;i>0;i++)

printf(“%d”,i);

04/18/23 9

Page 10: 5 Loops (1)

04/18/23 10

While Loop

while ( test expression ){Statement;Statement;Statement;}

while (test expression )statement;

Body of Loop

ExitTest

Expression

True

False

Single Statement Loop Body

Multiple Statement Loop Body

While Operation

Page 11: 5 Loops (1)

04/18/23 11

The body of the loop is executed until the condition becomes false. Here is a program which illustrates the working of the while loop.

void main ()

{

int c=0;

int i=0;

char str=‘y’;

while(str==’y’)

{

cout<<”Enter the number you want to add”<<endl;

scanf(“%d”,&i);

c=c+1;

cout<<”Do you want to enter another number y/n:”<<endl;

scanf(“%c”,&str);

}

printf(”The sum of the numbers are : %d“,c);

getch();

}

Page 12: 5 Loops (1)

// to find the average of as many numbers as user wants with while loop.

void main() { int n,sum,count; while(scanf(“%d”,&n)) { If(n==0) continue; sum=sum+n //sum+=n; count++; } Printf(“%d”,(sum/count)); }

04/18/23 12

Page 13: 5 Loops (1)

04/18/23 13

Do While Loop

Do{Statement;Statement;Statement;}while ( test expression );

Dostatement;

while (test expression );Body of Loop

ExitTest

Expression

True

False

Single Statement Loop Body

Multiple Statement Loop BodyMultiple Statement Loop Body

Do While Operation

Multiple Statement Loop Body

Page 14: 5 Loops (1)

The body of the loop is executed until the condition becomes false. Here is a program which illustrates the working of the do while loop.

void main ()

{

int c=0;

int i=0;

char str=‘y’;

do

{

cout<<”Enter the number you want to add”<<endl;

scanf(“%d”,&i);

c=c+1;

cout<<”Do you want to enter another number y/n:”<<endl;

scanf(“%c”,&str);

} while(str==’y’);

printf(”The sum of the numbers are : %d\n“,c);

getch();

}

04/18/23 14

Page 15: 5 Loops (1)

04/18/23 15

// to find the average of as many numbers as user wants with do while loop.

void main() { int n,sum,count; do { If(n==0) continue; sum=sum+n //sum+=n; count++; } while(scanf(“%d”,&n)); printf(“%d”,(sum/count)); }

Page 16: 5 Loops (1)

04/18/23 16

When to use Which Loop?

Advance knowledge For Loop

No Prior Knowledge While Loop Do While (executes at least once)

Page 17: 5 Loops (1)

04/18/23 17

Break & Continue Statement To exit a loop you can use the

break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition.

With “continue;” it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented).

EXAMPLE

int main() {

int j, total;for (int i=0; i<20;i++) {

cin>>j;if ( j == 10)

continue; total = i + j;

cout<<“total is=“<<total;} return 0;

}

Page 18: 5 Loops (1)

04/18/23 18

Continue statement With “continue;” it is possible to skip the rest of the commands in

the current loop and start from the top again. (the loop variable must still be incremented).

EXAMPLE

int main() {

int i;i = 0; while ( i < 20 ) {

i++; continue;printf(“hey wait! you are skipping me\n”);

} return 0;

}

Page 19: 5 Loops (1)

Nested Loop A loop within an other loop

is called nested loop. e.g. for(int i=1;i<=5;i++)

{ for(int j=1;j<=i;j++) { printf(“%d”,j); } printf(“\n”);

}

04/18/23 19

Output112123123412345

Page 20: 5 Loops (1)

04/18/23 20

Nested Loop for(int i=5;i>=1;i--)

{ for(int j=i;j>=1;j--) { printf(“%d”,j); } printf(“\n”);

}

Output543214321321211

Page 21: 5 Loops (1)

04/18/23 21

Nested Loop for(int i=5;i>=1;i--)

{ for(int j=1;j<=i;j++) { printf(“%d”,j); } printf(“\n”);

}

Output123451234123121

Page 22: 5 Loops (1)

04/18/23 22

Nested Loop for(int i=5;i>=1;i--)

{ for(int j=1;j<=i;j++) { printf(“*”); } printf(“\n”);

}

Output***************

Page 23: 5 Loops (1)

04/18/23 23

Nested Loop int i=5; while(i>=1)

{ int j=1;

while(j<=i) {

printf(“%d”,j); j++;

} i--; printf(“\n”);

}

Output123451234123121

Page 24: 5 Loops (1)

What makes a bad program?

Repeating trial and error without understanding the problem

Writing Code without detailed analysis and design (Abstraction, Algo)

Writing tricky and dirty programs

Page 25: 5 Loops (1)

Programms: Write a program using for, while and do while loops to:

Display numbers from 0 to 100 Display numbers from 100 to 0 Display even numbers from 0 to 100 Display odd numbers from 0 to 100 Display even numbers from 100 to 0 Display odd numbers from 100 to 0 Display Square of numbers from 0 to 100 Display Cube of numbers from 0 to 100 Display Square of numbers from 100 to 0 Display Cube of numbers from 100 to 0 Display Square of numbers from 40 to 100 Display Cube of numbers from 50 to 100 Display Square of numbers from 500 to 1000 Display Cube of numbers from 1000 to 1500 Display Average of even numbers from 1000 to 1200 Display Average of odd numbers from 1200 to 1000

04/18/23 25

Page 26: 5 Loops (1)

Programs: Write a program using for, while and do while and nested loops to display the

following outputs. You are also required to submit the dry runs of all these programs on paper.

04/18/23 26

12345678910123456789123456781234567123456123451234123121

===========**************** ******** **** ** *===========

********************************

11212312341234512345612345671234567812345678912345678910

Page 27: 5 Loops (1)

04/18/23 27

135791357913571357135135131

* *** ***** ******* ***** *** *

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

002024024602468024681002468101202468101214

Programs: Write a program using for, while and do while and nested loops to display the

following outputs. You are also required to submit the dry runs of all these programs on paper.