In any programing language, for executing a same statement or task several times a special control-flow statement is available. Yes! I am talking about the loops….
In C language we have three types of loops, namely- for loop, while loop and do-while loop! With the use of these loops it becomes real handy and easy to perform any task or print a statement any number of times. Let’s take a quick look at the syntax of these loops……
for(int i=0;i<n;i++) int i=0;{ while(i<n) { //DO this //Do this i++; } }
So here is the syntax for the for loop and the while loop. But there is a simple note to make. The declaration of the variable ‘i’ in the for loop is supported in the new compilers but is not accepted by the old compilers, in those compilers you need to declare the variables in the beginning, i.e., with the other variables. So don’t worry if the compiler yells at you for the error!!
In the above example both for loop and while loop perform the same task, i.e., they a task ‘n’ times, this is the speciality of these loops, they are inter-convertible,all while loops can be converted to for loops and vice-versa!!(In a bit I will prove myself wrong!!).
do{ //Do this }while(condition);
Above is the syntax for the do-while loop. This loop perform a specific task (given in the do statement) until the condition in while statement becomes false.
If punishment is given to programmers…..
So let’s talk about the mechanism of executing of the loops.
#gallery-0-5 { margin: auto; } #gallery-0-5 .gallery-item { float: left; margin-top: 10px; text-align: center; width: 33%; } #gallery-0-5 img { border: 2px solid #cfcfcf; } #gallery-0-5 .gallery-caption { margin-left: 0; } /* see gallery_shortcode() in wp-includes/media.php */
If you notice carefully, there is not much difference between do-while and while loop! In the while loop, first the condition is checked and then the statements enclosed within the loop gets executed whereas in the do-while loop first the loop is executed once and then the further execution of the loop depends on the correctness of the condition in the while statement.
Okay so here is some food for thought!! Can you convert the following while loop to for loop…..
int i = 0; int sum = 0;.... while (sum < 10000) { sum += i; i++; }
Come on don’t just keep reading…. Think Think…….
Yep gotcha!!!! Couldn’t convert it right???
Well I kept my promise. I proved myself wrong!
Yes, we cannot convert all while loops into for loops!! But the converse is not true! We can convert all for loops to while loops but not all while loops can be converted to for loops!!
You might also wanna check out these interesting facts on switch statement!! Below is the link!!
Link: http://www.geeksforgeeks.org/interesting-facts-about-switch-statement-in-c/
That’s all for the loops! Hope you enjoyed reading it!!
Don’t forget to leave a comment or give a thumbs up!! See you guys soon with a new topic and do not forget about the Program Time!!
Loops In C….. In any programing language, for executing a same statement or task several times a special control-flow statement is available.