Why is while loop used if the work can be done more efficiently by a for loop in C programming language?

Asked by Last Modified  

Follow 3
Answer

Please enter your answer

Programming World

For loops (and for each loops especially) are definitely more common, but sometimes you just need a while loop to get the job done. for loop is 'counter-controlled loop' where as while loop if you were making a game and needed to make the game loop over and over, you could use a while loop that would...
read more
For loops (and for each loops especially) are definitely more common, but sometimes you just need a while loop to get the job done. for loop is 'counter-controlled loop' where as while loop if you were making a game and needed to make the game loop over and over, you could use a while loop that would run infinitely by using. while (i = 1) { //your logic } read less
Comments

for loops basically is used when developer knows number of iterations to be performed is known already : Example : printing table of 10 up to 10 i.e 10 * 10=100 we know number of iteration=10 so for loop can be used for(i=1;i<=10;i++){ printf("%d \n",i); ...
read more
for loops basically is used when developer knows number of iterations to be performed is known already : Example : printing table of 10 up to 10 i.e 10 * 10=100 we know number of iteration=10 so for loop can be used for(i=1;i<=10;i++){ printf("%d \n",i); // already knew iteration=10 so for loop } WHILE loop on other hand is used if developer s not sure or uncertain about number of iterations prior. Example C program to reverse the number : here number n can be anything inputted by user so developer is not sure about number of iteration so while loop while(n != 0) { remainder = n%10; reversedNumber = reversedNumber*10 + remainder; n /= 10; } we can solve this by using for loop also but it is recommended if number of itertion not know use WHILE else if known then FOR. read less
Comments

IT Professional

There is no difference semantically in both the constructs. For is easy to use. While can be used for the same purpose and vice versa with for loop is true. It depends on choice and of course some specific cases where you can avoid cryptic code. All loops of for can be replaced with while and vice versa,...
read more
There is no difference semantically in both the constructs. For is easy to use. While can be used for the same purpose and vice versa with for loop is true. It depends on choice and of course some specific cases where you can avoid cryptic code. All loops of for can be replaced with while and vice versa, given we'll have to define some variables such as counters etc. read less
Comments

View 1 more Answers

Related Questions

How can I learn C easily and rapidly?
Practise as much as you can. Skill in programming comes mainly from experience.
Umamahesh
0 0
5
What is meant by "bit masking"?
For example if you want to know if third bit is 1 or not for an int x then you can mask all other bits and just check the third one: (n & 0x04) != 0 means 3rd bit is not zero, 0x04 is the bit mask
Kiran
0 0
6
What exactly is \r in the C language?
In the C language, "\r" represents the carriage return character. When encountered in a string, it instructs the output device to return the cursor to the beginning of the current line. This character...
Sajini
0 0
5
How would you round off a value from 1.66 to 2.0?
By adding 1.0 (float) to the integer part (type casted) of 1.66.
Dhanya
0 0
6
Can a file other than a .h file be included with #include?
yes .h -- is just for our understanding that its header file, you are free to put any filename and extention.
Pankaj
0 0
7

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Variables
Variables in C Language:A variable is a name that may be used to store a data value. Unlike constant, variables are changeable, we can change value of a variable during execution of a program. A programmer...

Be prepared to get trained--init
Before starting the training,students must be mentally prepared for acceptance of new knowledge. Students must attend training with open minded forgetting the position they are working.This will help...
S

Smartnub Softsolutions

0 0
0

C Programming basics
C PROGRAMMING BASICS 10 Hours Introduction to ‘ C’ programming –fundamentals – structure of a ‘C’...
O

Om K.

0 0
0

C PROGRAM FOR GENERATING SOUND
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { sound(3000); // sound function have single parameter , this parameter we put integer value its generating...

V. Muthu Ganeshan

0 0
0

C Program-The Odd Loop Demo
/* WAP to print square value of the number entered by user using odd loop*/ //Header Files #include<stdio.h>#include<conio.h> //Main Function void main(){ char another='y'; int num,sq; //...

Recommended Articles

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...

Read full article >

Looking for C Language Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you