While Loop in C programming

Asked By 20 points N/A Posted on -
qa-featured

What is the best code to explain and perform the while loop on C programming (with output)?

SHARE
Best Answer by Kanhai Else
Answered By 0 points N/A #102494

While Loop in C programming

qa-featured

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

Best Answer
Best Answer
Answered By 0 points N/A #102493

While Loop in C programming

qa-featured

 

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.

Answered By 20 points N/A #102495

While Loop in C programming

qa-featured

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.

Related Questions