Chapter 7: For the Love of God

Chapter 7: For the Love of God



This time we are talking about another loop in C, the final loop, and arguably the most useful.The for loop. A for loop works a lot like the while loop, in the sense that it repeat a given block of code until some condition is no longer true. But we can define its condition, declare a variable and set its incrimination all inside the parameters of the function itself.

// Print numbers from 1 to 100
#include <stdio.h>

int main() {
  int i;

  for (i = 1; i == 100; ++i)
  {
    printf("%d /n", i);
  }
  return 0;
}

Here, we keep printing a number and adding one to it, beginning from i equal to 1, until eventually i is equal to 100, and the loop exits and the program finishes

And we can nest a for loop inside another for loop. This loop will print triangles after being given a series of rows to work with.

#include <stdio.h>
void main()
{
   int i    ;
   int j    ;
   int rows ;


   //Prompt User Input
   printf("Input number of rows : ") ;
   scanf("%d",&rows)                 ;
   for(i=1;i<=rows;i++)
 
   {
	for(j=1;j<=i;j++)
	   printf("'");
	printf("\n");
   }
}

So let us now move on to some exercises, remember you should have an attempt and creating a solution of your own, before copying out the text I have written and compiling it for yourself . It is always a good idea to play around with the digits, and parameters of the loops, try turn a + to a -, see what happens, change a 10 to a 100, and so on. Just experiment and have fun where you can.

Exercise One

Write out a program using for loops that prints a diamond.

#include <stdio.h>

void main()
{
   int i,j,r;
   printf("Input number of rows (half of the diamond) :");
   scanf("%d",&r);
   for(i=0;i<=r;i++)
   {
     for(j=1;j<=r-i;j++)
     printf(" ");
     for(j=1;j<=2*i-1;j++)
       printf("*");
     printf("\n");
   }
 
   for(i=r-1;i>=1;i--)
   {
     for(j=1;j<=r-i;j++)
     printf(" ");
     for(j=1;j<=2*i-1;j++)
       printf("*");
     printf("\n");
   }
 
}

Exercise Two

Write a program to display the times table of a given digit.

#include <stdio.h>
void main()
{
   int j,n;
   printf("Input the number (Table to be calculated) : ");
   scanf("%d",&n);
   printf("\n");
   for(j=1;j<=10;j++)
   {
     printf("%d X %d = %d \n",n,j,n*j);
   }
} 

Exercise Three

This time, make a triangle of numbers.

#include <stdio.h>

void main()
{
   int i,j,spc,rows,k;
   printf("Input number of rows : ");
   scanf("%d",&rows);
   spc=rows+4-1;
   for(i=1;i<=rows;i++)
   {
         for(k=spc;k>=1;k--)
            {
              printf(" ");
            }
                      
	   for(j=1;j<=i;j++)
	   printf("%d ",i);
	printf("\n");
    spc--;
   }
}

So there is one last subject to cover in our for loop. Break causes a loop to break early, continue forces a loop to continue early. ) Circumstantial to your needs, while, or do-while loop, you might not want to execute the entire body of the loop every iteration.

This program works out the sum the numbers with a maximum of 100, if the user enters a negative, the loop breaks and program finishes early.


#include <stdio.h>

int main() {
   int i;
   double number, sum = 0.0;

   for (i = 1; i <= 100; ++i) {
      printf("Enter n%d: ", i);
      scanf("%lf", &number);

      // if the user enters a negative number, break the loop
      if (number < 0.0) {
         break;
      }

      sum += number; // sum = sum + number;
   }

   printf("Sum = %.2lf", sum);

   return 0;
}

This program is very similar, however this time instead of terminating the program, it skips past the negative input.

#include <stdio.h>
int main() {
   int i;
   double number, sum = 0.0;

   for (i = 1; i <= 10; ++i) {
      printf("Enter a n%d: ", i);
      scanf("%lf", &number);

      if (number < 0.0) {
         continue;
      }

      sum += number; // sum = sum + number;
   }

   printf("Sum = %.2lf", sum);

   return 0;
}

Congratulations, you will surely be looping mad by this point, and if you have been writing out all the examples, you have no doubt began to become comfortable with the syntax of C. At this point we have actually covered the main functions of programming, and you should be able to challenge yourself to write your own silly programs. Have fun and experiment, see what you can challenge yourself to make.

This course is provided for free, as we at Devoke believe all education should be. However, if you enjoy the course, we are always grateful for support and donations to Patreon to ensure we can continue providing resources for free.

https://www.patreon.com/DevokeStudio/posts





Please Login to read or post comments