1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple c programming

Discussion in 'C' started by vivekgupta, Oct 25, 2012.

  1. vivekgupta

    vivekgupta New Member

    Joined:
    Jun 10, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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);
     }
     
    Last edited by a moderator: Oct 26, 2012
  2. vivekgupta

    vivekgupta New Member

    Joined:
    Jun 10, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    They are not the same. In one, the for loop is:
    Code:
    for(i=0;i<5;i++)
    {
    ...
      continue; // clearly you don't understand what this statement does
    ...
    }
    
    and in the other it is:
    Code:
    for(i=0;i<5;)
    {
    ...
      continue; // clearly you don't understand what this statement does
    ...
    i+=1;
    }
    
    Go and RTFM on "continue" and you will see why the programs behave differently.
     
  4. Rajesh M. Kanojia

    Rajesh M. Kanojia New Member

    Joined:
    Dec 9, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    INDIA
    continue keyword is used to send the control back to the loop without executing remaing code.
    now comes to ur problem:-
    when u enter any -ve number then only continue statement get execute.
    in ur program increment of variable i is also play a important role how:-
    in ur first program i value get incremented as contol come back to the loop that's why u get answer 13
    but in second program the value of i will incremented only when if condition get false.

    try one more thing enter all number -ve u find that every time u get a message on console
    Enter the value:-
    the reason is i value doesn't change.
     
  5. smp

    smp New Member

    Joined:
    Jul 8, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    The only problem here is 'continue' statement. In your code, calling the 'continue' statement will result the execution to go back to the for loop for the next iteration. In the first case, calling the continue statement returns to the 'for' loop in which the value of 'i' is incremented. In the latter case the value of i is incremented after the continue statement. So if the continue statement is called in the second program, then the pointer returns to for loop without incrementing the value of i which makes the programs differ.

    In first case, even if continue statement is called, value of i increases.
    In second case, if continue statement is called, value of i remains the same.
     
  6. iranjit

    iranjit New Member

    Joined:
    Jan 5, 2013
    Messages:
    4
    Likes Received:
    2
    Trophy Points:
    0
    Home Page:
    When continue statement is executed, i+=1; is not executed in the second program. That's why you are getting different result.
     

Share This Page