0% found this document useful (0 votes)
34 views12 pages

Gulis Laboratory 5

The document describes an experiment on control structures using iteration in C++ programming. Students are asked to write an algorithm, flowchart, and program to display a vertical diamond pattern using nested loops with different iteration statements. The procedure involves getting user input for number of rows, and printing the top and bottom halves of the diamond pattern based on the input using while, do-while, and for loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views12 pages

Gulis Laboratory 5

The document describes an experiment on control structures using iteration in C++ programming. Students are asked to write an algorithm, flowchart, and program to display a vertical diamond pattern using nested loops with different iteration statements. The procedure involves getting user input for number of rows, and printing the top and bottom halves of the diamond pattern based on the input using while, do-while, and for loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Course: CPE 102A - Computer Programming Experiment No.

: 5
Name: John Kenneth S. Gulis Section: EE21S1
Date Performed: November 12, 2021
Date Submitted: November 13, 2021
Instructor: Engr. Julie Ann Susa

LABORATORY ACTIVITY NO. 5


CONTROL STRUCTURE
(Iteration Structure)
1. Objective(s):

The activity aims to introduce a iteration structure using analytical tools such as algorithm and flowchart to evaluate the
output based on the requirement whether true or false

2. Intended Learning Outcomes (ILOs):


The students shall be able to:

1. Understand the Iteration structures and how to simplify sequential number.


2. Construct a program that applies different forms of Iterative statement.
3. Simplify a program with applicable iterative statement.

3. Discussion:
Iteration statements cause statements (or compound statements) to be executed zero or more times, subject to some
loop-termination criteria. When these statements are compound statements, they are executed in order, except when
either the break statement or the continue statement is encountered “cout” and “cin” statements are used to display and
accept values in C programming. Programs do not only perform accepting and displaying values. It also does analysis
on a certain problem. A condition that was not able to be attained in the program should not be left hanging on the code
but instead there should be another event or statement capable of giving producing outcome for the unsatisfied event
and that’s what the conditional statement works at.
C++ provides three iteration statements — while, do, and for. Each of these iterates until its termination expression
evaluates to zero (false), or until loop termination is forced with a break statement. The following table summarizes
these statements and their actions; each is discussed in detail in the sections that follow
Iteration Statements
Statement Evaluated at Initialization Increment
while Top of loop No No
do Bottom of loop No No
for Top of loop Yes Yes
The statement part of an iteration statement cannot be a declaration. However, it can be a compound statement
containing a declaration.
The while statement
A while statement evaluates a Boolean expression. If the expression is true, a given statement is repeatedly executed
for as long as the expression continues to evaluate to true.
Syntax:
while (<condition>)
{
statement;
statement;
// Place as many statements
// here as necessary
}
The do-while Statement
A do statement executes a given statement and then evaluates a Boolean expression. If the expression evaluates to
true, the statement is executed repeatedly as long as the expression continues to evaluate to true:
If the expression evaluates to false, the statement following the do statement is the next statement to be executed.
Because the expression is evaluated after the contained statement is executed, the statement is always executed at
least once.
Syntax:
do {
statement(s);
// Place as many statements
// here as necessary
}
while(<condition>);
The for Statement
A for statement is a more structured form of a while statement. A for statement performs an initialization step and then
evaluates a Boolean expression. If the expression evaluates to true, a given statement is executed and an increment
expression is evaluated repeatedly as long as the expression continues to evaluate to true:
Syntax:
for (initialization; condition; increment/decrement)

4. Resources:

DevC++

5. Procedure and Output


EXERCISE No. 1
Write an algorithm, flowchart and a program that will display a vertical diamond pattern using
nested loops. Note that the maximum length is 9 same characters. Create the program using (a.)
while statement; (b). do-while statement (c). for statement

REQUIREMENTS:
1. Write the corresponding algorithm
a. Narrative
b. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem
3. Construct the program and record your screen display result
a. Using while-statement
ALGORITHM

NARRATIVE PSEUDOCODE
Step 1: Start the programming. Step 1: Start
Step 2: Make a variable that will hold the user's Step 2: Enter the number of rows you'd like to work with (half of the
input. diamond). Place in the r directory.
Step 3: Make 3 variables to determine the loop's Step 3: Use the following codes to print the top half of the diamond:
conditions. While (i <= r) {
Step 4: Inquire about the number of rows from the j = 1;
user (half of the diamond). while (j <= r - i) {
Step 5: Print half of the diamond in the top part cout << " ";
using user input. j++;}
Step 6: Use the user input to print the diamond's j = 1;
bottom half. while (j <= 2*i - 1) {
Step 7: End the program cout<< "*";
j++;}
cout << endl;
i++;}
Step 4: Use the following codes to print the bottom half of the
diamond:
while (i >= 1) {
j = 1;
while (j <= r - i) {
cout << " ";
j++;}
j = 1;
while (j <= 2*i - 1) {
cout << "*";
j++;}
cout << endl;
i--;}
Step 5: End
FLOWCHART
Start
yes
cout << “ ” cout << “*”

Enter the number of yes


rows. Place in the r j ++
directory j <= r - i j ++
yes j <= 2*i-1

j=1 no
j=1 no

i <= r
i ++ cout << endl;

no

cout << “ ” cout << “*”


