Dear Friend ,
Why for loop is effective than while loop ?
sanjay
5 1751
Dear Friend ,
Why for loop is effective than while loop ?
sanjay
Hi. As an abstract statement this is not true. Each has a purpose that is quite different from the other. The for loop is used when there is a specific number of iterations to loop through. The while loop is used when there is not a specific number of iterations.
Dear Friend ,
Why for loop is effective than while loop ?
sanjay
the for statement is a shorthand way of writing a while(), e.g. -
for (expression1; expression2; expression3)
-
statement
-
next statement
-
can be considered equivalent to: -
expression1;
-
while (expression2)
-
{
-
statement
-
expression3;
-
}
-
next statement
-
The complexity of the algorithm will determine whether a for or a while is most suitable, e.g once the for (expression1; expression2; expression3) structure is longer than a single line a while should be used.
Banfa 9,065
Expert Mod 8TB
The complexity of the algorithm will determine whether a for or a while is most suitable, e.g once the for (expression1; expression2; expression3) structure is longer than a single line a while should be used.
I've never heard of that as a rule before (which of course doesn't make it invalid).
I think it is personal preference whether you use a for or while loop. Generally I tend to use a for loop if expression1, expression2 and expression3 are all expressions using the same variable (or expression1 is empty and the other 2 are expressions using the same variable), otherwise I tend use a while loop.
I've never heard of that as a rule before (which of course doesn't make it invalid).
I think it is personal preference whether you use a for or while loop. Generally I tend to use a for loop if expression1, expression2 and expression3 are all expressions using the same variable (or expression1 is empty and the other 2 are expressions using the same variable), otherwise I tend use a while loop.
In fact I always use a for loop when possible because I find it easier to read given that all of the parameters appear in the first line. This makes life much easier when debugging. I use a while loop when the end of the loop cannot be determined before runtime as in looking for the end of file in a recordset or file on disk.
I usually use a for... loop when working strictly with numbers and some incremented value. When your end condition involves something other than ++ or --, I make use of a while loop. For instance, if I know I have to perform something exactly 10 times, I'd use a for loop. Or, if I was initializing each member of an array, I'd use a for loop, since I know exactly how many elements there are, and each end condition will be i++ (with a header of for (int i = 0; i < SIZE; i++) ).
But, if I don't know how many times something will happen, I'll use a while loop. For instance, when playing chess, you should repeat the process of getting a move from each player, but the amount of times that the loop occurs is indefinite - the game may be over in 5 moves or 25. Or, when reading from a file, I'd use a while loop because I'm not sure how many lines of text there are, or I may want to add text later.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: pertheli |
last post by:
Hello,
I have a large array of pointer to some object. I have to run test
such that every possible pair in the array is tested.
eg. if A,B,C,D are items of the array,
possible pairs are AB, AC,...
|
by: ben |
last post by:
i have a bit of code, that works absolutely fine as is, but seems over
complicated/long winded. is there anyway to shorten/simplify it?
the code is below. description of it: it's like strcpy in...
|
by: Shamrokk |
last post by:
My application has a loop that needs to run every 2 seconds or so. To
acomplish this I used...
"Thread.Sleep(2000);"
When I run the program it runs fine. Once I press the button that starts the...
|
by: John Pass |
last post by:
What is the difference between a While and Do While/Loop repetition structure.
If they is no difference (as it seems) why do both exist?
|
by: Allerdyce.John |
last post by:
Hi,
I am trying to compare the amount of work between using STL algorithm
VS a plain Java loop.
Let's say the class Rect has 2 attributes: area, and areaPerCent.
In Java, I just write a...
|
by: Egbert Nierop \(MVP for IIS\) |
last post by:
Hi,
Out of curiousity, I sometimes look at the produced assembly after
compilation in release mode.
What you often see, is that CPP, always fully addresses registers to copy
values from a to...
|
by: vamshi |
last post by:
Hi all,
This is a question about the efficiency of the code.
a :-
int i;
for( i = 0; i < 20; i++ )
printf("%d",i);
b:-
int i = 10;
|
by: Jan Schmidt |
last post by:
Hi,
in a nested do-while-loop structure I would like to "continue" the outer
loop. With goto this should be no problem in while-loops. However, for
do-while I cannot get it to work (without a...
|
by: bonneylake |
last post by:
Hey Everyone,
What i am trying to do is show previously entered information. I know i need a loop for this , but i am not sure how to loop through this based on my form appears all in the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |