Loop in C Programming

0

Loop is needed whenever we want to do same task over and over. For example we want print “Hello pusheax” for 100 of times? Should we write the same line for 100 times something like below?

 

Obviously not! At least i am not going to do that. For this we can use while,for,while-do loop. Below i will give practice example for each loop.

 

While Loop

While loop seems to be very easy to me. The structure is exactly like this:

 

 

Here is the practical example:

  1. In this program in line 4 i first declared a empty integer variable.
  2. The i test it if the i is less than 100.
  3. if i is less than 100 then it will execute the next line.
  4. i++ is the post increment. Meaning i will be 1,2,3,4,….100. It will print “hello pusheax” on each count.

 

For Loop

The same thing i will do with for loop. Before writing the example i want to show you the basic syntax.

The var is the variable for the loop and only once executed. If condition is true. If the condition is true then it will start executing the branch code.

 

 

Next i wanted to print a alert when the loop reach to 50. For this within for loop i simply tested the variable i like this:

 

 

 

This is the most used loop in C programming. Do you think i need to explain this more in theoretically?