Header Ads

switch case:C Programming Language - Part 4.5

switch case



Depending on a value, switch case is used to execute a Statement from many Statements. The switch case is usually written as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Switch (variable) {
    Case expression 1:
        Statement;
        Break;
    Case expression 2:
        Statement;
        Break;
    Case expression 3:
        Statement;
        Break;
    Default:
        Statement;
        Break;
}
Here, if the value of the variable of variable varies with expression 1, then the statement of case expression1 will be executed. If the value of the variable matches the value of 2, then the statement of case expression 2 will be executed. If the value of the variable matches the value of 3, then the statement of case expression 3 will be executed. If no one matches it, then the default statement will be executed. Here you can add as many cases as you like. And at the end of the Statement of the case break; To add. Break; This means that we can get out of the switch case. Break; It is very important.

It may seem a little complicated yet, if we look at an example, this switch case statement will become very easy. We will write a program, where if we input r, then it will be written if you select Red, if w inputs, then the text will be written You select White. If b inputs, then the text will be written You select Black And we will write the program using switch case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
Int main ()
{
Char colorCode;
Printf ("Enter first word of Red, White or Black: \ n");
Scanf ("% c", & colorCode);
Switch (colorCode) {
    Case 'r':
    Printf ("You select Red.");
    Break;
    Case 'w':
    Printf ("You select White.");
    Break;
    Case 'b':
    Printf ("You select Black.");
    Break;
    Default:
    Printf ("Wrong choose!");
    Break;
}
Return 0;
}
Run the above program, then r, w, b will give the input of any one of the three, see the color. And if we input any other curator, then it will be written Wrong choose !.
If the value of the variable inside the switch case's colorFirsWord, then if the value of colorFirsWord is true, then the program will execute this statement: printf ("You choose Red."); The next Statement is break; This means that we have entered the switch case to do the work, it is finished. Now we can get out of the switch case. The switch case is closed with the break.

Similarly if we input w, then look at the case 'r'. Since we gave the input w, the first case does not match. The next case will look. The next case will come in and match the case 'w', so the next statement will be executed. Printf ("You select White."); And after that break out of the switch case.

In the same way if we give B input, then there will be no match in the above two cases, so the statemates of those cases will not be executed. Only the 'b' statemates will be executed.

If we input any other curator, then the default value for that. Wrong selection will be written!

We can write any expression here in switch (colorCode). We will see such an example. Again, we can write more than one Statement. Let's see his example a little later.

If we input R, W or B in the upper part of the program, then Wrong choose !. Now we will modify the above program, so if we put color or code in small or large hands, we can give the correct output in both cases.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
Int main ()
{
Char colorCode;
Printf ("Enter first word of Red, White or Black: \ n");
Scanf ("% c", & colorCode);
Switch (colorCode) {
    Case 'r':
    Case 'R':
    Printf ("You select Red.");
    Break;
    Case 'w':
    Case 'W':
    Printf ("You select White.");
    Break;
    Case 'b':
    Case 'B':
    Printf ("You select Black.");
    Break;
    Default:
    Printf ("Wrong choose!");
    Break;
}
Return 0;
}
Here we will notice that we have used the case twice without using break. Such as

Case 'r':
Case 'R':

If you write more than one case without break, then they work like OR operation. If it is true then those expressions will be executed. And wait until break is found.

Any case inside the switch case can be run, loop running, call function and everything else. First we will write a program, where we will use the for loop once inside the switch case, once using the while loop. It means that we can run any code if we wish to switch between case
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
Int main ()
{
Int code;
Int i = 0;
Printf ("Enter 1 to print even integers or enter 2 to print odd integers:");
Scanf ("% d", & code);
Switch {code} {
    Case 1:
        While (i <= 100) {
            Printf ("% d \ n", i);
            I = i + 2;
        }
    Break;
    Case 2:
         For (i = 1; i <= 100; i = i + 2)
                Printf ("% d \ n", i);
    Break;
    Default:
    Printf ("Wrong choose!");
    Break;
}
Return 0;
}
The program will ask us to give 1 or 2 inputs. If you give 1 input, print the odd number from 1 to 100. If you input 2 input, then print the number evenly from 1 to 100.

If we want, we can call functions and calls from within the switch case. See the following program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
Int main ()
{
    Void playGame () {
       Printf ("You select to Play the game.");
        }
    Void closeGame () {
       Printf ("You select to Close the game.");
        }
Int code;
Printf ("Enter 1 to play the Game. \ NEnter 2 to close the Game:");
Scanf ("% d", & code);
Switch {code} {
    Case 1:
        PlayGame ();
        Break;
    Case 2:
        CloseGame ();
        Break;
    Default:
    Printf ("Wrong choose!");
    Break;
}
Return 0;
}
We've created two functions, playGame () and closeGame (). If the user has 1 input, the playGame () function will be called, and 2 will give the call closeGame () function ... In this way we can use the switch case in our program just like our own creativity.



No comments

Theme images by sebastian-julian. Powered by Blogger.