While Loop in C programming
Hello Ralph Collado, understanding WHILE loop is very simple through a SYNTAX, which says:
while(condition is true) {
perform statement/s;
}
It performs the statement again and again creating loops until the condition becomes FALSE. Here is a simple snippet which prints 0 to 10, each number is printed on the screen on the new line.
#include <stdio.h>
int main() {
int a=0; // a variable is declared first
while(a<=10) { // condition is checked while a is less then 10
printf("%dn", a); // the value of a is printed with new argument until 10th value called
a++; // the value of a is equal to a plus 1
}
getchar();
}
Hope this explains and solve the conclusion in your mind. Thank you
Emon Asejo
While Loop in C programming
As a tutor in C, I do always explain in the following way.
We should teach the loops structures at a time in a class itself. first explain the for loop, than while and do while. the difference of while with dowhile and the compact form of for loop.
First explain the for loop
Void main(){
for(initialisation;condition;increment){
statements;
}
}
Then the While Loop
void main(){
initialisation;
while(condition){
statements;
increment;
}
}
Now explain with example
Void main(){
Int i=0; //this is the initialisation
While(i<10){ //loop based on a condition
printf(“Lines 10 times”); //statements to be executed again and again within a loop.
i++; //increments
}
OUTPUT:
Lines 10 times
Lines 10 times
Lines 10 times
Lines 10 times
Lines 10 times
Lines 10 times
Lines 10 times
Lines 10 times
Lines 10 times
Lines 10 times
Thus we see that both the loops are complementary to each other, Infact, For loop is more compact then any other loop format.
Thanks.
While Loop in C programming
Hey that what I am looking for. Simply the best! My outmost thanks Kanhai.
You gave me a very such useful way. More power..
Always share your knowledge.