Professional Documents
Culture Documents
Fundamentals
BSCS Semester1
Course Instructor: M. Nadeem
Contact: Muhammad.nadeem@cs.uol.edu.pk
What is a Loop?
09/15/15
Types of Loop
Types of Loop
09/15/15
While Loop
Do While Loop
Test
Expression
False
Exit
True
Body of Loop
Increment Expression
09/15/15
Increment
Expression
keyword
statement;
09/15/15
Increment Expression
keyword
{ statement;
statement;
statement;
09/15/15
For Loop
// demonstrate simple for loop
# include <iostream>
#include<conio.h>
void main()
{
int j;
for (j=0 ; j < 5 ; j++)
printf(%d
getch();
}
Output:
0
09/15/15
, j * j;
16
j++ , x++)
controlling
Increment or Decrement
While Loop
Single Statement Loop Body
09/15/15
{
Statement;
Statement;
Statement;
}
False
Exit
True
Body of Loop
While Operation
10
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();
}
09/15/15
11
09/15/15
12
Do While Loop
Single Statement Loop Body
Do
statement;
while (test expression );
Do
09/15/15
{
Statement;
Statement;
Statement;
}
while ( test expression );
Body of Loop
Test
Expression
False
Exit
True
Do While Operation
13
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();
}
09/15/15
14
15
For Loop
No
09/15/15
knowledge
Prior Knowledge
While Loop
Do While
16
09/15/15
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;
}
17
Continue statement
09/15/15
int main()
{
int i;
i = 0;
while ( i < 20 )
{
i++;
continue;
printf(hey wait! you are skipping me\n);
}
return 0;
}
18
Nested Loop
A
for(int j=1;j<=i;j++)
{
printf(%d,j);
}
printf(\n);
Output
1
12
123
1234
12345
}
09/15/15
19
Nested Loop
for(int
09/15/15
i=5;i>=1;i--)
for(int j=i;j>=1;j--)
{
printf(%d,j);
}
printf(\n);
Output
54321
4321
321
21
1
20
Nested Loop
for(int
09/15/15
i=5;i>=1;i--)
for(int j=1;j<=i;j++)
{
printf(%d,j);
}
printf(\n);
Output
12345
1234
123
12
1
21
Nested Loop
for(int
09/15/15
i=5;i>=1;i--)
for(int j=1;j<=i;j++)
{
printf(*);
}
printf(\n);
Output
*****
****
***
**
*
22
Nested Loop
int
i=5;
while(i>=1)
{
int j=1;
while(j<=i)
{
printf(%d,j);
j++;
}
i--;
printf(\n);
Output
12345
1234
123
12
1
}
09/15/15
23
Programms:
09/15/15
25
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.
12345678910
123456789
12345678
1234567
123456
12345
1234
123
12
1
09/15/15
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
*
***
*****
*******
*******
*****
***
*
===========
****************
********
****
**
*
===========
26
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.
13579
13579
1357
1357
135
135
13
1
09/15/15
0
02
024
0246
02468
0246810
024681012
02468101214
&
&&
&&&
&&&&
&&&&&
&&&&&
&&&&
&&&
&&
&
*
***
*****
*******
*****
***
*
27