Well here is an example of using goto
Code:
# include <stdio.h>
int main()
{
float num,average,sum;
int i,n;
printf("Maximum no. of inputs: ");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
goto jump;
sum=sum+num;
}
jump:
average=sum/(i-1);
printf("Average: %.2f",average);
return 0;
}
and here is a slightly different version of it.
Code:
#include <stdio.h>
int main()
{
float num, average, sum;
int i, n;
printf("Maximum no. of inputs: ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
{
average=sum/(i-1);
printf("Average: %.2f",average);
return 0;
}
else
sum+=num;
}
average=sum/(i);
printf("Average: %.2f",average);
return 0;
}
Questions:
Is the second version any better than the first in terms of unnecessary code(is the program more compact and concise)?
is there any way that you would have modified the first version without use of keyword like return, break, goto, etc?