×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

Some more advanced concepts and tricks with loops !


 

loops

Welcome back to Newbie Programmer Series. In the last part (click here), we have learnt the fundamental difference between the different kinds of loops and also used them practically. In this part, we are gonna learn some more tricks that can be used within loops. This is going to be the last part on loops. If you are new to this series, check out the index here and read all the previous parts so that you can catch everything below easily, and please do revise the previous parts other wise you will find this one difficult.

In this post we are gonna discuss

So Please continue reading...


No Blocks for "for"

Yes it means that if you are using single statements, you don't need to use the block structure every time. Therefore our 10 times hello world is now like:

#include <stdio.h>
main()
{
   int x;
   for ( x = 0; x < 10; x++ )
   printf("Hello world \n ");
}

And even our square number program is now like:

#include<stdio.h>
main()
{
   int x;
   printf("Squares \n");
   for (x =1; x < 11; x++ )
   printf("Square of %d is %d \n", x, x*x );
   printf("table ends");
}

Only the line next to the loop will be considered in the looping process until " ; " comes up.

The Infinite Loops !

Do you think what will happen if the conditions of the loop are never satisfied ? Will the program runs continuously ? Will it ever end ? Answer is, It will go on forever !!!

Lets do an example:

#include <stdio.h>
main()
{
   int x = 0; 
   while ( x < 1 )
   {
      printf("Hello, World !\n");
   }
   printf("Bye\n");
}

EXPLANATION:
This is quite a useful program to understanding the principle. When the program starts, x has the value 0. The while loop is having the condition of x being having a value less than 1. And x is zero so the condition is met. In the loop, the Hello World msg will be printed. Now the condition will be checked again. Still x is zero, so the loop will repeat again. When the loop ends, the "Bye" msg would be printed. Now check the code, do you think the value of x will ever change ? No and so the condition of x being less than 1 will always satisfied and the loop will go on forever.

I don't need to show the output. I know it's already on your eyes. Its "Hello, World !" infinite times !!!

You can do it with for very simply :

#include <stdio.h>
main()
{
   for ( ; ; ) printf("Hello, World !\n");
}

Note here that I didn't make the block structure for the for loop here. Go it ? or Read again above.

This is my favorite program below.

#include <stdio.h>
main()
{
    for ( ; ; ) 
}

Because it at least wait forever. While the below one just quit !

#include <stdio.h>
main()
{
}

Loops within loops

Yes we can use many loops one inside another. This is also known as Nested loops. The best and main advantage of this feature is when we deal with tables. Any type of tabular thing. But how ? It's very simple and all because of the block structure. For example, doing nested loop with for is like :

for (---)
{
     ---
     for (---)
          {
          ---
          }
     ---
}

You don't need to stuck with this only, you can so anything because we are using the block structure.

while (---)
{
     ---
     for (---) {
     ---
     }
}

Whatever, but simply take care of the brackets to maintain block structure.

Lets do some serious programs.

Multiplication Tables

#include <stdio.h>
main()
{
   int i,k;
   printf("* MULTIPLICATION TABLES *\n");
   printf("\n");
   printf("This program is going to print all the multiplication tables from 1 to 10 \n");
   printf("After each, press enter to continue \n");
   printf("\n");
   for(i = 1; i < 11; i++)
   {
      printf("Table of %d \n", i);
      for(k = 1; k < 11; k++)
      {
        printf("%d x %d = %d \n", i, k, i*k);
      }
      printf("Press enter to continue... \n");
      getchar();
   }
   printf("Tables ended !, press enter to exit \n");
   getchar();
   return 0;
}

This is one of my favorite program. Lets discuss its working. First of all, the formalities are printed, we come directly to the first loop. i will be going from 1 to 10. And will repeat everything inside it 10 times. Inside it, another loop is doing the same with k, 10 times. But it has a dirty statement. Its: printf("%d x %d = %d \n", i, k, i*k); But this is the real brain. Suppose i is in its second round. I mean i = 2 due to the for loop. Now j will also repeat that statement 10 times with its value from 1 to 10. Every time that statement is repeated, it will be executed with the values of i and k. So if i is 2 for a case, and then k will be going from 1 to 10, the following table will be printed

%d x %d = %d with i, k, and i*k so...

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6

...and so on 10 times.

After these 10 times of k, the loop will be completed. Now it will again come back to the loop of i. Now it will take value of 3. And as above table of 2, the table of 3 will be printed. It's a beautiful program. Please run it yourself.

Pyramids

#include <stdio.h>
int main()
{
    int i,j,rows;
    printf("Enter the number of rows: ");
    scanf("%d",&rows);
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           printf("* ");
        }
        printf("\n");
    }
    return 0;
}

This is also a great one. It will ask you number of rows. Like if I entered 5. Now the for loop of i will work. The current i is 1. So it will print first row. With j stars in it. For the first time, j is 1 so overall printing is a row of single star. The loop will end and a new row will be created due to printf("\n"); Then the loop will be repeated. Now i and j are gonna be 2. So a row will be printed, the 2nd row and this time, a row with 2 stars. The same process will continue till the value of rows is reached which is 5 in my example. So the output is gonna like:

*
* *
* * *
* * * * 
* * * * *

But didn't you find something different ? What is <= ? and ++j ? Do you want to know more about such awesome unknown operators ? That's gonna be my next part. This part ends here and the loops too. Stay connected for the awesome next part with a new topic !

 

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo