0

This is just for my information and I don't really have much need for it... But, I would like to know anyway :D.

#1. Is a for or while statement faster than one another? As in, if someone put a for would it be faster (application-wise, not when their typing it) than a while statement?

Example:

for(i=0;i<string.length;i++);

 OR

 i=0;
 while(i<string.length){
    i++;
 }

#2. Is it faster (application-wise, not typing) to use no { and just the ; or not? As in...

while(i<string.length){
i++;
}

OR

while(i<string.length)
i++;

#3. What is more efficient, if different, using a boolean for true/false or an int for 1/0?

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by fmwyso
0

1) Depends on the compiler and whether you compile the program for debug or release. Most modern compiler will make them the same code, despite the errors in the code snippets you posted.

2) Speed has nothing to do with it -- the { and } are there to block lines of code. I like to use { and } even with one liners for readability and ease of future enhancements.

3) One is not more efficient than the other -- is readability and maintainability that counts. Use boolean true/false whereever it makes sense to do that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.