Switch Caseprogramming A final requirement for                         and Looping                                       h...
Programming        We first define the word “programming”, it is acomputer language programmers use to developapplications...
As an individual, I have learned that programming is avery broad because it composes many scripts, applicationsand can be ...
At first, programming is confusing becauseyou have so much to understand about codes thatwill enable to run a program. Pro...
You have to consider languages to run or writeyour own program, most demanded language inprogramming is the DEV C++ (a ful...
Switch Case     Switch case statements are a substitute for long if statements that compare a variable to several "integra...
basic format for using switchcase: switch ( <variable> ) {case this-value:       Code to execute if <variable> == this-val...
The condition of a switchstatement is a value. The case saysthat if it has the value of whatever isafter that case then do...
“Break” is a keyword that breaksout of the code block, usuallysurrounded by braces, which it is in. Inthis case, break pre...
The default case is optional, but it iswise to include it as it handles anyunexpected cases. Switch statementsserves as a ...
This shows how would you use a Switch in a Program #include <iostream> using namespace std; void playgame() {    cout << "...
That program will compile, but cannot be rununtil the undefined functions are given bodies, butit serves as a model (albei...
Looping      Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code ...
(They may be executing a smallnumber of tasks, but in principle, toproduce a list of messages onlyrequires repeating the o...
One Caveat: before going further,you should understand the concept ofC++s true and false, because it will benecessary when...
FORFor ( variable initialization; condition; variable update ) {   Code to execute while the condition is true}           ...
The variable initialization allows you to either declare avariable and give it a value or give a value to an alreadyexisti...
Example:  #include <iostream>  using namespace std; // So the program can see cout and endl  int main()  {     // The loop...
WHILEThe basic structure:     While ( condition ) { Code to execute while the     condition is true } The true represents ...
Example:  #include <iostream>  using namespace std; // So we can see cout and endl  int main()  {     int x = 0; // Dont f...
DO..WHILE    are useful for things that want to loop at least once.The Structure:     do {     } while ( condition ) ;    ...
Notice that the condition is tested at theend of the block instead of the beginning, sothe block will be executed at least...
Example:   #include <iostream>   using namespace std;   int main()   {      int x;       x = 0;       do {          // "He...
Codes and Explanations of thePrograms have been tested                     http://eglobiotraining.com
LOOPING STATEMENT 1#include <iostream>int main(){   using namespace std;    // nSelection must be declared outside do/whil...
LOOPING STAEMENT 2#include <iostream>using namespace std; int main(){   int nSelection;   double var1, var2;  do  {    cou...
LOOPING STATEMENT 3 if (nSelection == 2)       {         cout << "Please enter the first whole number ";         cin >> va...
LOOPING STATEMENT 3#include <iostream>using namespace std; int main(){   int nSelection;   double var1, var2;   while (1) ...
LOOPING STATEMENT 4 else if (nSelection == 2)     {         cout << "Please enter the first whole number ";         cin >>...
LOOPING STATEMENT 5 #include <iostream> using namespace std; // So the program can see cout and endl int main() {   // The...
LOOPING STATEMENT 6#include <iostream>using namespace std;int main(){  int x;    x = 0;    do {      // "Hello, world!" is...
LOOPING STATEMENT 7#include <iostream>using namespace std;int main (){int n;cout << "Enter the starting number > ";cin >> ...
SWITCH CASE 1SWITCH CASE#include <iostream>using namespace std;int main (){    int score;    cout << "What was your score?...
SWITCH CASE 2else if (score <= 50) {     cout << "nYou score aint great mate.."; } else if (score <= 75) {     cout << "nY...
SWITCH CASE 3 else    {        cout << "nYou cant score higher than 100!!! Cheater!!!!";    }    cin.ignore();    cin.get(...
SWITCH CASE 4#include <iostream>using namespace std;int main(){cout << "Enter a number between 1 and 5!" << endl;int numbe...
SWITCH CASE 5#include <iostream>using namespace std;int main(){int price_before_discount, RM, dozen, total_price;cout<< "H...
SWITCH CASE 6#include <stdlib.h>#include <stdio.h>int main(void) {  int n;  printf("Please enter a number: ");  scanf("%d"...
SWITCH CASE 7#include <iostream>using namespace std;int main(void){  char grade;  cout << "Enter your grade: ";  cin >> gr...
An outputprogram using Dev C++                   http://eglobiotraining.com
In this looping statement, I used “while” looping, and I choose toshow MDAS just as an example for the program to run. If ...
I have came up with this by just starting to write this code:#include <iostream> and then enter the succeedingcodes, compi...
I noticed that sometimes if the program does not run, it isbecause some braces are not included and I accidentallyput brac...
When I learned that programming is very sensitive and atthe same time very detailed when it comes to enteringcodes, I make...
So much codes that should be entered that even the spacesare needed programming is very specific that whatever youhave ent...
Because of so many experiences I had before this programrun, I found programming is also interesting for the more youare p...
In this switch case missing out a break statement causescontrol to fall through to the next case label. Switches canalways...
Unlike if-then and if-then-else statements, the switch statementcan have a number of possible execution paths, A switch wo...
The switch statement evaluates its expression, then executesall statements that follow the matching case label.           ...
The switch statement can include any numberof case instances, but no two case constants within thesame switch statement ca...
Submitted to:Prof. ErwinGlobiohttp://eglobiotraining.com              Submitted by:             Guevarra, Katrina B.      ...
Upcoming SlideShare
Loading in...5
×

My final requirement

218
-1

Published on

:D

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
218
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

My final requirement

  1. 1. Switch Caseprogramming A final requirement for and Looping http://eglobiotraining.com
  2. 2. Programming We first define the word “programming”, it is acomputer language programmers use to developapplications, scripts, or other set of instructions for acomputer to execute. programming is instructing a computer to dosomething for you with the help of a programminglanguage. The role of a programming language can bedescribed in two ways:  Technical: It is a means for instructing a Computer to perform Tasks  Conceptual: It is a framework within which we organize our ideas about things and processes. http://eglobiotraining.com
  3. 3. As an individual, I have learned that programming is avery broad because it composes many scripts, applicationsand can be used to run a program that has been part of theprogramming language. A programming language should both provide meansto describe primitive data and procedures and means tocombine and abstract those into more complex ones. The distinction between data and procedures is notthat clear cut. In many programming languages, procedurescan be passed as data (to be applied to ``real data) andsometimes processed like ``ordinary data. Conversely``ordinary data can be turned into procedures by anevaluation mechanism. http://eglobiotraining.com
  4. 4. At first, programming is confusing becauseyou have so much to understand about codes thatwill enable to run a program. Programming hasapplications and program development, the bestexample for this is the Internet bowser… Programming is a creative process done byprogrammers to instruct a computer on how to do atask. Fundamentally programs manipulate numbersand text. These are the building blocks of allprograms. Programming languages let you use themin different ways, e.g adding numbers, etc… orstoring data on disk for later retrieval. http://eglobiotraining.com
  5. 5. You have to consider languages to run or writeyour own program, most demanded language inprogramming is the DEV C++ (a full-featured IntegratedDevelopment Environment (IDE)). C++ is one of the most used programminglanguages in the world. Also known as "C with Classes".New to programming or thinking about it? It mightsurprise you to know that there are many programmerswho program just for fun and it can lead to a job. http://eglobiotraining.com
  6. 6. Switch Case Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). http://eglobiotraining.com
  7. 7. basic format for using switchcase: switch ( <variable> ) {case this-value: Code to execute if <variable> == this-value break;case that-value: Code to execute if <variable> == that-value break;...default: Code to execute if <variable> does not equal the value followingany of the cases break;}The value of the variable given into switch is compared to the valuefollowing each of the cases, and when one value matches the valueof the variable, the computer continues executing the program fromthat point. http://eglobiotraining.com
  8. 8. The condition of a switchstatement is a value. The case saysthat if it has the value of whatever isafter that case then do whateverfollows the colon. The break is usedto break out of the casestatements. An important thing tonote about the switch statement is http://eglobiotraining.comthat the case values may only be
  9. 9. “Break” is a keyword that breaksout of the code block, usuallysurrounded by braces, which it is in. Inthis case, break prevents the programfrom falling through and executing thecode in all the other case statements. http://eglobiotraining.com
  10. 10. The default case is optional, but it iswise to include it as it handles anyunexpected cases. Switch statementsserves as a simple way to write long ifstatements when the requirements aremet. Often it can be used to processinput from a user. http://eglobiotraining.com
  11. 11. This shows how would you use a Switch in a Program #include <iostream> using namespace std; void playgame() { cout << "Play game called"; } void loadgame() } cout << "Load game called"; void playmultiplayer() { cout << "Play multiplayer game called"; } int main() { int input; cout<<"1. Play gamen"; cout<<"2. Load gamen"; cout<<"3. Play multiplayern"; cout<<"4. Exitn"; cout<<"Selection: "; cin>> input; switch ( input ) { case 1: // Note the colon, not a semicolon playgame(); break; case 2: // Note the colon, not a semicolon loadgame(); break; case 3: // Note the colon, not a semicolon playmultiplayer(); break; case 4: // Note the colon, not a semicolon cout<<"Thank you for playing!n"; break; default: // Note the colon, not a semicolon cout<<"Error, bad input, quittingn"; http://eglobiotraining.com break; } cin.get(); }
  12. 12. That program will compile, but cannot be rununtil the undefined functions are given bodies, butit serves as a model (albeit simple) for processinginput. If you do not understand this then trymentally putting in if statements for the casestatements. Default simply skips out of the switchcase construction and allows the program toterminate naturally. If you do not like that, thenyou can make a loop around the whole thing tohave it wait for valid input. You could easily makea few small functions if you wish to test the code. http://eglobiotraining.com
  13. 13. Looping Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. http://eglobiotraining.com
  14. 14. (They may be executing a smallnumber of tasks, but in principle, toproduce a list of messages onlyrequires repeating the operation ofreading in some data and displayingit.) Now, think about what this means:a loop lets you write a very simplestatement to produce a significantlygreater result simply by repetition. http://eglobiotraining.com
  15. 15. One Caveat: before going further,you should understand the concept ofC++s true and false, because it will benecessary when working with loops (theconditions are the same as with ifstatements). Three types of Loops: for, while, and do.. http://eglobiotraining.com
  16. 16. FORFor ( variable initialization; condition; variable update ) { Code to execute while the condition is true} http://eglobiotraining.com
  17. 17. The variable initialization allows you to either declare avariable and give it a value or give a value to an alreadyexisting variable. Second, the condition tells the program thatwhile the conditional expression is true the loop shouldcontinue to repeat itself. The variable update section is theeasiest way for a for loop to handle changing of the variable.It is possible to do things like x++, x = x + 10, or even x = random( 5 ), and if you really wanted to, you could call otherfunctions that do nothing to the variable but still have a usefuleffect on the code. Notice that a semicolon separates each of thesesections, that is important. Also note that every single one ofthe sections may be empty, though the semicolons still haveto be there. If the condition is empty, it is evaluated as trueand the loop will repeat until something else stops it. http://eglobiotraining.com
  18. 18. Example: #include <iostream> using namespace std; // So the program can see cout and endl int main() { // The loop goes while x < 10, and x increases by one every loop for ( int x = 0; x < 10; x++ ) { // Keep in mind that the loop condition checks // the conditional statement before it loops again. // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; } cin.get(); } This program is a very simple example of a for loop. x is set to zero, while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until the condition is met. Keep in mind also that the variable is incremented after the code in the loop is run for the first time. http://eglobiotraining.com
  19. 19. WHILEThe basic structure: While ( condition ) { Code to execute while the condition is true } The true represents a boolean expression which could be x == 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5 || v == 7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop. http://eglobiotraining.com
  20. 20. Example: #include <iostream> using namespace std; // So we can see cout and endl int main() { int x = 0; // Dont forget to declare variables while ( x < 10 ) { // While x is less than 10 cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get(); } The easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block. http://eglobiotraining.com
  21. 21. DO..WHILE are useful for things that want to loop at least once.The Structure: do { } while ( condition ) ; http://eglobiotraining.com
  22. 22. Notice that the condition is tested at theend of the block instead of the beginning, sothe block will be executed at least once. Ifthe condition is true, we jump back to thebeginning of the block and execute it again.A do..while loop is basically a reversed whileloop. A while loop says "Loop while thecondition is true, and execute this block ofcode", a do..while loop says "Execute thisblock of code, and loop while the condition istrue". http://eglobiotraining.com
  23. 23. Example: #include <iostream> using namespace std; int main() { int x; x = 0; do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!n"; } while ( x != 0 ); cin.get(); } Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition. http://eglobiotraining.com
  24. 24. Codes and Explanations of thePrograms have been tested http://eglobiotraining.com
  25. 25. LOOPING STATEMENT 1#include <iostream>int main(){ using namespace std; // nSelection must be declared outside do/while loop int nSelection; do { cout << "Please make a selection: " << endl; cout << "1) Addition" << endl; cout << "2) Subtraction" << endl; cout << "3) Multiplication" << endl; cout << "4) Division" << endl; cin >> nSelection; } while (nSelection != 1 && nSelection != 2 && nSelection != 3 && nSelection != 4); // do something with nSelection here // such as a switch statement return 0;} http://eglobiotraining.com
  26. 26. LOOPING STAEMENT 2#include <iostream>using namespace std; int main(){ int nSelection; double var1, var2; do { cout << "Please make a selection: " << endl; cout << "1) Addition" << endl; cout << "2) Subtraction" << endl; cout << "3) Multiplication" << endl; cout << "4) Division" << endl; cin >> nSelection; } while (nSelection != 1 && nSelection != 2 && nSelection != 3 && nSelection != 4); if (nSelection == 1) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1+var2) << endl; } http://eglobiotraining.com
  27. 27. LOOPING STATEMENT 3 if (nSelection == 2) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1-var2) << endl; } if (nSelection == 3) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1*var2) << endl; } if (nSelection == 4) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1/var2) << endl; } return 0; } http://eglobiotraining.com
  28. 28. LOOPING STATEMENT 3#include <iostream>using namespace std; int main(){ int nSelection; double var1, var2; while (1) { do { cout << "Please make a selection: " << endl; cout << "1) Addition" << endl; cout << "2) Subtraction" << endl; cout << "3) Multiplication" << endl; cout << "4) Division" << endl; cout << "5) Exit" << endl; cin >> nSelection; } while (nSelection != 1 && nSelection != 2 && nSelection != 3 && nSelection != 4 && nSelection != 5); if (nSelection == 1) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1+var2) << endl; } http://eglobiotraining.com
  29. 29. LOOPING STATEMENT 4 else if (nSelection == 2) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1-var2) << endl; } else if (nSelection == 3) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1*var2) << endl; } else if (nSelection == 4) { cout << "Please enter the first whole number "; cin >> var1; cout << "Please enter the second whole number "; cin >> var2; cout << "The result is " << (var1/var2) << endl; }else { return 0; } }} http://eglobiotraining.com
  30. 30. LOOPING STATEMENT 5 #include <iostream> using namespace std; // So the program can see cout and endl int main() { // The loop goes while x < 10, and x increases by one every loop for ( int x = 0; x < 10; x++ ) { // Keep in mind that the loop condition checks // the conditional statement before it loops again. // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; } cin.get(); } http://eglobiotraining.com
  31. 31. LOOPING STATEMENT 6#include <iostream>using namespace std;int main(){ int x; x = 0; do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!n"; } while ( x != 0 ); cin.get();} http://eglobiotraining.com
  32. 32. LOOPING STATEMENT 7#include <iostream>using namespace std;int main (){int n;cout << "Enter the starting number > ";cin >> n;while (n>0) {cout << n << ", ";--n;}cout << "FIRE!n";return 0;} http://eglobiotraining.com
  33. 33. SWITCH CASE 1SWITCH CASE#include <iostream>using namespace std;int main (){ int score; cout << "What was your score?"; cin >> score; if (score <= 25) { cout << "nOuch, less than 25...!"; } http://eglobiotraining.com
  34. 34. SWITCH CASE 2else if (score <= 50) { cout << "nYou score aint great mate.."; } else if (score <= 75) { cout << "nYour pretty good, wel done man!"; } else if (score <= 100) { cout << "nYou got to the top!!!"; } http://eglobiotraining.com
  35. 35. SWITCH CASE 3 else { cout << "nYou cant score higher than 100!!! Cheater!!!!"; } cin.ignore(); cin.get(); return 0;} http://eglobiotraining.com
  36. 36. SWITCH CASE 4#include <iostream>using namespace std;int main(){cout << "Enter a number between 1 and 5!" << endl;int number;cin >> number;if(number == 1){cout << "one";}else if(number == 2){cout << "two";}else if(number == 3){cout << "three";}else if(number == 4){cout << "four";}else if(number == 5){cout << "five";}else{cout << number << " is not between 1 and 5!";}cout << endl;system("pause");} http://eglobiotraining.com
  37. 37. SWITCH CASE 5#include <iostream>using namespace std;int main(){int price_before_discount, RM, dozen, total_price;cout<< "How much is the price before discount for 1 dozen boxes of tissue?n";cout<<"RM ";cin>>price_before_discount;cout<<"nn";cout<< "How many dozen boxes of tissue you buy?n";cin>>dozen;cout<<"nn";switch (dozen){total_price = ((price_before_discount*dozen) * (95/100));case 1: cout<< "Total price is RM ";cout<<RM;cout<<"nn";break;total_price = ((price_before_discount*dozen) * (88/100));case 2: cout<< "Total price is RM ";cout<<RM;cout<<"nn";break;total_price = ((price_before_discount*dozen) * (75/100));case 3: cout<< "Total price is RM ";cout<<RM;cout<<"nn";break;total_price = ((price_before_discount*dozen) * (60/100));case 4 : cout<< "Total price is RM ";cout<<RM;cout<<"nn";break;total_price = ((price_before_discount*dozen) * (40/100));default : cout<< "Total price is RM ";cout<<RM;cout<<"nn";}return 0;} http://eglobiotraining.com
  38. 38. SWITCH CASE 6#include <stdlib.h>#include <stdio.h>int main(void) { int n; printf("Please enter a number: "); scanf("%d", &n); switch (n) { case 1: { printf("n is equal to 1!n"); break; } case 2: { printf("n is equal to 2!n"); break; } case 3: { printf("n is equal to 3!n"); break; } default: { printf("n isnt equal to 1, 2, or 3.n"); break; } } system("PAUSE"); return 0;} http://eglobiotraining.com
  39. 39. SWITCH CASE 7#include <iostream>using namespace std;int main(void){ char grade; cout << "Enter your grade: "; cin >> grade; switch (grade) { case A: cout << "Your average must be between 90 - 100" << endl; break; case B: cout << "Your average must be between 80 - 89" << endl; break; case C: cout << "Your average must be between 70 - 79" << endl; break; case D: cout << "Your average must be between 60 - 69" << endl; break; default: cout << "Your average must be below 60" << endl; } return 0;} http://eglobiotraining.com
  40. 40. An outputprogram using Dev C++ http://eglobiotraining.com
  41. 41. In this looping statement, I used “while” looping, and I choose toshow MDAS just as an example for the program to run. If logicalExpression evaluates to true, the statement executes. The logicalExpression is reevaluated. The body of the loop continues toexecute until the logicalExpression is false http://eglobiotraining.com
  42. 42. I have came up with this by just starting to write this code:#include <iostream> and then enter the succeedingcodes, compiled and run. http://eglobiotraining.com
  43. 43. I noticed that sometimes if the program does not run, it isbecause some braces are not included and I accidentallyput braces on the same line and it causes the program notto read its contents. Programming is sensitive, when there ismissing variable or braces or some words it does not run. http://eglobiotraining.com
  44. 44. When I learned that programming is very sensitive and atthe same time very detailed when it comes to enteringcodes, I make sure that it is clear means that I puteverything important codes in it so that the programwould run. http://eglobiotraining.com
  45. 45. So much codes that should be entered that even the spacesare needed programming is very specific that whatever youhave entered in to it you should specify because when thestatement is false it wouldn’t let you run the program, I haveexperienced it before I arrived at this result.http://eglobiotraining.com
  46. 46. Because of so many experiences I had before this programrun, I found programming is also interesting for the more youare practicing to make a program run, the more questionsthat came up in my mind and try something that will fit tothis or entering new codes to make matrix etc… that I know http://eglobiotraining.comis possible.
  47. 47. In this switch case missing out a break statement causescontrol to fall through to the next case label. Switches canalways be replaced by nested if-else statements, but in somecases this may be more clumsy. Each break statementterminates the enclosing switch statement. Control flow http://eglobiotraining.comcontinues with the first statement
  48. 48. Unlike if-then and if-then-else statements, the switch statementcan have a number of possible execution paths, A switch workswith the byte, short, char, and int primitive data types. http://eglobiotraining.com
  49. 49. The switch statement evaluates its expression, then executesall statements that follow the matching case label. http://eglobiotraining.com
  50. 50. The switch statement can include any numberof case instances, but no two case constants within thesame switch statement can have the same value. Executionof the statement body begins at the selected statement andproceeds until the jump-statement transfers control out of http://eglobiotraining.comthe case body.
  51. 51. Submitted to:Prof. ErwinGlobiohttp://eglobiotraining.com Submitted by: Guevarra, Katrina B. BM10203 http://eglobiotraining.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×