Difference between for n while

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rohitjogya@gmail.com

    #1

    Difference between for n while

    Can anyone tell me the difference bet for loop and while loop
    execution?
    _______________ _____
    for (i=0 ; i<10 ; i++)
    ; /* Do nothing*/

    print i;

    _______________ ____
    i=0;
    while (i<10) {
    i++;
    }
    print i;
    _______________ _____

    wat will be the output?
    please expain ur ans

  • Anthony Irwin

    #2
    Re: Difference between for n while

    rohitjogya@gmai l.com wrote:
    Can anyone tell me the difference bet for loop and while loop
    execution?
    _______________ _____
    for (i=0 ; i<10 ; i++)
    ; /* Do nothing*/
    >
    print i;
    >
    _______________ ____
    i=0;
    while (i<10) {
    i++;
    }
    print i;
    _______________ _____
    >
    wat will be the output?
    please expain ur ans
    >
    Hi,

    You code won't work in c.

    Try the following instead.

    #include <stdio.h>

    int main(void) {
    int i=0, ii = 0;
    while (i<10) {
    i++;
    }
    printf("%d\n", i);

    for (ii=0 ; ii <10 ; ii++)
    ; /* Do nothing*/

    printf("%d\n", ii);
    return 0;
    }

    The answer to the above is 10 for both loops.

    The reason is that it reaches 9 and then increments in the loop and
    when it comes back around it is 10 and then the loop stops because it
    is not less then 10.

    Kind Regards,
    Anthony Irwin

    Comment

    • Anthony Irwin

      #3
      Re: Difference between for n while

      Anthony Irwin wrote:
      rohitjogya@gmai l.com wrote:
      >Can anyone tell me the difference bet for loop and while loop
      >execution?
      >______________ ______
      >for (i=0 ; i<10 ; i++)
      > ; /* Do nothing*/
      >>
      >print i;
      >>
      >______________ _____
      >i=0;
      >while (i<10) {
      > i++;
      >}
      >print i;
      >______________ ______
      >>
      >wat will be the output?
      >please expain ur ans
      >>
      Actually this program will probably show you more.

      #include <stdio.h>

      main() {
      int i=0, ii = 0;
      while (i<10) {
      printf("Inside Loop: %d\n", i);
      i++;
      }

      printf("Outside Loop: %d\n", i);

      for (ii=0 ; ii <10 ; ii++)
      printf("Inside Loop: %d\n", ii);

      printf("Outside Loop: %d\n", ii);
      return 0;
      }

      Output below:

      Inside Loop: 0
      Inside Loop: 1
      Inside Loop: 2
      Inside Loop: 3
      Inside Loop: 4
      Inside Loop: 5
      Inside Loop: 6
      Inside Loop: 7
      Inside Loop: 8
      Inside Loop: 9
      Outside Loop: 10
      Inside Loop: 0
      Inside Loop: 1
      Inside Loop: 2
      Inside Loop: 3
      Inside Loop: 4
      Inside Loop: 5
      Inside Loop: 6
      Inside Loop: 7
      Inside Loop: 8
      Inside Loop: 9
      Outside Loop: 10

      See how the loop never reaches 10 on the inside only outside the loop.
      The loop never executes when it is 10.

      the ii++ increments after the loop has complete in the for loop.

      Just play with some code and compile it any you will learn a fair bit.

      Kind Regards,
      Anthony Irwin

      Comment

      • rohitjogya@gmail.com

        #4
        Re: Difference between for n while

        Thank you so much

        Comment

        • rohitjogya@gmail.com

          #5
          Re: Difference between for n while

          If there is no any diff bet for and while loop, den why some people
          say that
          use while loop while handling pointers like in LinkedList programs?

          Comment

          • Richard Heathfield

            #6
            Re: Difference between for n while

            rohitjogya@gmai l.com said:
            If there is no any diff bet for and while loop, den why some people
            say that
            use while loop while handling pointers like in LinkedList programs?
            I y c b b t w E p a f a y c, w s w b b t r w y w ?

            --
            Richard Heathfield
            "Usenet is a strange place" - dmr 29/7/1999

            email: rjh at the above domain, - www.

            Comment

            • J. J. Farrell

              #7
              Re: Difference between for n while

              On Apr 1, 11:46 pm, "rohitjo...@gma il.com" <rohitjo...@gma il.com>
              wrote:
              If there is no any diff bet for and while loop, den why some people
              say that
              use while loop while handling pointers like in LinkedList programs?
              Please try to write in English. You'll need to ask the people who say
              that.

              Comment

              • CBFalconer

                #8
                Re: Difference between for n while

                "rohitjogya@gma il.com" wrote:
                >
                .... snip ...
                >
                wat will be the output?
                please expain ur ans
                Ankor Wat is overgrown and abandoned in the jungles of Cambodia. I
                believe the actual location of Ur is not accurately known, and that
                it is also buried in the sands of Mesopotamia. The pain comes from
                not taking the exlax. It is not connected with Ur.

                Your shift key is apparently broken.

                --
                Chuck F (cbfalconer at maineline dot net)
                Available for consulting/temporary embedded and systems.
                <http://cbfalconer.home .att.net>



                --
                Posted via a free Usenet account from http://www.teranews.com

                Comment

                • Army1987

                  #9
                  Re: Difference between for n while

                  <rohitjogya@gma il.comha scritto nel messaggio
                  news:1175491046 .417003.21000@d 57g2000hsg.goog legroups.com...
                  Can anyone tell me the difference bet for loop and while loop
                  execution?
                  _______________ _____
                  for (i=0 ; i<10 ; i++)
                  ; /* Do nothing*/
                  >
                  print i;
                  >
                  _______________ ____
                  i=0;
                  while (i<10) {
                  i++;
                  }
                  print i;
                  In C89, the only relevant differences between

                  expression_0;
                  while (condition) {
                  expression_1;
                  expression_2;
                  expression_3;
                  expression_n;
                  }

                  and:

                  for (expression_0; condition; expression_n) {
                  expression_1;
                  expression_2;
                  expression_3;
                  }

                  are:

                  1. In the first case, condition cannot be empty, in the second case it can
                  and if it is it is considered to be 1 (always true).
                  2. If a continue statement is used in the for loop, expression_n is evalued
                  before checking condition and resuming the loop from expression_1 if it is
                  true.

                  In C99 expression_0 can be a variable declaration, and the variable so
                  declared is only visible within the loop body. There are also other
                  differences, but unless you want to do pointless things such as re-defining
                  an enum in the loop body, they won't show up.

                  (Personally, I consider while (condition) a shorthand for for (;condition;)
                  rather than vice versa...)


                  Comment

                  • rohitjogya@gmail.com

                    #10
                    Re: Difference between for n while

                    Exactly....
                    I was look for this information only...
                    For and while loop will produce diff o/ps in case of continue
                    statement !!

                    Comment

                    • Gunvant Patil

                      #11
                      Re: Difference between for n while

                      On Apr 2, 11:46 am, "rohitjo...@gma il.com" <rohitjo...@gma il.com>
                      wrote:
                      If there is no any diff bet for and while loop, den why some people
                      say that
                      use while loop while handling pointers like in LinkedList programs?
                      I would say For is better choice than While if you are dealing with
                      LinkedList.

                      for (temp = head; temp->nxt != NULL; temp = temp->nxt) {
                      /* Do something */
                      }

                      temp = head;
                      while (temp->nxt) {
                      /* Do something */
                      temp = temp->nxt;
                      }

                      decide your self which is more readable and less prone to errors.

                      -Cheers,
                      Gunvant
                      ~~~~~~~~
                      No trees were killed in the sending of this message. However a large
                      number of electrons were terribly inconvenienced.

                      Comment

                      Working...