y an indefinate loop??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shweta khare
    New Member
    • Aug 2008
    • 13

    y an indefinate loop??

    the range of character constants is from -128 to 127....but in the following program....


    main( )
    {
    char x;
    for(x= -128; x<=127;x++)
    {
    printf("\n %d %c",x,x);
    }
    getch( );
    }


    the program gets into an indefinate loop while printing the ascii value & the character from -128 to 127.Altough the program does print the output but it is in the form of an indefinate loop.why does the program get into an indefinate loop?



    in the above program if we alter the for statement as-


    for(x= -128;x<=126;x++)


    the above program prints the ascii value & the characters from -128 to 126 without getting into an indefinate loop.


    why does the program enter an indefinate loop on specifing the limits as -128 to 127 while it doesnt in the case of -128 to 126?





    Also we observed that on pressing ctrl+num lk/scroll lk we can escape an indefinate loop in progress.y does this happen....





    thank u......
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If characters can only hold values in the range [-128, 127] what would the value
    be of the addition 127+1?

    kind regards,

    Jos

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      indefinIte is not defined; infinite is endless, and what stray 'y' is I could not decipher yet. Look at binary representation of signeds.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        127+1 will overflow and the value goes back to -127 again.
        Thats why it is infinite loop.

        Raghu

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by gpraghuram
          127+1 will overflow and the value goes back to -127 again.
          Thats why it is infinite loop.

          Raghu
          -128; because 127 == 01111111 + 1 == 10000000 == -128

          kind regards,

          Jos

          Comment

          • shweta khare
            New Member
            • Aug 2008
            • 13

            #6
            the ascii values of 255 & -1 correspond to the same character same goes for
            254 & -2
            253 & -3
            252 & -4 & so on so forth...
            so if we change the for satement as
            for(x=0;x<=255; x++)


            it should print all the characters widout encountering the problem of overflow....
            but still it gives us an indefinate loop....



            thank u.........

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by shweta khare
              the ascii values of 255 & -1 correspond to the same character same goes for
              254 & -2
              253 & -3
              252 & -4 & so on so forth...
              so if we change the for satement as
              for(x=0;x<=255; x++)


              it should print all the characters widout encountering the problem of overflow....
              but still it gives us an indefinate loop....



              thank u.........
              Sure, because the largest value that can be stored in a (signed) char is 127
              which is definitely smaller than 255 so your loop never ends; if you insist of
              having a char as your loop index better make that loop a do-while loop as in:

              [code=c]
              char x= -128;
              do {
              ...
              } while (x++ != 127);
              [/code]

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Realize also (as tacitly implied by Jos' posts above) that the only guaranteed values for char are 0 to 127.

                Comment

                • shweta khare
                  New Member
                  • Aug 2008
                  • 13

                  #9
                  thank u for the guidance....

                  Comment

                  • shweta khare
                    New Member
                    • Aug 2008
                    • 13

                    #10
                    Originally posted by JosAH
                    Sure, because the largest value that can be stored in a (signed) char is 127
                    which is definitely smaller than 255 so your loop never ends; if you insist of
                    having a char as your loop index better make that loop a do-while loop as in:

                    [code=c]
                    char x= -128;
                    do {
                    ...
                    } while (x++ != 127);
                    [/code]

                    kind regards,

                    Jos



                    Y does it so happen that while we are using the do-while loop the character corresponding to the ascii value of 127 gets printed but using the same specifications in the for loop only characters upto ascii 126 gets printed and not that corresponding to 127?........... .

                    Comment

                    • edwardrsmith
                      New Member
                      • Feb 2008
                      • 62

                      #11
                      It has to do with where the condition is checked in the loop. In a for loop the condition (in this case X != 127) is checked before each iteration so since x is incremented at the end of the iteration, the loop is never run with x equal to 127.

                      With a do-while loop the condition is checked at the end of the loop. In this case, the do-while loop will run one more time than the for loop. (If you changed the condition in the for loop to x != 128 the for loop would print 127).

                      Edward

                      Comment

                      • shweta khare
                        New Member
                        • Aug 2008
                        • 13

                        #12
                        do preincrementati on and postincrementat ion lose their functionallity when used in a for loop??...like for example-

                        main( )
                        {
                        char x;
                        for( x=12;x!=14;x++)
                        printf("\n %d %c",x,x);
                        getch( );
                        }

                        the output is the same even if we use ++x in place of x++ ie it prints the output for ascii 12 & 13.


                        but if we are using while or do-while loop then in the case of preincrementati on the output is for 12 & 13 and in the case of postincrementat ion the output is for 12,13 &14.....

                        (in the case of while loop we have initialised x=11)

                        Comment

                        • edwardrsmith
                          New Member
                          • Feb 2008
                          • 62

                          #13
                          pre and post incrementation is only helpful in a case like

                          Code:
                          int x = 0;
                          int y, z;
                          
                          y = ++x;
                          z = x++;
                          The difference between the above and a for loop is that the x++ in a for loop runs

                          Code:
                          x++
                          There is no assignment or operation being performed at the same time as the incrementation so it doesn't matter.

                          Edward

                          Comment

                          • Savage
                            Recognized Expert Top Contributor
                            • Feb 2007
                            • 1759

                            #14
                            Originally posted by edwardrsmith
                            pre and post incrementation is only helpful in a case like

                            Code:
                            int x = 0;
                            int y, z;
                            
                            y = ++x;
                            z = x++;
                            The difference between the above and a for loop is that the x++ in a for loop runs

                            Code:
                            x++
                            There is no assignment or operation being performed at the same time as the incrementation so it doesn't matter.

                            Edward
                            Result stays same,but ++x is a bit faster.

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by Savage
                              Result stays same,but ++x is a bit faster.
                              That all depends on the processor being used of course.

                              kind regards,

                              Jos

                              Comment

                              Working...