Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Manual # 7

Topic:
While Loop, Do-While Loop, Nested
While & Do-While Loops

Subject:
Introduction to Computing

Author:
Engr. Ali Faisal Murtaza

1
------------------------------------------------------------------------------------------------------------
In a For Loop, you can initialize multiple variables and multiple conditions like:

#include<iostream.h>

int main()
{

for(int i=0, int j=5; i>=0 && j<=8;i++,j++)


{
cout<<i<<" "<<j<<"\n";
}
cout<<"\n\n";

return 0;
}

Note the output of the following program:

#include<iostream.h>

int main()
{

for(int i=0, int j=5; i>=0 && j<=8;i++)


{
cout<<i<<" "<<j<<"\n";
}
cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------

2
------------------------------------------------------------------------------------------------------------
Write a program which will create the sequence of numbers from 1 to 26.
Note the output of the following program:

Without Int & Inc With Int & Without Inc With Int & Inc

#include<iostream.h> #include<iostream.h> #include<iostream.h>

int main() int main() int main()


{ { {
int i; int i; int i;

while(i<=26) i=0; i=0;


{ while(i<=26) while(i<=26)
cout<<i<<" "; { {
cout<<i<<" ";
} cout<<i<<" ";
i++;
cout<<"\n\n"; } }

return 0; cout<<"\n\n"; cout<<"\n\n";


}
return 0; return 0;
} }

Program can also be initialized as:

#include<iostream.h>

int main()
{
int i=0;

while(i<=26)
{
cout<<i<<" ";

i++;
}

cout<<"\n\n";

return 0;
}

3
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
Write a program which will display the English language letters in both capital & small
letters.

#include<iostream.h>

int main()
{
int i;
char a;

a='A';

i=0;
while(i<26)
{
cout<<a<<" ";

a++;
i++;
}

cout<<"\n";

a='a';

i=0;
while(i<26)
{
cout<<a<<" ";

a++;
i++;
}

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Write a program which will fill the entire screen with a smiling face. The smiling face
has an ASCII value 1.

#include<iostream.h>

4
int main()
{
char a;

a=1;

while(1)
{
cout<<a;
}

return 0;
}
------------------------------------------------------------------------------------------------------------
Q1: Write a program to print all prime numbers from 1 to 300.
------------------------------------------------------------------------------------------------------------
Nested While-Loops:
I want to display the sequence of numbers from 0 to 10 ten times:

Note the output of the programme:

#include<iostream.h>

int main()
{
int i,j;

i=0;
j=0;

while(i<=10)
{
while(j<=10)
{
cout<<j<<" ";
j++;
}

i++;
cout<<"\n";
}

cout<<"\n\n";

return 0;

5
}
Error occurs in initialization.
Note the following program:
#include<iostream.h>

int main()
{
int i,j;

i=0;

while(i<=10)
{
j=0;
while(j<=10)
{
cout<<j<<" ";
j++;
}

i++;
cout<<"\n";
}

cout<<"\n\n";

return 0;
}
------------------------------------------------------------------------------------------------------------
Q2: Write a program in which the user gives the four values of ages. The program should
be schemed in such a way that it will take the three ranges, first range b/w first and
second value, second range b/w second and third value, third range b/w third and fourth
value.
Now consider the table below according to which the program takes the first
range and then tells you that the first range has this much sequence of ages in the
following category. And then pick the second range and so on.

Range of Age Category


0-15 Teenager
16-32 Young
33-55 Mid-Age
56 to onwards Old-Age
----------------------------------------------------------------------------------------------------------
Q3: Write a program in which a user can enter the range of values. Now, the scheme of
the program should be like that first the program will display all the prime numbers

6
present in the sequence and after that sequence the program should display separate even
and odd numbers in that sequence.
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
Q4: According to a study, the approximate level of intelligence of a person can be
calculated using the following formula:
i = 2 + ( y + 0.5x)
Write a program, which will produce a table of values of i, y and x, where y varies from 1
to 6, and for each value of y, x varies from 5.5 to 12.5 in steps of 0.5 .
------------------------------------------------------------------------------------------------------------
Q5: Write a program which will create the following output:

------------------------------------------------------------------------------------------------------------
Note: You can try all these programs using do-while loop.
------------------------------------------------------------------------------------------------------------

You might also like