Himanshu Singh
Himanshu Singh

Reputation: 11

Why for loop is not working in switch case control?

In case control 1 for loop is not executing. I don't understand why?

In case control 1 printf function is working but for loop is not. And turboc++ didn't get error and warning message after compiling.

Program I try :-

    '''
    #include<studio.h>
    #include<conio.h>
    void main()
    {

    clrscr();
    int fun,inp,i;
    printf ("Enter a number = 
            = ");
    scanf ("%d",&inp);
    printf ("\n");

    printf ("Enter 1 for 
            reverse the number 
            \n");
    printf ("ENTER = ");
    scanf ("%d",&fun);
    printf ("\n");

    switch (fun)
    {
     case 1 :
     printf ("\n Case 1 \n");
     for (i=0;i>=inp;i++)
     {
      printf ("\n %d \n",inp);
      inp=inp-1;
     }
     break;
    }

    getch();
    }
    '''

Upvotes: -2

Views: 66

Answers (1)

Jo&#235;l Hecht
Jo&#235;l Hecht

Reputation: 1842

You may change the for loop to :

for (i=0; i<=inp; i++)  // Note the comparison operator
{
  printf ("\n %d \n",inp);
  inp=inp-1;
}

Or a bit shorter with one less variable :

while (inp >= 0)
{
  printf ("\n %d \n",inp);
  inp--;
}

Upvotes: 0

Related Questions