Professional Documents
Culture Documents
LAB REPORT # 5
NAME:
SECTION:
REG#
TITLE:
TEACHER:
DATE:
1
LAB TASK#5:
FOR LOOP.
WHILE LOOP.
DO…WHILE LOOP.
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:
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: