Can anyone please discuss some common pitfalls of writing loops in Java. Thanks so much.
Printable View
Can anyone please discuss some common pitfalls of writing loops in Java. Thanks so much.
Common pitfalls of writing loops are :
Not including a statement that will eventually makes the boolean expression false.
Not avoiding to use real numbers for testing and increments
The inclusion of an avoidable test in a loop.
I know about these common pitfalls of writing loops, but can someone please discuss some more.. I really need the help, thank you.
The common pitfall that i know about loop.
Some of the pitfall are occurs because of this reason;
If the program have while loop, then the common pitfall will going to occurs if we make use of semi-column \followed by while loop parentheses.
for e.g.
And the second mistake that, sometime we forget to use increment or decrement of variables within while loop, and because of that mistake we get Run Time Error.Code:while(a>=10); //this mistake will make while loop as an infinity loop.
{
//Do this statement;
}
for e.g.
This are some common mistake which are mostly done with the beginners for programming language.Code:int i=2;
while(i<=5)
{
// Do this statement;
}
//Since the above code don't have increment or decrement for the variable i,
this will also make the above loop as an infinity loop.