Thread: Simple c programming

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    Simple c programming

    Given two programmes are same ???

    if yes then try both with these inputs 4 2 3 -1 4 -1 -2 4
    i am getting different answers :/

    don't know why??
    plz help
    Code:
    #include<stdio.h>
     int main()
     {
     int x,i=1,sum=0;
     /*printf("no. of numbers");*/
     /*scanf("%d",&n);*/
     for(i=0;i<5;i++)
     {
     printf("enter the values");
     scanf("%d",&x);
     if(x<0)
     {
     continue;
     }
     sum+=x;
    
     }
     printf("%d",sum);
     }
    
    ----------------------------------------------------------------------------------------------------------------
    
    
    #include<stdio.h>
     int main()
     {
     int x,i=1,sum=0;
     /*printf("no. of numbers");*/
     /*scanf("%d",&n);*/
     for(i=0;i<5; )
     {
     printf("enter the values");
     scanf("%d",&x);
     if(x<0)
     {
     continue;
     }
     sum+=x;
    i+=1;
     }
     printf("%d",sum);
     }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Because the 'continue' will not execute the i+=1 in the second example.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    but both have same meaning means na.... if we define increment i++ in for loop or we define it at the end of the loop body (i=i+1 . Then why it is coming like this??

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Let's say that in the second piece of code,the input is negative number.Then x<0 is true,so it will go into the body of the if and continue.It will ignore ALL the rest of the body of the loop.Better now?

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    hmm.. I know that after continue every statement in body will not be excuted. I am not getting the for loop thing .. means in both case the increment of 'i' will be at the end of the loop body ... so it'll be ignored for the -ve input case and control should be at the begining of for loop till it gets the +ve value . Why it is ignoring increment of 'i' in later case and not in the earlier .
    Tanks for reply but still waiting for your reply :P

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    To expand on the comment by nonoob:

    "continue" makes execution jump back to the beginning of the loop.

    In the first case, after "continue" is encountered, execution jumps back to the beginning of the "for()" loop, and 'i' is incremented as defined in the body of the loop.

    In the second case, after "continue" is encountered, execution jumps back to the beginning of the "for()" loop skipping the "i+=1;" line below it.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    skipping the "i+=1; is logical .. it is jumping back to the starting of the loop .But in the first case
    for(i=0;i<5;i++) <<--- here should it think as it is incrementing i's value here (at starting of loop body :O)
    so basically when does this i gets incremented ??? in "for(i=0;i<5;i++)" or after this in the loop body (n where )

    sorry I am slow .
    Last edited by vivekgupta; 10-25-2012 at 04:00 PM.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    for( i = 0 ; i < 2 ; i++ )
    {
            /*start of body of loop*/
            /*your code that does some work(it does not change value of i)*/
            /*end of body of loop*/
    }
    How i have it in mind is this :
    From the point that the code reaches the for(part1 ; part2 ; part3)
    -in our example part1 is i =0
    part2 is i < 5
    part3 is i++
    • Just reached the for loop,so execute part1,thus i is set to zero
    • stay there and execute part2,thus checking if the condition i < 2 is true.i=0,so i<2 is true.Because of the true,go into the body
    • reach start of body of loop
    • execute your code
    • reach end of body loop
    • go to the for(...) and execute part3,thus i is increased by one,so i equals one now
    • stay there and execute part2,thus checking if the condition i < 2 is true.i=1,so i<2 is true.Because of the true,go into the body
    • reach start of body of loop
    • execute your code
    • reach end of body loop
    • go to the for(...) and execute part3,thus i is increased by one,so it is set to 2 now
    • stay there and execute part2,thus checking if the condition i < 2 is true.i=2,so i<2 is false.Because of the false,we say goodbye to this for loop and go on with the rest of code(after the loop)

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Dude you are awesomeeeeeeee. Thank you very much.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by vivekgupta View Post
    Dude you are awesomeeeeeeee. Thank you very much.
    Not so much as the other two users posted here for sure You are welcome .Hope i helped

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Quote Originally Posted by std10093 View Post
    Not so much as the other two users posted here for sure You are welcome .Hope i helped
    Thank you all @^ guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need simple programming help
    By Gbomb in forum C Programming
    Replies: 11
    Last Post: 02-07-2012, 04:51 PM
  2. simple C programming... but i need help PLZ
    By astrosona in forum C Programming
    Replies: 1
    Last Post: 03-04-2009, 08:58 PM
  3. Simple Programming Help Req
    By umar7001 in forum C++ Programming
    Replies: 6
    Last Post: 01-27-2008, 02:34 PM
  4. Simple programming help
    By IxTanGxI in forum C Programming
    Replies: 5
    Last Post: 02-21-2006, 02:00 AM
  5. Help with simple programming
    By jjj93421 in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2004, 07:05 AM