CCS003L Demerin (M6-TECHNICAL)
CCS003L Demerin (M6-TECHNICAL)
CCS003L Demerin (M6-TECHNICAL)
CCS0006L
(COMPUTER PROGRAMMING 1)
EXERCISE
6
REPETITION CONTROL STRUCTURE
Loops have as objective to repeat a statement a certain number of times or while a condition is
fulfilled.
int main ()
{
Format:
The while loop performs the conditional test first and then executes the loop, so the statements within
a loop may never be executed. The do...while loop performs the statements first and then tests the
condition. This means that the body of the loop is always executed at least once.
do
{
statements;
}while(test_condition);
Look carefully at the end of the loop. There is a semi-colon at the end of the while condition
statement. This terminates the whole statement and results in one more semi-colon than the equivalent
while loop.
// number echoer Enter number (0 to end): 12345
#include <iostream> You entered: 12345
using namespace std; Enter number (0 to end): 160277
You entered: 160277
int main () Enter number (0 to end): 0
{ You entered: 0
The for keyword marks the beginning of the code which will be repeated according to the conditions
supplied in the parenthesis following the for. The general form of the statement is
count = 0; // initialization
while (count < 10) // condition
{
cout << count;
count++; //increment
} // end while
The for loop can be incremented or decremented by any amount, for example the following are all
valid for statements.
and its main function is to repeat statement while condition remains true, like the while loop. But
in addition, for provides places to specify an initialization instruction and an increase instruction. So this
loop is specially designed to perform a repetitive action with a counter.
int main ()
{
for (int n=10; n>0; n--) {
cout << n << ", ";
}
cout << "FIRE!";
return 0;
}
This loop will execute 50 times if neither n nor i are modified within the loop:
n starts with 0 and i with 100, the condition is (n!=i) (that n be not equal to i). Because n is
increased by one and i decreased by one, the loop's condition will become false after the 50th loop, when
both n and i will be equal to 50.
int main ()
{ int n;
for (n=10; n>0; n--) {
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}}
return 0; }
The continue instruction causes the program to skip the rest of the loop in the present iteration as if
the end of the statement block would have been reached, causing it to jump to the following iteration. For
example, we are going to skip the number 5 in our countdown:
int main ()
{ for (int n=10; n>0; n--) {
if (n==5) continue;
cout << n << ", ";
}
cout << "FIRE!";
return 0; }
It allows making an absolute jump to another point in the program. You should use this feature
carefully since its execution ignores any type of nesting limitation.
The destination point is identified by a label, which is then used as an argument for the goto
instruction. A label is made of a valid identifier followed by a colon (:).
// goto loop example 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
#include <iostream>
using namespace std;
int main ()
{ int n=10;
loop:
cout << n << ", ";
n--;
if (n>0) goto loop;
cout << "FIRE!";
return 0; }
G. The exit function
The purpose of exit is to terminate the running program with a specific exit code. Its prototype
is:
The exit code is used by some operating systems and may be used by calling programs. By
convention, an exit code of 0 means that the program finished normally and any other value means an
error happened.
V. LABORATORY ACTIVITY
Using while loop, calculate the product of two numbers without using the multiplication (*) operator.
Sample Output:
Enter 1st number: 2
Enter 2nd number: 2
Product: 4
Using do while loop, write a program that will ask the user to input an integer number. Compute for the
factorial value. Factorial is the product of all positive integers less than or equal to n.
Sample Output:
Enter an integer number: 5
The factorial of 5 is: 120
Using for nested loop, write a program that will ask for a positive integer number. The program will display
an increasing number of printed star/s from 1 to the integer entered by the user. If the user entered an invalid
input such as 0 or any other input, display an error message.
Sample Output:
Enter a number: 5
*
**
***
****
Using any loop structure, write a program that computes for the sum of the natural numbers from 1 up to
the positive integer entered by the user.
Sample Output:
ACTIVITY 6.5: Up To
Using any loop structure, write a program that displays the numbers from the starting number up to the last
number. The starting number should be less than or equal to the last number otherwise display an error
message.
Sample Output:
Enter starting number: 3
Enter last number: 9
The numbers are: 3456789
int main() {
int x, no, y, total = 0;
cout << "How many stars? ";
cin >> no;
for (x = no; _____________; x--) {
total = _____________
for (_____________; y > 0; y--)
cout << "*";
cout << "\n";
}
cout << "Wow " << total << " stars in
all!!!";
return 0;
}
Code:
int main() {
int initial, terminal;
cout << "Enter initial value:";
cin >> initial;
cout << "Enter terminal value:";
cin >> terminal;
if (______________) {
cout << "Numbers in the range
divisible by 5:\n";
do {
if (______________)
cout << initial << " ";
initial++;
} while (______________);
}
else
cout << "Invalid entry.";
return 0;
}
Code:
int main() {
int x, no;
cout << "Enter number:";
cin >> no;
Code:
Since there are some of the problems that often need to be repeated, repetition controls
structures help the user to repeat a certain segment over and over. It is used when a program
needs to repeatedly process one or more instructions until the condition is met, at which time the
loops end. VENZON
Repetition control structures are used when a program needs to repeat one process or more,
until the condition/s is met, and then the loop ends. It is used to perform the same take over and
over again such as problems like searching, sorting, or optimization algorithms. DEMERIN
It was used in activity 6.2. It’s used in that activity to repeat the formula as long as the input
number is greater than zero. DEL ROSARIO
do-while is used if the user wants a certain value to be repeated as long as the conditional
expression evaluates to true. Just like in activity 6.5 where to program continuously printing
numbers from the first value until the last number that the user input and then stops when it
reaches the given last value. VENZON
In Activity 6.2 and Activity 6.5, do-while is used. The do-while loop is used when a piece of code
has to be executed once and then repeated as long as a given condition is true. One such
scenario is user input validation, in which you want to check that the user enters a legitimate
value before proceeding with the program logic. Other issue cases where do-while loops are
beneficial include menu-driven applications, debugging and troubleshooting, and iterative
methods that need the code block to be completed at least once. DEMERIN
VII. REFERENCES
• Abraham (2015). Coding for dummies. John Wiley and Sons: Hoboken, NJ
• Zak, D (2015). An Introduction to Programming with C++. 8th Edition
• Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th Edition).Sams Publishing
RUBRIC:
Criteria 4 3 2 1 Score
A completed
A completed solution is An incomplete
A completed
solution is implemented solution is
solution runs
tested and runs on the required implemented
without errors.
but does not platform, and on the required
It meets all the
meet all the uses the platform. It
specifications
specifications compiler does not
and works for
nd/or work for specified. It compile and/or
all test data.
all test data. runs, but has run.
Solution(x5) logical errors.