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:
8