yes
yes
j ++
j <= r - i j ++
yes j <= 2*i-1

j=1 no
j=1 no

i >= 1 i -- cout << endl;

no

End
Record your screen display:

QUESTIONS

1. What conditions needs to be evaluated to satisfy the program requirement?


➢ For the top half of the diamond, the condition that must be met is that variable ("i") must be less than or equal to the
number of rows ("r"), and for the bottom section of the diamond, the condition that must be met is that variable ("i")
must be greater than or equal to 1.
2. What did you observe using while statement compare to other iteration structure?
➢ When I used this while loop statement, I saw that the loop kept repeating due to the conditions.
b. Using do-while statement

ALGORITHM PSEUDOCODE
Step 1: Start
NARRATIVE Step 2: Set the number of rows into 9.
Step 1: Start the programming
Step 3: Use the following codes to print the top half of the diamond:
Step 2: Make a variable that will hold the
do {
user's input.
cout<<" "; j++;}
Step 3: Make 2 variables to determine the
while(j<=space);
loop's conditions.
space--;
Step 4: Print half of the diamond in the top
j=-1;
part.
do {
Step 5: Print the diamond's bottom half.
cout<<"*"; j++;}
Step 6: End the program.
while(j<=(2*i-1));
cout<<endl;
i++;
Step 4: Use the following codes to print the bottom half of the diamond:
do {
j=5;
do {
cout<<" ";
j++;
} while(j<=space);
space++; j=1;
do {
cout<<"*";
j++;
} while(j<=(2*(num-i)-1));
cout<<endl;
i++;
} while(i<=(num-1));}
Step 5: End
FLOWCHART

Start

Set the number of


rows into 9.

Half of the diamond in top part


End
do {
cout<<" "; j++;} no
while(j<=space);
space--;
j=-1; Condition:
} while (j<=space;
do { } while(j<=(2*(num-i)-1));
} while (i<=(num-1));
cout<<"*"; j++;} yes

while(j<=(2*i-1));
cout<<endl;
i++;
yes Bottom half of the diamond.
do {
cout<<" "; j++;}
while(j<=space);
space--;
Condition: no
while(j<=space); j=-1;
while(j<=(2*i-1));
} while(i<=num); do {
cout<<"*"; j++;}
while(j<=(2*i-1));
cout<<endl;
i++;
Record your screen display:

QUESTIONS

1. What conditions needs to be evaluated to satisfy the program requirement?


➢ The number of rows the user wants to put and the rows will print first the triangle based on the number of rows and then
the nested loop for the inverted triangle to make a diamond shape. The while statement in the do-while loop contain
the condition that must be met.
2. What did you observe using do-while statement compare to other iteration structure?
➢ I discovered that using a do-while loop statement ensures that statements inside the loop block are always executed
once, even if the loop condition fails.
c. Using for statement
ALGORITHM

NARRATIVE PSEUDOCODE
Step 1: Start the programming Step 1: Start
Step 2: Make three variables that will be used in the Step 2: Enter the number of rows you'd like to work with (half
program. of the diamond). Place in the r directory.
Step 3: Inquire about the number of rows from the user Step 3: for (i = 0; I <= r; i++) {
(half of the diamond). for (j=1; j <= r - i; j++) {
Step 4: Check to see if the variables' values satisfy the cout<<" ";}
loop condition. for (j = 1; j<= 2*i - 1; j++) {
Step 5: Display “ “ and “*”. cout<<"*";}
Step 6: Repeat the loop until the variables' values no cout<<endl;}
longer meet the loop condition. for (I = r-1; i >= 1; i--) {
Step 7: End the program for (j = 1; j <= r - i; j++) {
cout<<" ";}
for (j = 1; j <= 2*i - 1; j++) {
cout<<"*";}
cout<<endl;}
Step 4: End
FLOWCHART
Start

i ++

Enter the number of


rows. Place in the r
directory

yes
i=0 i <= r j=1

cout << “*”

no

j <= r - i j=1
no
j ++
yes no
j <= 2*i-1
cout << “ ”

j ++
yes

yes no cout << “*”


i >= 1

j=1
End

i --

j <= r - i
no

cout << “ ”
cout << “ ”

yes

j ++ no

cout << “*” j <= 2*i-1


yes

j ++
Record your screen display:

QUESTIONS

1. What conditions needs to be evaluated to satisfy the program requirement?


➢ The number of rows the user wants to put and the rows will print first the triangle based on the number of rows and then
the nested loop for the inverted triangle to make a diamond shape. The first loop's condition is that variable ("i") values
must equal the value of variable r, while the second loop's condition is that variable ("i") must be greater than or equal
to 1. The first inner loop's condition is that j must be less than or equal to "r – i" while the second inside loop's condition
is that j must be less than or equal to "2*i – 1."
2. What did you observe using for statement compare to other iteration structure?
➢ When I used a 'for' loop statement, I discovered that the 'for' loop iterates across a numerical range. Simply said,
readability makes a difference (and therefore maintainability). The 'for' and 'while' statements are interchangeable.

6. Conclusion:
➢ I've concluded that a loop is a structure that repeats a sequence of instructions until a specific condition is fulfilled.
Programmers use loops for cycling over data, adding numbers, repeating routines, and various other tasks. It may also
be used to repeat code blocks and is highly helpful in computer programming.

You might also like