Nested For LOOPS

Nested For LOOPS

In this part you will learn: 1. For LOOP 2. Nested For LOOP 3. More on conditional statements 4. C syntax 5. Showing output In this tutorial I will teach you how to make different shapes using For Loop and * . Making different shapes with * may sound something like very unimportant but trust me making these shapes provide a very good understanding of the loop. What is a NESTED LOOP ? Sometimes we need two loops to execute a statement. For example if we want to solve a 3 x 3 matrix then we will need two loops one for rows and other one for columns. So we use a loop within a loop for every iteration of the parent loop the inner loops runs completely. Eg: for(int i=1;iMaking a rectangle with astericks using nested FOR LOOP: Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>
These are the header files that we need for coding. The main header is stdio.h which provides us with most of the functions for coding in C and the second header files is just for using a function which can pause the screen until the user hits any key.
  1. int main(){
  2. int width,height; // dimensions
  3. int w,l;
For making a rectangle we need to take input two variable i.e width and height. So we declare them first and we also declare the loop counters.
  1. printf("Enter the width of rectangle: ");
  2. scanf("%d",&width);
  4. printf("Enter the height of rectangle: ");
  5. scanf("%d",&height);
Now we are taking inputs from the user, The user will enter width and height of the rectangle. We will use these variables to pass values to the for loop.
  1. for(w=1; w<=height; w++){
  2. for(l=1; l<=width ; l++){
  3. printf("*");
  4. }
  5. printf("\n");
  6. }
Then we will run two nested for loops with the variables w and l. The first loop is for width and the second loop is for the height. For every row we will print astericks from 1 to width and then after the inner loop is complete we simply print \n to change the line ( for going into the next row to print more astericks.
  1. getch();
  2. return 0;
  3. }//end main
Then we will simply call the getch function to pause the console and end the program. Execute > compile then Execute > run Output outputTriangle using Nested for loops File > new source file
  1. #include<stdio.h>
  2. #include<conio.h>
  4. int main(){
  5. int size; //size of half triangle
  6. int w,i; //loop variables
  8. printf("Enter the size of the triangle : ");
  9. scanf("%d",&size);
  10. //upper half of the triangle
  11. for( w=1; w<=size; w++){
  12. for( i=1; i<=w;i++){
  13. printf("*");
  14. }
  15. printf("\n");
  16. }
In this program we will simply print a triangle (using astericks) double the size provided by the user. We first declare the variables size and loop variables and then get the size from the user. After that we run nested for loop to print the upper half of the triangle. The first loop is to change the row of the triangle and second loop is used to print the astericks equal to row number ( if 1st row then 1 asterick 2nd row 2 astericks and so on .. ). Then we will simply end these loops and start tow new nested for loops for the lower end.
  1. // lower half of the triangle
  2. for(w=1;w<=size;w++){
  3. for (i= size-1;i>=w;i--){
  4. printf("*");
  5. }
  6. printf("\n");
  7. }
  8. getch();
  9. return 0;
  10. }
In the second set of loops we do exactly the same thing in the first loop but in the second loop we just start it backwards so that we can make the triangle evenly . So we decrement in the second loop and change the row after it is complete. In the end we pause the screen and return and our program is complete. Execute > compile then Execute > run Output: output


Submitted byvutani (not verified)on Sun, 05/10/2015 - 23:35

I understand it better

Add new comment