09 November 2013

The For Loop in C

The loop is one of the most critical parts of programming.
According to Wikipedia, a loop is a sequence of statements which is specified once but which may be carried out several times in succession. Read more about Loops here.

So, basically, a loop is a statement used to repeat one or more actions for a specified number of times.

There are two types of loop :
  1. Finite Loop
  2. Infinite Loop
When looping, in most cases, you would want to avoid infinite loops because it might crash your whole system.

There are actually several kinds of Loops in C which are the For loop, the While loop, and the Do...While Loop. But today, we are going to discuss about the For loop.


Let's go straight to the syntax of declaring a for loop:
for(initial_condition;loop_condition;increment){
//statement1
//statement2
//....
}
The initial condition is set by assigning a value to a variable, called Control Variable.
The loop condition is the condition to be checked at the start of every loop cycle. If the condition is true, continue the looping process, else, break(or stop) the process.
The increment is the value added to the Control Variable at the end of every loop cycle.
The statements within the parentheses are the statements to be executed in every cycle of the loop.


Example:

Output numbers from 1 - 10 using no more than 1 printf function.
 #include <stdio.h>
#include <stdlib.h>

int main(){
 //declaration of control variable
 int i = 0;

 //declaration of for loop
 for(i = 1; i <= 10; i++){
     printf("%d\n", i);  
    }
   
 system("Pause");  
 return 0;  
}
Magically, it works. Here is the result :


Let me explain the loop declaration part.

There is i = 1, this means, the count starts from 1.
Then, there is i <= 10, this indicates that the loop will only end after i is larger than 10. If i is still in range (1-10), the loop will continue to display i, which is incremented by 1 every cycle by using the i++ statement.

Note : You can also use +1 increment this way -> i += 1


Okay, let's mess a little with the increment part of loop now.
Let us create two loops to display odd and even numbers from 1-10, respectively.

#include <stdio.h>
#include <stdlib.h>

int main(){
 //declaration of control variable
 int i = 0;

 //declaration of for loop for odd numbers

 printf("Odd numbers:\n");
 for(i = 1; i <= 10; i += 2){
     printf("%d\n", i);  
    }

 //declaration of for loop for even numbers  
   
 printf("\nEven numbers:\n");  
 for(i = 2; i <= 10; i += 2){
     printf("%d\n", i);  
    }  
   
   
 system("Pause");  
 return 0;  
}

As you can see from the code, there are two for loops having the same increment but producing different results. If you look closely, you will find that the only difference is in the initial value of both loops. To obtain odd numbers, you start from 1 and add by 2 each loop. However, in order to display even numbers, you need to start from 2(the first even number in range) and increment it by 2 through the loop. You can see the result below:



To close this post, I would like to present you with an example of creating an infinite loop.
Despite the fact that we are doing it in purpose right now, it might happen a lot as accidents in programming later on. Here it is:

#include <stdio.h>
#include <stdlib.h>

int main(){
 //declaration of control variable
 int i = 0;

 //declaration of for loop for odd numbers

 for(i = 1; i <= 10; i--){
     printf("*");  
    }
    
   
 system("Pause");  
 return 0;  
}

 It prints the * symbol endlessly. Why? As you can see, the loop condition is i <= 10 while the loop increment is i--. This ensures that the i never reaches 10 or above(not even 2 actually).

Note: To stop the program, press Ctrl + C


Loops can be used for many occasions, you have to experiment by yourself in order to be fluent in using loops. Any questions are welcomed. Post them here as comments or send it to my email at cyberfrostprogrammer@yahoo.com.

Thank you for reading.




Happy Coding,
Cyber Frost


No comments:

Post a Comment

Search