Java/C2/Switch-Case/Gujarati
From Script | Spoken-Tutorial
| Time' | Narration |
| 00:02 | જાવામાં સ્વિચ કેસ પરના સ્પોકન ટ્યુટોરીયલમાં તમારું સ્વાગત છે. |
| 00:06 | આ ટ્યુટોરીયલમાં, જાવામાં સ્વીચ કેસ કન્સટ્રક નો ઉપયોગ કેવી રીતે કરવું તે શીખીશું. |
| 00:11 | આ ટ્યુટોરીયલ માટે આપણે,
|
| 00:21 | આ ટ્યુટોરીયલ માટે, તમને જાવામાં ઇફ એલ્સ સ્ટેટમેન્ટ વિષે જ્ઞાન હોવું જરૂરી છે. |
| 00:25 | જો નહિં, અમારી વેબસાઇટ [1] પર આ વિષયો પર ઉપલબ્ધ ટ્યુટોરિયલ્સ જુઓ. |
| 00:32 | સ્વીચ કેસ સ્ટેટમેન્ટ વેરિયેબલની વેલ્યુ પર આધારિત એક્શન કરવા માટે ઉપયોગી છે. |
| 00:39 | અહીં સ્વીચ કેસ સ્ટેટમેન્ટ માટે સિન્તેક્ષ છે. |
| 00:44 | ચાલો હવે તેનો ઉપયોગ કરીએ. |
| 00:47 | મેં પહેલેથી જ Eclipse ખોલ્યું છે. |
| 00:49 | મેં SwitchCaseDemo નામનો એક ક્લાસ બનાવેલ છે. |
| 00:53 | હવે ચાલો કેટલાક વેરીયેબલો ઉમેરિયે. |
| 00:57 | મેઈન મેથડ અંદર, આપણે int ટાઇપનો day નામનું વેરિયેબલ બનાવીશું. |
| 01:02 | તો મેઈન મેથડ અંદર ટાઇપ કરો, int day , અને આપણે તેને વેલ્યુ આપીશું, equal to 3 સેમી કોલોન.
|
| 01:12 | હવે, ચાલો સ્ટ્રીંગ ટાઇપનું વેરિયેબલ dName બનાવીએ.
|
| 01:18 | String dName , આપણે તેને null થી ઇનીસીલાઈઝ કરી શકીએ છીએ.
|
| 01:25 | અહીં dName એક વેરિયેબલ છે જે અઠવાડિયાના દિવસો નામોને સંગ્રહે છે.
|
| 01:34 | day દિવસ નમ્બર સંગ્રહે છે.
|
| 01:36 | હવે, આપણે સ્વીચ કેસ સ્ટેટમેન્ટ ટાઇપ કરીશું. તો આગામી લાઈન ટાઇપ કરો,
|
| 01:43 | સ્વીચ કૌંસ અંદર day, પછી કર્લી કૌંસ ખોલો ... એન્ટર દબાવો.
|
| 01:52 | આ સ્ટેટમેન્ટ કયા વેરીયેબલો કેસો માટે ધ્યાનમાં છે, તે વ્યાખ્યાયિત કરે છે. |
| 01:59 | આગામી લાઇનમાં ટાઇપ કરો,
|
| 02:01 | case 0 કોલન,
|
| 02:04 | આગામી લાઇન, dName ઇકવલ ટુ, ડબલ અવતરણ ચિહ્ન અંદર, Sunday સેમી કોલન,
|
| 02:14 | પછી આગામી લાઈનમાં break ટાઇપ કરો,
|
| 02:17 | આ સ્ટેટમેન્ટ કહે છે કે, જો day 0 છે, તો dName, Sunday થી સુયોજિત થયેલ હોવું જોઈએ. |
| 02:26 | નોંધ લો કે break સ્ટેટમેન્ટ દરેક કેસના અંતે જ વાપરી શકાય છે. |
| 02:31 | break સ્ટેટમેન્ટ વિના, સ્વીચ કેસ જટિલ ફેશન માં કાર્ય કરે છે. |
| 02:35 | તે ટ્યુટોરીયલના અનુગામી ભાગમાં સમજાવવામાં આવશે. |
| 02:40 | એ જ રીતે, ચાલો બાકીના કેસો લખીએ.
|
| 02:45 | આગામી લાઈનમાં ટાઇપ કરો, case 1 કોલન, |
| 02:50 | આગામી લાઇનમાં ટાઇપ કરો, dName ઇકવલ ટુ, ડબલ અવતરણ ચિહ્ન અંદર Monday સેમીકોલન, |
| 02:56 | આગામી લાઇનમાં break ટાઇપ કરો. |
| 02:58 | પછી ટાઇપ કરો, case 2 કોલન, |
| 03:01 | આગામી લાઇનમાં dName ઇકવલ ટુ Tuesday અને પછી સેમીકોલન, |
| 03:06 | આગામી લાઇનમાં break ટાઇપ કરો, |
| 03:08 | પછી case 3 કોલન ટાઇપ કરો, |
| 03:12 | આગામી લાઇનમાં dName ઇકવલ ટુ ડબલ અવતરણ ચિહ્ન અંદર Wednesday અને પછી સેમીકોલન ટાઇપ કરો, |
| 03:18 | આગામી લાઇનમાં break ટાઇપ કરો, |
| 03:20 | પછી case 4 કોલન ટાઇપ કરો, |
| 03:24 | આગામી લાઇનમાં dName ઇકવલ ટુ ડબલ અવતરણ ચિહ્ન અંદર Thursday અને પછી સેમીકોલન ટાઇપ કરો, |
| 03:32 | પછી break , |
| 03:24 | પછી case 5 કોલન ટાઇપ કરો, |
| 03:37 | dName ઇકવલ ટુ ડબલ અવતરણ ચિહ્ન અંદર Friday અને પછી સેમીકોલન ટાઇપ કરો, |
| 03:41 | પછી break લખો. |
| 03:43 | પછી case 6 કોલન ટાઇપ કરો, |
| 03:47 | આગામી લાઇનમાં dName ઇકવલ ટુ ડબલ અવતરણ ચિહ્ન અંદર Saturday અને પછી સેમીકોલન ટાઇપ કરો, |
| 03:55 | પછી break સેમીકોલોન ટાઇપ કરો, |
| 03:59 | પછી કૌંસ બંધ કરો. |
| 04:03 | now Let us add a print statement and see the code in action. |
| 04:07 | So next line Type System dot out dot println within brackets dName then semicolon.
|
| 04:16 | Now Save and run the file.
|
| 04:20 | Now press Ctrl S and Ctrl F11 keys
|
| 04:25 | we get the output as Wednesdaywhich is corresponding to the case 3.
|
| 04:31 | Now Let us change the value of the day and see the result |
| 04:35 | SoChange 3 to 0
|
| 04:38 | NowSave and Run tht file
|
| 04:40 | As we can see, the output is Sunday corresponding to the case 0
|
| 04:46 | Now what if there is no case corresponding to the value. So Let us see that |
| 04 :52 | change day equal to -1 Save and run the file |
| 04:58 | As we can see, there is no output.
|
| 05:01 | But it would be better if we could have a case for all other values.
|
| 05:06 | That is done by using the default keyword. |
| 05:09 | So After the last case, type
|
| 05:12 | default colon |
| 05:14 | Next line dName equal to within double quotes Wrong Choice then semicolon |
| 05:24 | Next line break semicolon |
| 05:27 | We do not say case default;
|
| 05:30 | Note that we simply use the keyword default .
|
| 05:34 | Now let us run the code. So save and run the file
|
| 05:38 | As we can see, the default case is executed and the required message Wrong choice is printed.
|
| 05:45 | Let us try with another random value. |
| 05:48 | Change -1 to 15
|
| 05:51 | As we can see, again the default case is executed. |
| 05:57 | Now let us see what happens if we remove the break statement. |
| 06:01 | So Let us change day = 15 to day = 4
|
| 06:07 | remove the corresponding break statement for day =4
|
| 06:12 | Now Save and run |
| 06:15 | Although the case is 4, we get the output as Friday and not Thursday. |
| 06:20 | because of the way switch case works.
|
| 06:24 | First the value of day is compared with 0.
|
| 06:29 | Then with 1 then with 2 and so on with all the possible cases. |
| 06:34 | When a match is found, it executes all the case from the match onwards.
|
| 06:42 | In our case, it executed case 5 after case 4 .
|
| 06:47 | Then it stops because of the break statement in case 5. |
| 06:53 | To avoid that, we need to add a break statement in each case. |
| 06:57 | Let us now add the break statement we have removed.
|
| 07:00 | So type break semicolon.
|
| 07:05 | Now let us run the code. |
| 07:08 | As we can see, now only case 4 is executed.
|
| 07:13 | As a rule, remember to use a break statement in every case to avoid errors. |
| 07:20 | We have come to the end of this tutorial.
|
| 07:22 | In this tutorial we have learnt how to use switch case construct and how to use break statement. |
| 07:30 | As an assignemet write a program that has a name and gender as a variable Use a switch case statement that prints “Hello Mr....” for males and “Hello Ms...” for females. |
| 07:44 | To know more about the Spoken Tutorial project, watch the video available at the following link, it summarises the spoken-tutorial project. |
| 07:53 | If you do not have good bandwidth you can download and watch it. |
| 07:58 | The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test.
|
| 08:06 | For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
| 08:12 | Spoken Tutorial Project is a part of the Talk to a Teacher project and it is supported by the |
| 08:17 | National Mission on Education through ICT, MHRD, Government of India. |
| 08:22 | More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro |
| 08:31 | This tutorial has been contributed by TalentSprint. Thanks for joining.
|