As for switch, it also allows you to execute a set of codes for different condition. Most of the time it is used as an alternative for the if-else if statements. They are deemed to look more orderly and efficient compared to a large chucks of if and else if statements.
However, there are several conditions to be fulfilled when you are using the switch function. Things you will need to take note of will be:
- The type of controlling variables you used must be byte, short, int or char. They are specified in the case statements (You will see this in an example later).
- You may have any number of case statements in a switch. Do note that after each case statement, you will need to end them with a colon.
- All the case values must be of the same types.
- Duplicated case values are not allows.
- Default statement is optional.
- Break statement are used to terminate the instructions at the end case. Whether a break is compulsory would depends on the position of the case you are writing your code for.
Now, let us look at one example before I explain this further.
Alright, the above is an example I wrote to illustrate the switch function. You should notice immediately that, in this example, the type of data I chose to deal with is integer. This is just so that we have a link to the previous tutorial that I did earlier today. Now, remember that for switch function, we can only use it if the types you are dealing with are int, bytes, short and char. If you are dealing with other types such as float, you may not use the switch function. You may still use the if-else if statements for the different conditions you may have in mind. (You may refer to the previous tutorial here if you require to use other data types or if you have missed the previous tutorial. Just a reminder, in this blog, Java Tutorial title will all begin with 1 while python tutorial will all begin with 2. A kind note for those who are lost.)
Let's say I write a program which prints out the comments for particular score and will print out the score we set it to be. I will assign a score to my variable x, in our case, 5 would be the maximum score we can obtain and 1 is the lowest. It may not make much sense, however, it will simplify things and we can understand the function better.
After which, I would like to have different comments for each score that is available. In this case, we will need a different comment for the five different score from 1 to 5. In order to do that, we introduce the switch function. The controlling variable for our switch function in this case would x because for a different integer value of x, we would like to print out a different comment.
Now, we are ready to create the case. This should have the following format: "case (varaible-consistent with your data type) :". In the above example, it is pretty straight forward, we have our case for integer 1. Do note that we will need to end the case statement with a colon at the end, then key in our instruction for that case. Repeat this for all cases you have. The general idea for this example here is if my score is 1, the system would print the string for the case when x value is equal to 1 and print out my score at the end.
So the result for my above program will look like this:
Since I assign the integer 5 to my x value, the program would execute the code I have written for case 5, printing out "You got perfect score of 5/5. Awesome." After that, it will continue to execute the last line of my code, printing out "You score is 5/5." Since the last line of the above code does not depend on any condition and it is written outside the switch function, the line of code will always execute in this program.
Lets say you would like to print out all the comments for all five different score. What can you do?
A for loop will be a good idea because in this case we would like the program to run five times for five different set of score. The for loop would be great for finite number of times you would like to re-run a code.
Here, your code would look some thing like this:
Instead of assigning an integer to the variable, you would like a loop instead. From the above, we have the for loop to start at integer 1, and the limit of the loop would be that x should not exceed 6. This would allow the code to run five times exact. Last, but not least, after each round, we would want to have the variable x to increase by one. This would mean that the code would run the first time at x = 1, then add 1 to x and run the second time at x = 2. Eventually, it will end when x is more than 5 as the loop restrict the code to run above that.
(Had some problems with my keyboard and mouse connection. It is all sorted out now) Alright, the result for the above code would look something like this:
We will have all our five comments printed out nicely for us. Do note that since we no longer need the program to tell us our score in the end, the last line of the code which we have previously was deleted if you have not notice that.
That is pretty much the basic for the switch function. Feel free to try it out in different scenarios and situations you find suitable. If you spot any errors in the post, please highlight them to me so that I can correct them. Or if you have any question regarding the tutorial, you can comment or drop a message, I will reply with the best of my ability.
If I have the time, I would post a slightly more complex program using the switch function. However, that will probably happen when I really have the extra time to spare. Unfortunately, there are no public holidays coming apart from next Monday. After which, we will need to wait till October to enjoy the public holiday and rest. Hence, no promises for this.
It's quite late already. I ought to rest my hands and eyes since I have been fixing and staring at my computer for quite some time. I need some sleep.
If I am free tomorrow, I will continue with my posts, however, I may need to settle some of the problems we are facing tomorrow. Plenty of work to be completed.
Until tomorrow, as usual, have a good rest!
Ciao.
No comments:
Post a Comment