Computer Fundamentals and Programming
REPTITIVE / ITERATIVE PROGRAM STRUCTURE
Pretest Loop
1.) while loop
Syntax: initialization;
while (condition) //no semicolon after parenthesis
{
statement/s;
update;
}
2.) for loop
Syntax: for (initialization; condition; update) //no semicolon after parenthesis
{
statement/s;
Posttest Loop
1.) do-while loop
Syntax: initialization;
do
{
statement/s;
update;
} while (condition);
Course Material for CS10 Page 1 of 7
Computer Fundamentals and Programming
Fixed-Count Loop Examples
1.) Create C++ program that will generate the following number series:
10, 5, 9, 10, 8, 15, 7, 20, 6, and 25
C++ Program
#include<iostream>
using namespace std;
int main()
{ int X, Y;
cout<<"Solution 1 - using while"<<endl;
X = 10;
Y = 5;
while (X >= 6)
{
cout<< X<<endl<<Y<<endl;
X = X - 1;
Y = Y + 5;
}
cout<<endl<<endl;
cout<<"Solution 2 - using for loop"<<endl;
for (X = 10, Y = 5; Y <= 25; X --, Y += 5)
{
cout<< X<<endl<<Y<<endl;
}
cout<<endl<<endl;
cout<<"Solution 3 - using do-while loop"<<endl;
X = 10;
Y = 5;
do
{
cout<< X<<endl<<Y<<endl;
--X;
Y += 5;
} while (X >= 6);
return 0;
}
Course Material for CS10 Page 2 of 7
Computer Fundamentals and Programming
2.) Create a C++ program that will generate the following number series:
1, 2, 4, 7, 11, 16, 22, 29, 37, and 46
C++ Program
#include<iostream>
using namespace std;
int main()
{ int X, Y;
cout<<"Solution 1 - using while"<<endl;
X = 1;
Y = 1;
while (Y <= 46)
{
cout<<Y<<"\t";
Y = X + Y;
X = X + 1;
}
cout<<endl<<endl;
cout<<"Solution 2 - using for loop"<<endl;
for (X = 1, Y = 1; X <= 10; Y+=X, X++)
{
cout<<Y<<"\t";
}
cout<<endl<<endl;
cout<<"Solution 3 - using do-while loop"<<endl;
X = 1;
Y = 1;
do
{
cout<<Y<<"\t";
Y+=X;
++X;
} while (Y <= 46);
return 0;
}
Course Material for CS10 Page 3 of 7
Computer Fundamentals and Programming
3.) Create a C++ program that will generate ten random numbers, range is from 1 to 10.
Output the sum of all the random numbers count and output how many even and odd
numbers were generated. Prompt the user to press any key to try again or press X/x to
quit.
C++ Program
#include <iostream>
#include <cctype> //toupper or tolower
#include <cstdlib> //srand and rand
#include <ctime> //time
#include <windows.h> //system
using namespace std;
int main()
{ int rndNum, sum, ctr, ctrEven, ctrOdd;
char tryagain;
do{
system("cls");
cout<<"The ten random numbers generated by the computer : "<<endl;
srand(time(0));
for(ctr=1, sum=0, ctrEven=0, ctrOdd=0; ctr<=10; ctr++)
{
rndNum = rand() % 10 + 1; //generates 1 to 10
sum = sum + rndNum;
if (rndNum % 2 == 0)
ctrEven++;
else
ctrOdd++;
cout<<rndNum<<"\t";
}
cout<<endl<<endl;
cout<<"Sum of all the random numbers generated = "<<sum<<endl;
cout<<"There is/are "<<ctrEven<<" even numbers generated"<<endl;
cout<<"There is/are "<<ctrOdd<<" odd numbers generated"<<endl;
cout<<endl<<endl;
cout<<"Press any key to try again and [X/x] to exit."<<endl;
cin>>tryagain;
tryagain = tolower(tryagain);
}while(tryagain != 'x');
system("pause");
return 0;
Course Material for CS10 Page 4 of 7
Computer Fundamentals and Programming
4.) Create a C++ program that will generate random numbers in the range of -10 to 10,
terminated by a zero (0) sentinel. Output how many random numbers generated (0
sentinel excluded), count and output how many positive and negative numbers were
generated. Prompt the user to press any key to try again or press X/x to quit.
C++ Program
#include <iostream>
#include <cctype> //toupper or tolower
#include <cstdlib> //srand and rand
#include <ctime> //time
#include <windows.h> //system
using namespace std;
int main()
{ int rndNum, totalNum, ctr, ctrPos, ctrNeg;
char tryagain;
do{
system("cls");
cout<<"The random numbers generated by the computer in the range of -10 to 10:
"<<endl;
srand(time(0));
ctr = 0;
ctrPos = 0;
ctrNeg = 0;
do
{
rndNum = rand() % 21 - 10; //-10 to 10
cout<<rndNum<<endl;
if(rndNum != 0)
ctr++;
if (rndNum > 0)
ctrPos++;
if (rndNum < 0)
ctrNeg++;
}while(rndNum != 0);
cout<<endl<<endl;
cout<<"There is/are "<<ctr<<" random numbers generated excluding zero (0) is
"<<ctr<<endl;
cout<<"There is/are "<<ctrPos<<" positive numbers generated"<<endl;
cout<<"There is/are "<<ctrNeg<<" negative numbers generated"<<endl;
cout<<endl<<endl;
cout<<"Press any key to try again and [X/x] to exit."<<endl;
Course Material for CS10 Page 5 of 7
Computer Fundamentals and Programming
cin>>tryagain;
tryagain = tolower(tryagain);
}while(tryagain != 'x');
system("pause");
return 0;
}
Course Material for CS10 Page 6 of 7
Computer Fundamentals and Programming
Course Material for CS10 Page 7 of 7