Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    C Programming මුල සිට ඉගෙනගනිමු (part 8 - Switch Statement)

    Blogs
    c programming
    3
    3
    487
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Kalana
      Kalana last edited by Kalana

      සුභ දවසක් වේවා ඔයාලට. අද ම්න් කතා කරන්න බලාපොරොත්තු වෙන්නේ C Programming වල එන Switch Statement එක ගැනයි. Switch Statement එක කියන්නේ අපි කලින් කතා කරපු
      if-else statement එකට සමාන දෙයකි. මෙහිදීද කරන්නේ අප ගත් තීරණයක් හරිද වැරදිද කියලා සොයා බැලීමයි.

      අපි දැන් බලමු Switch Statement එකේ ආකෘතිය මොන වගේ එකක්ද කියලා.

      switch(variable)
      {
      	case condition1 : //codes
      			  //codes
      			  break;
      	
      	case condition2 : //codes
      			  //codes
      			  break;
      					  
      	default : //codes
      		  //codes
      		  break;
      }
      
      • switch(variable) -: මෙහිදී සිදුවන්නේ අපට පරීක්ශා කිරීමට සිදුවන variable එක switch එකක් තුල store කර ගැනීමයි. එසේ කල විට අපට if-else වගේ නැවත නැවත එම
        variable එක ලිවීමට අව්ශ්‍ය නොවේ.

      • case condition1 : //codes -: මෙහිදී අප කරන්නේ අප ඇතුලත් කරපු දත්තය සත්‍ය හෝ අසත්‍යදැයි බැලීමයි. සත්‍ය නම් අපගේ //codes ටික execute වන අතර අසත්‍යනම් ඊලග
        case එක execute වේ. මෙය හරියට if-else වල එන if එකට හා elseif` එකට සමාන වේන්

      • default : //codes -: මෙය හරියටම if-else වල එන else වලට සමාන වේ. කිසිම case එකක් සත්‍ය නොවේනම් default එක ක්‍රියාත්මක වේ. මෙයට කිසිවිටකත්
        condition එකක් නැත.

      • break; -: මෙය යොදන්නේ case හා default සියල්ලම එකවර execute වීම වැලැක්වීමටයි. මෙය යෙදුවේ නැති වුවහොත් case හා default සියල්ලන්ගේම ඇති codes
        run වේ. මෙය මගින් කිසියම් case එකක් සත්‍ය වුවහොත් එම case එක පමණක් run වී සම්පූර්ණswitch(){} එකෙන් අපව එලියට ගෙන යයි.

      Switch Statement භාවිත කිරීමේදී අප නීති කිහිපයක් අනුගමනය කලයුතු වනවා.

      1. switch(variable) මෙහි එන variable එක සෑම විටම integer හෝ character එකක් විය යුතු වනවා. එය කිසිවිටකත් float විය නොහැකිය.
      2. case condition : මෙහි එන condition එකත් සෑම විටම integer හෝ character එකක් විය යුතු වනවා.
      3. case condition : මෙහි එන condition භාවිතා කල හැක්කේ switch එකක් තුල පමණි.
      4. break; අනිවාර්‍ය අංගයක් නොවන අතර එය භාවිතා කරන්නේ. එකවර case කිහිපයක් run වීම වැලැක්වීමටයි.

      මන් කිව්ව ගොඩක් දේවල් පැහැදිලි වෙයි මේ උදාහරණයෙන්.

      Example -: Input any number from the keyboard. After that check the following using switch statement

      • number == 25 , print "number is equal to 25"
      • number == 50 , print "number is equal to 50"
      • number == 75 , print "number is equal to 75"
      • else, print "Your number is not equal to 25, 50 or 75"

      write a C program to fulfill this conditions.

      මෙහිදී කියන්නේ කිසියම් සංඛ්‍යාවක් ඇතුලත් කල විට එය 25, 50, 75 ට සමානනම් සමාන ලෙසත් සමාන නොවේනම් සමාන නොවේ ලෙසත් output කල යුතු බවයි.

      #include <stdio.h>
      
      int main()
      {
          int number;
          
          printf("Enter a number = "); //කිසියම් සංඛ්‍යාවක් ඇතුලත් කිරීම.
          scanf("%d",&number);
          
          switch(number)
          {
              case 25 : printf("Number is equal to 25"); //ඇතුලත් කල සංඛ්‍යාව 25ට සමානනම්
                        break;
                        
              case 50 : printf("Number is equal to 50");//ඇතුලත් කල සංඛ්‍යාව 50ට සමානනම්
                        break;
                        
              case 75 : printf("Number is equal to 75");//ඇතුලත් කල සංඛ්‍යාව 75ට සමානනම්
                        break;
      
              default : printf("Number is not equal to 25, 50 or 75");//ඇතුලත් කල සංඛ්‍යාව 25, 50, 75 ට සමාන නොවේනම්
                        break;
          }
          return 0;
      }
      

      Output

      First run -: Enter a number = 25
                   Number is equal to 25
      			 
      Second run -: Enter a number = 50
                    Number is equal to 50
      			 
      Third run -: Enter a number = 75
                   Number is equal to 75
      			 
      Fourth run -: Enter a number = 100
                    Number is not equal to 25, 50 or 75
      

      අපි දැන් බලමු break; නැතුව මෙම code එක run කලාම ලැබෙන output එක මොකක්ද කියලා

      #include <stdio.h>
      
      int main()
      {
          int number;
          
          printf("enter a number = ");
          scanf("%d",&number);
          
          switch(number)
          {
              case 25 : printf("\nNumber is equal to 25"); //break; යොදා නැත
              case 50 : printf("\nNumber is equal to 50");
              case 75 : printf("\nNumber is equal to 75");
              default : printf("\nNumber is not equal to 25, 50 or 75");
          }
          return 0;
      }
      

      Output

      First run -: Enter a number = 25
                   Number is equal to 25
      	     Number is equal to 50
      	     Number is equal to 75
      	     Number is not equal to 25, 50 or 75
      			 
      Second run -: Enter a number = 50
                    Number is equal to 50
      	      Number is equal to 75
      	      Number is not equal to 25, 50 or 75
      			 
      Third run -: Enter a number = 75
                   Number is equal to 75
      	     Number is not equal to 25, 50 or 75
      			 
      Fourth run -: Enter a number = 100
                    Number is not equal to 25, 50 or 75
      

      ඉහත උදාහරණයෙන් උබට පෙනෙනවා break; එකක තිබෙන වටිනාකම හා switch එකක ක්‍රියාකාරිත්වය.

      if-else වල වගේ switch වලත් nested ආකාර පවතී. එනම් switch ඇතුලේ තව switch තිබීමට හැකිය.

      Example

      #include <stdio.h> 
      
      int main () 
      {  
          int number1;  
          int number2;  
          
          printf("Enter Number1 = ");
          scanf("%d", &number1);
             
          switch(number1) 
          {  
              case 10 : printf("Number1 is equal to 10");  
                        break;
                        
              case 20 : printf("Number1 is equal to 20 there for enter Number2 = ");
                        scanf("%d", &number2);
                        switch(number2) 
                        {  
                              case 50 : printf("Number2 equal to 50");  
                                        break;
                                        
                              default : printf("Number2 not equal to 50"); 
                        }
                        break;
                         
              default : printf("Number1 is not equal to 10 or 20");
             }  
              
             return 0;  
      }  
      

      Output

      First run -: Enter Number1 = 10
                   Number1 is equal to 10
      			 
      Second run -: Enter Number1 = 20
                    Number1 is equal to 20 there for enter Number2 = 50
      	      Number2 equal to 50
      			 
      Third run -: Enter Number1 = 20
                   Number1 is equal to 20 there for enter Number2 = 70
      	     Number2 not equal to 50
      			 
      Fourth run -: Enter Number1 = 100
                    Number1 is not equal to 10 or 20
      

      මන් ඔයාලට ප්‍රශ්න කිහිපයක් දෙන්නම් උත්තරත් එක්ක උත්සහ කරලා බලන්න කරන්න පුලුවන්ද කියලා. බැරි උනොත් හරි තේරෙන් නැත්තම් හරි මට message එකක් දාන්න.

      Question 1 -: Write a C program to create a simple calculator. The program should allow the user to
      enter two numbers and an option. The options are

      1. +
      2. -
      3. *
      4. /
        You should print an appropriate error message if the user enters invalid operator. Use switch statement in your program.

      Answer -: https://drive.google.com/open?id=1Osup1sSG3x60JDWd1aH9v4ked5Z8fsk0


      Question 2 -: An online retailer sells five different products whose retail prices are shown in the following
      table:

      Product number  Retail price
            1            2.98
            2            4.50
            3            9.98
            4            4.49
            5            6.87
      

      Write a C program that reads the following:

      1. Product number
      2. Quantity sold for one day
        Your program should use a switch statement to determine the total retail price for each product.

      Answer -: https://drive.google.com/open?id=1f6ZZkLNwYe0Qds8EHzsIHxX_rayfcSzU


      Question 3 -: Write a C program that compute the area of a square (area=side*2 ) or a triangle
      (area=base*height/2) after prompting the user to type the first character of the shape (S or T).

      1. Write the program using if-else statements
      2. Write the program using switch statement.
      3. Modify the program to find the area of a circle when the Letter “C” is entered.
      4. Modify the program to print an error message when the user enters an invalid
        character.

      Answer -: https://drive.google.com/open?id=1p2O7iIWCV4uRF1eR7_6dYz0gkm2i-RTR


      • මූලාශ්‍ර -: https://beginnersbook.com/2014/01/switch-case-statements-in-c/

      • C programm එකක් windows වල run කරන විදිහ --> https://bit.ly/2O6rLXR

      ඔබට අවශ්‍යනම් ඔබේ බ්‍රව්සර් එක හරහා online C programms run කරන්න පුලුවන්. එහෙම කරන්න පුලුවන් ලින්ක්ස් මන් පහතින් දාන්නම්

      • https://www.onlinegdb.com/online_c_compiler
      • https://www.tutorialspoint.com/compile_c_online.php
      • https://repl.it/languages/c

      සරලව මුල ඉදන් C programming ඉගෙන ගන්න පුලුවන් හොදම site කිහිපයක් මන් දාන්නම්

      • https://www.geeksforgeeks.org/c-programming-language/
      • https://www.tutorialspoint.com/cprogramming/index.htm
        https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/

      web url කෙටි කරන්න lankadevelopers ලා හදපු එයාලගෙම සයිට් එකක් තියෙනවා. -> https://link.lankadevelopers.com/

      මගේ කලින් ලිපි

      • C Programming මුල සිට ඉගෙනගනිමු(part 1 - Introduction) -: https://link.lankadevelopers.com/4WpH
      • C Programming මුල සිට ඉගෙනගනිමු (part 2 - Variables) -: https://link.lankadevelopers.com/mXio
      • C Programming මුල සිට ඉගෙනගනිමු (part 3 - Operators) -: https://link.lankadevelopers.com/SHNt
      • C Programming මුල සිට ඉගෙනගනිමු (part 4 - Input & Output functions) -: https://link.lankadevelopers.com/2MNku
      • C Programming මුල සිට ඉගෙනගනිමු (part 5 - create simple applications) -: https://link.lankadevelopers.com/KUF6
      • C Programming මුල සිට ඉගෙනගනිමු (part 6 - Decision making(if-else statement - part 1)) -: https://link.lankadevelopers.com/8Xe71
      • C Programming මුල සිට ඉගෙනගනිමු (part 7 - Format Specifiers in C) -: https://link.lankadevelopers.com/761PT

      මන් ඊළග ලිපියෙන් කියලා දෙන්නම් Iterate(loops)statements ගැන . මගේ ලිපි වල අඩුපාඩු තියෙනවනම් දන්නේ නැති දේවල් තියෙනවනම් පහලින් කමෙන්ට් එකක් දාන්න.
      තව ලිපියකින් හම්බෙමු ජය වේවා.

      1 Reply Last reply Reply Quote 1
      • dev_lak
        dev_lak last edited by

        niyamai machan, thanks

        1 Reply Last reply Reply Quote 1
        • root
          root Linux Help last edited by root

          superb bro, thank you.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post

          0
          Online

          3.7k
          Users

          1.3k
          Topics

          5.3k
          Posts

          • Privacy
          • Terms & Conditions
          • Donate

          © Copyrights and All right reserved Lanka Developers Community

          Powered by Axis Technologies (PVT) Ltd

          Made with in Sri Lanka

          | |