Dynamic jump to label in C

后端 未结 5 964
清酒与你
清酒与你 2021-01-05 01:29

I would like to display the output - numbers 1 to 5, followed by 4-5 infinitely. Is there any way i can pass the value of i(4) instead of the character i in goto1. Or is the

5条回答
  •  耶瑟儿~
    2021-01-05 02:17

    Wouldn't a switch accomplish the same thing?

    int main()
    {
        int i = 1;
        while (1)
        {
            switch (i)
            {
                case 1:
                    printf(" num is 1 \n");
                case 2:
                    printf(" num is 2 \n");
                case 3:
                    printf(" num is 3 \n");
                case 4:
                    printf(" num is 4 \n");
                case 5:
                    printf(" num is 5 \n");
                default:
                    break;
            }
    
            // code to calculate i
            i = 4;
            // end code to calculate i
        }
        return 0;
    }
    

提交回复
热议问题