0% found this document useful (0 votes)
72 views8 pages

Computer Programming: Lab Report # 5

This lab report discusses for loops in C++ programming. It explains that a for loop is used to execute a block of code a specific number of times until a condition is met. The general structure of a for loop is presented with an initialization, condition, and increment statement. Nested for loops are used to build different triangle shapes by varying the number of rows and stars printed in each row. Screenshots show the code and output of programs that generate right-side up, upside down, and side triangles using nested for loops.

Uploaded by

muhammad hussam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views8 pages

Computer Programming: Lab Report # 5

This lab report discusses for loops in C++ programming. It explains that a for loop is used to execute a block of code a specific number of times until a condition is met. The general structure of a for loop is presented with an initialization, condition, and increment statement. Nested for loops are used to build different triangle shapes by varying the number of rows and stars printed in each row. Screenshots show the code and output of programs that generate right-side up, upside down, and side triangles using nested for loops.

Uploaded by

muhammad hussam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

COMPUTER PROGRAMMING

LAB REPORT # 5

NAME:

SECTION:

REG#

TITLE:

TEACHER:

DATE:

1
LAB TASK#5:

INTRODUCTION TO FOR LOOP:


In computer programming, loop repeats a certain block of code until some
end condition is met.

There are 3 types of loops in C++ Programming:

FOR LOOP.
WHILE LOOP.
DO…WHILE LOOP.

A FOR LOOP is used for executing a block of statements until a particular


condition is satisfied. FOR EXAMPLE when you are displaying a number
from 1 to 100 you may set the value of the variable to 1 and display it 100
times, increasing its value by one (1) on each loop iteration.

Note that the LOOP will only print the result if the given condition is true
otherwise the compiler will not show the required result because the given
condition was false. The following FLOWCHART shows that how a FOR
LOOP works.

2
GENERAL FORM OF A FOR STATEMENT:

 For ( intialization; loopcontinuation condition; increment statement )


 Where, initialization expression initializes the loop.
 Loop continuation condition determines whether the statement is
true or false and should it continue executing.
 And increment, increments the control variable.

STRUCTURE OF FOR STATEMENT:

In this lab we built a triangle using these NESTED FOR LOOPS and it is
shown in the following screenshots.

Using the similar NESTED FOR LOOPS we can build other shapes of
triangles and some of them are shown below.

3
PROGRAM BUILD-UP:
According to the above discussion we have built-up the using FOR LOOP which
is shown in the SCREENSHOT as under:

In our program TWO (2) FOR LOOPS can be seen, the first one is for the number
of ROWS and the second one is for the number of STARS.

The user enters the desired height of the TRIANGLE and we have given the
condition in the second for loop that the number of stars in each row should be
less than the HEIGHT.

4
RESULT ON CONSOLE WINDOW:
Using the FOR LOOPS we built a PROGRAM that prints a stars triangle on the
CONSOLE WINDOW and it is shown in the following picture:

RESULT#1

RESULT#2

5
BUILD-UP & RESULT OF A FULL TRIANGLE:
The screenshot below shows the build-up of a full triangle using nested for
loops:

RESULT:

6
BUILD-UP & RESULT OF AN UPSIDE DOWN
TRIANGLE:
Following screenshot contains the program of how to build an upside
down triangle

RESULT:

7
BUILD-UP & RESULT OF A SIDE TRIANGLE:

You might also like