Showing posts with label un-conditional. Show all posts
Showing posts with label un-conditional. Show all posts

continue statement

Continue Statement 
continue statement भी एक jump statement है । जो एक किसी एक भाग को Skip करता है । 

continue statement का  for loop में  use  


continue statement

continue statement को if के साथ उपयोग करते है । 
जैसे मान लीजिए की हमें 1 से 10 तक संख्याओ(5 को छोड़कर) को प्रिंट कराना है ।  तो इसके लिए  continue की मदद से इस तरह प्रोग्राम बनाएगे।

    /* Use of continue statement */      
              #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i;
               clrscr();   
               printf("Number 1 to 10\n");
               for (i=1;i<=10;i++)
              {
                  if(i==5)
                 {
                     continue; 
                 }
                 printf("%d ", i);
              }
                   getch(); 
                   return 0;
             }

Output :
Number 1 to 10 
1 2 3 4 6 7 8 9 10 

उपरोक्त प्रोग्राम में जैसे ही i की value 5 होगी । तो की condition true हो जायेगी और प्रोग्राम का कन्ट्रोल continue पर आयेगा फिर loop वापिस लौट जायेगा और i की value  5 प्रिन्ट नही होगी 



   /* Program to print odd numbers between 1 to 20 using continue statement */      
              #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i;
               clrscr();   
               printf("Odd numbers between 1 to 20 \n");
               for (i=1;i<=20;i++)
              {
                  if(i%2==0)
                 {
                     continue; 
                 }
                 printf("%d ", i);
              }
                   getch(); 
                   return 0;
             }
Output :

Odd numbers between 1 to 20  
1 3 5 7 9 11 13 15 17 19


NEXT   : User defined functions
PREVIOUS : Multiple Initialization of a for loop 

goto statement

goto statement 
goto statement को jump statement भी कहते है। इसका उपयोग करके प्रोग्राम में कही भी Jump कर करते है परन्तु उसके लिए Destination (Label) define करना पड़ता है।
Syntax
goto statement
अब एक प्रोग्राम के द्वारा समझते है। 

/* Program to print numbers from 1 to 10  using goto statement*/

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int i=1;   
               clrscr();  
               printf("Numbers from 1 to 10:\n");  
              a:  // label 
               if(i<=10)
               {
                       printf("%d ",i);   
                       i++;
                       goto a;
               }   
                   getch(); 
                   return 0;
             }
Output :
Number from 1 to 10 :
1 2 3 4 5 6 7 8 9 10


उपरोक्त प्रोग्राम में  i की value 1 होगी फिर प्रोग्राम का कन्ट्रोल printf("Numbers from 1 to 10:\n") पर आयेगा और यह मैसेज स्क्रीन पर प्रिन्ट हो जायेगा उसके बाद प्रोग्राम का कन्ट्रोल if(i<=10) पर आयेगा और check करेगा कि condition सही है या नही । उपरोक्त प्रोग्राम में हमारी condition सही है इसलिए printf("%d ",i) पर आयेगा और i की value,1 प्रिन्ट हो जायेगी  और इसके बाद i की value में 1 का increment होगा फिर प्रोग्राम का कन्ट्रोल goto a पर आयेगा और प्रोग्राम का कन्ट्रोल Jump करके लेविल a आ जायेगा और फिर से condition check होगी और यह process तब तक चलता रहेगा जब तक condition false नही होगी ।  


/* Program to check whether numbers is even or odd using to statement*/

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int number;   
               clrscr();  
               printf("Enter the Number:");
               scanf("%d", &number);  
               if(number%2==0)
               {
                       goto even;   
               } 
              else
              {
                    goto odd;
              }
             even:
                   printf("Number is Even");
                   getch();
                   exit(0);
             odd:
                  printf("Number is Odd");
                  getch(); 
                  return 0;
             }
Output :
Number from 1 to 10 :
1 2 3 4 5 6 7 8 9 10


exit() function का उपयोग प्रोग्राम को Terminate करने के लिए करते है। 


NEXT : Loops
PREVIOUS :  switch statement