Skip to main content

14. Goto statement and Type casting

 

C goto statement

The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the program control to a predefined label. The goto statement can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can't be done by using a single break statement.

#include <stdio.h>

int main()
{
   int num, i = 1;
   printf("Enter the number whose table you want to print : ");
   scanf("%d", &num);
   table:
   printf("%d x %d = %d\n", num, i, num*i);
   i++;
   if (i <= 10){
      goto table;
   }
   return 0;
}

Enter the number whose table you want to print : 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

#include <stdio.h>

int main()
{
   int i, j, k;
   for (i = 0; i < 10; i++){
      for (j = 0; j < 5; j++){
         for (k = 0; k < 3; k++){
            printf("%d %d %d\n", i, j, k);
            if (j == 3){
               goto out;
            }
         }
      }
   }
   out:
   printf("Came out of the Loop.");
   return 0;
}

0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
0 3 0
Came out of the Loop.

Type Casting in C

Type Casting allows us to convert one data type into the other. In C language, we use cast operator for typecasting which is denoted by (type).

#include <stdio.h>

int main()
{
   int f = 9/4;
   printf("f : %d\n", f);
   float a = (float)9/4;
   printf("a : %f\n", a);
   return 0;
}

f : 2
a : 2.250000

Comments