0% found this document useful (0 votes)
62 views

Worksheet 4 (Looping Statements One)

The document contains examples of various looping statements in C++ like while, do-while, and for loops. It provides 14 problems with sample code using these loops and asks the reader to trace the execution and output of each loop. The loops demonstrate basic and nested looping structures used to iterate, print values, and perform repetitive tasks in C++ programming.

Uploaded by

sebsbe ayalew
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Worksheet 4 (Looping Statements One)

The document contains examples of various looping statements in C++ like while, do-while, and for loops. It provides 14 problems with sample code using these loops and asks the reader to trace the execution and output of each loop. The loops demonstrate basic and nested looping structures used to iterate, print values, and perform repetitive tasks in C++ programming.

Uploaded by

sebsbe ayalew
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

HiLCoE

School of Computer Science & Technology

CS530 Problem Solving using Computer programming I Autumn 2021


Worksheet 4 (Looping statements)
Objective:
 To see how looping statements of C++ handle repetitive tasks

1. Hand trace the following ‘while statements‘ and tell their outputs.

a.

while(0) f.
cout<<"Hellow world\n"; int count=2;
while(count<=8)
b. {
cout<<count<<" ";
while(1) count++;
cout<<"Hellow world\n"; }

c. if(count==9)
cout<<"\ncount = "<<count+1;
while(1); else
cout<<"Hello world \n"; cout<<"\ncount = "<<count+2;

d.
g.
int i=1;
while(i<=5) int maxSize=5;
{ float num, average,sum=0;
cout<<"Hellow world\n"; int i=1;
i++;
} cout<<"\nEnter 5 numbers\n\n";
// Say the user enters 10,20,30,40 & 50

while(i<=maxSize)
e. {
int k=5; cout<<"Number "<<i<<" = ";
while(k>=1) cin>>num;
{ sum=sum+num;
cout<<endl<<k<<" "; i++;
k--; }
} average=sum/maxSize;
cout<<"\nSum = "<<sum;
cout<<"\nAverage = "<<average;

Page | 1
2. Hand trace the following ‘do… while’ statements and tell their outputs.

a. }while(j>=0);
do
cout<<"Hellow world\n";
while(0);
f.
b.
do float num;
cout<<"Hellow world\n"; int flag;
while(1); cout<<"\nEnter any number\n\n";
// Say the user enters 10,20,30,40 & 50
c. do
{
int i=1;
cout<<"Number = ";
do
cin>>num;
{
if(num<0)
cout<<"Hellow world\n";
{
i++;
cout<<"You have just"
} while(i<=5);
<<"entered a -ve no.";
d. flag=0;
int count=3; }
do else if(num==0)
{ {
cout<<count<<" "; cout<<"You have just"
count++; <<"entered zero.";
}while(count<=9); flag=0;
}
e.
else
int j=6;
flag=1;
do
{
}while(flag==1);
cout<<j<<" ";
j--;
cout<<"\nYou are out of the Loop";

3. Hand trace the following ‘for statements‘ and tell their outputs.

a.
d.
int i;
for(i=0; i<=5; i++) int i=0;
cout<<i<<" "; for( ; i<=5 ; i++)
cout<<i<<" ";
b.
e.
for(int i=0; i<6; i++) int i=0;
cout<<i<<" "; for( ; i<=5 ; )
{
c. cout<<i<<" ";
i++;
for(int i=0; i<=5; i=i+1) }
cout<<i<<" ";
Page | 2
f.

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


cout<<i<<" ";
k.
for(int i=1;i<=5;i++)
g. {
for(int j=1;j<=i;j++)
for(int i=0;i<=5;cout<<i<<" ",i++); cout<<j;
cout<<endl;
}
h.

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


cout<<i<<" "; l.
for(int i=1;i<=3;i++)
{
i. for(int j=1;j<=3;j++)
for(int i=0;i<=5;i++) cout<<j;
{ cout<<endl;
cout<<"\nWhen i = "<<i<<endl; }
cout<<"The inner loop looks like";
for(int j=0;j<=4;j++)
cout<<"j = "<<j<<" ";
m.
} int k=1;
for( int i=1;i<=3;i++)
{
for(int j=k;j<k+3;j++)
j.
cout<<j;
k=j;
for(int i=1; i<=5; i++)
cout<<endl;
{
}
cout<<"\nWhen i ="<<i<<endl;
cout<<"The inner loop looks like";
for(int j=1; j<=i; j++)
n.
cout<<"j = "<<j<<" ";
for(char i='a';i<='f';i++)
}
{
for(char ch='a' ; ch <=i ; ch++)
cout<<ch;

Page | 3

You might also like