wat is difference among loops?
can anybody answer this question?
wat is difference among loops?
can anybody answer this question?
What do you mean? What sort of loops are we talking about? The four basic loop constructs in C, or something else?
If the former:
for loop: used when you want to get from some value to another value iteratively.
while loop: do something zero or more times.
do-while loop: do something one or more times.
goto: don't use unless you are REALLY sure you have to. [Which is very rare].
--
Mats
Last edited by matsp; 05-05-2009 at 09:37 AM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
To show possible practical uses:
for loop: used when working with every element in a series
while loop: used when working until a certain condition is met (until the users exits, or until a certain procedure is successful)
do-while: pretty much the same as above, it just happens AT LEAST once, no matter what.
goto: used when hacking the kernel, and that's about it, pretty much.
1.for loop
In for loop first i is initialized to 1 then it is checked against the condition(<=5) and then executes the body, after which it increments i and again checks for the condition and the process is repeated until i equals 6.Code:for(i=1;i<=5;i++) printf("FOR");
2.while loop
You can see the difference between the structures of for and while loop. They both generally do the same thing.Code:i=1; while(i<=5) { //do something i++; }
3.do while loop
This loop is used if anything is to be done atleast once.
This loop guarantees that the body will be executed at least once while the other two loops (for and while) may not execute the body even once.Code:do { //do something }while(i<=5);
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition