![]() So here’s the complete code for the previous example where you are allowed to input the grade while running the program using the Scannerinput. Try deleting the default case.īut if you give a wrong input other than the acceptable ones, your program is not capable of identifying that error. ![]() Therefore, all you need to do is to jump out of the switch statement and that is done by break.īut what happens if you just didn’t write it? Then, after the execution of case “B” statements, the program will consider the next case “ C” as well.įinal note: default is not always compulsory to write. It is used to get out of the switch statement block.įor example, if you got a grade “ B”, then once the statements under case “ C” finished the execution, there is no need to move to the next case “ C” and waste your time. Likewise, for case “ B”, “ C” and “ F” same process is applied and if the value in grade is none of the cases “ A”, “ B”, “ C” or “ F”, then the statements under default are executed. If the grade is not the case “ A”, then the statements below it, are skipped and computer moves to next case. If the grade has the String value “ A” in it, then the statements under case “ A” are executed. So we consider each case of this grade variable. The expression within brackets of the switch statements is the variable grade. How do we write a program so that if some person enters his or her grade into it, then these info is given out?įor this case, switch statements are the programmer’s choice □ Let’s see how do we write it… If you have got “ F”, then you can’t enroll into the course.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |