Gulis Laboratory 5
Gulis Laboratory 5
: 5
Name: John Kenneth S. Gulis Section: EE21S1
Date Performed: November 12, 2021
Date Submitted: November 13, 2021
Instructor: Engr. Julie Ann Susa
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
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++
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 << “*”
j=1 no
j=1 no
i <= r
i ++ cout << endl;
no
j=1 no
j=1 no
no
End
Record your screen display:
QUESTIONS
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
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
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 ++
yes
i=0 i <= r j=1
no
j <= r - i j=1
no
j ++
yes no
j <= 2*i-1
cout << “ ”
j ++
yes
j=1
End
i --
j <= r - i
no
cout << “ ”
cout << “ ”
yes
j ++ no
j ++
Record your screen display:
QUESTIONS
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.