Results 1 to 5 of 5

Thread: test your thinking power

  1. #1
    Expert Member
    Join Date
    May 2008
    Answers
    100

    Thumbs up test your thinking power


    which of THE LOOPS IS BETTER?
    1> for() loop
    2> while() loop
    3> do -while loop

    and if so than whyyyyy???????????????


  2. #2
    Junior Member
    Join Date
    Aug 2007
    Answers
    9

    Re: test your thinking power

    It depends on the logic which you are using. It is not correct that one is better than other.


  3. #3
    Junior Member
    Join Date
    Apr 2008
    Answers
    1

    Re: test your thinking power

    hi,this is swetha,i do think that a while loop is better a for loop because the while loop repeats the condition if the condition satisfies or else it terminates but in for loop is used to test the condition for many times until the condition fails


  4. #4
    Junior Member
    Join Date
    May 2008
    Answers
    2

    Re: test your thinking power

    for() loop is best
    because
    u do initialization,loop condition,loop modification in a single step
    u can have less no. of lines of code
    under d cncpt of looping
    for loop has strict condition checking


  5. #5
    Junior Member
    Join Date
    Jun 2007
    Answers
    17

    Re: test your thinking power

    Quote Originally Posted by aman15490 View Post
    which of THE LOOPS IS BETTER?
    1> for() loop
    2> while() loop
    3> do -while loop

    and if so than whyyyyy???????????????
    Depends on the program logic. Each has its downsides and upsides though.
    For Loop
    Advantages include
    1. The entire loop can be in one single executable line of code
    2. Has the loop conter initialization and post condition action, as well as exiting the loop condition right at the top
    3. Enhances readability
    One of the major disadvantages is that the counter is defined regardless of the condition is true or false at the start.

    while Loop
    Quite simple to identify what the condition to come out of the loop is. However, there is no easy way to track if there is any code that ensures that the condition is met at some point. For example, in while (x<10), in the loop body, you need to remember to make sure that x is at some point, 10 or greater. If not, this will be an infinite loop.

    do... while
    Most useful when you want the body of the loop to execute at least once, regardless of the condition. However, again, if that is not the intent, this is pretty useless. Also, this is not a truly conditional loop, because at least one iteration of the loop executes unconditionally. Again, the disadvantage of remembering to ensure the loop's exit condition is met, is still there.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact