I'm a beginner at c. I learnt that goto statement can be used to get out of all nested loops at a time. I also learnt that it is not so preferred in C. However, I use it frequently because I think it helps a lot, and sometimes, it's much easier than the common alternatives.
Here is a little program in which I used goto statement to correct the user's mistake instead of using a loop.
So, my question is : Should I really stop using it?
#include <stdio.h>
#include <stdlib.h>
int main()
{
/*A program to store a number in 4 bits only !*/
printf("Enter x then z :\n");
int x, y;
Start:
scanf("%d %d", &x, &y);
if((x > 15) || (y > 15) || (x < 0) || (y < 0))
{
printf("Wrong numbers! : 0<= x,y <=15\n");
printf("Enter the numbers again : \n");
goto Start;
}
char z;
x<<= 4;
z = x + y;
printf("z = %d", z);
return 0;
}