Final requirement in programming

  • Published on
    29-Jun-2015

  • View
    135

  • Download
    2

Transcript

  • 1. http://eglobiotraining.comFINAL REQUIREMENT INPROGRAMMINGDel Rosario, Trisha Maxine A.

2. http://eglobiotraining.com SWITCH CASE AND LOOPING 3. SWITCH CASE STATEMENT In programming,a switch, case, select or inspect statement is a typehttp://eglobiotraining.comof selection control mechanism that exists inmost imperative programming languages suchas Pascal, C/C++, C#, Java, and so on. It is also included in several other types oflanguages. Its purpose is to allow the value of a variable orexpression to control the flow of program executionvia a multiway branch (or "goto", one of severallabels). 4. SWITCH CASE The main reasons for using a switch in programminginclude improving clarity, by reducing otherwiserepetitive coding, and (if the heuristics permit) also http://eglobiotraining.comoffering the potential for faster execution through easiercompiler optimization in many cases. It is a substitute for long if statements that compare avariable to several "integral" values ("integral" values aresimply values that can be expressed as an integer, suchas the value of a char). In computer programming, the value of the variablegiven into switch is compared to the value followingeach of the cases, and when one value matches thevalue of the variable, the computer continues executingthe program from that point. 5. THE BASIC FORMAT FOR USING SWITCH CASEIS OUTLINED BELOW.switch ( ){case this-value: http://eglobiotraining.comCode to execute if == this-valuebreak;case that-value:Code to execute if == that-value break;...default: Code to execute if does not equal the value following any ofthe cases break;} 6. SWITCH CASE In computer programming, the condition of a switch statementis a value. The case says that if it has the value of whatever isafter that case then do whatever follows the colon. The breakis used to break out of the case statements. Break as one ofhttp://eglobiotraining.comthe language used in programming is a keyword that breaksout of the code block, usually surrounded by braces, which itis in. In this case, break prevents the program from falling throughand executing the code in all the other case statements. Animportant thing to note about the switch statement is that thecase values may only be constant integral expressions. It can be useful to put some kind of output to alert you to thecode entering the default case if you dont expect it to. Switchstatements serve as a simple way to write long if statementswhen the requirements of programming are met. Often it canbe used to process input from a user. 7. ACTUAL SOURCE CODE OF SWITCH CASE#include switch ( input ) {case 1:// Note the colon, not a semicolonusing namespace std; playgame();void playgame() break;http://eglobiotraining.com{ case 2:// Note the colon, not a semicolon cout