0
Switch Case andLooping                      http://eglobiotraining.comA final requirement
What is Programming?Programming is instructing a computer to do something for                                             ...
http://eglobiotraining.com         Programming skills are regarded as crucial todevelop a thriving economy (Silicon Valley...
http://eglobiotraining.com        In the beginning , programming is quite confusingbecause you have so much to learn and u...
http://eglobiotraining.com        A programming language is a notation forwriting programs, which are specifications of a ...
Switch Case                                                            http://eglobiotraining.comIn programming,a switch, ...
Basic Formation of Switch Case:switch ( <variable> ) {case this-value:       Code to execute if <variable> == this-value  ...
Typical Syntax  In most languages, a switch statement is defined across                                                   ...
Typical Syntax                                                           http://eglobiotraining.comIn many languages, ever...
Advantages :In some languages and programming environments, the                                                           ...
Simple examples that use switch statement the use examples C#switch (n){case 0:   Console.WriteLine("You typed zero.");   ...
Ruby uses case, when, and else                                                  http://eglobiotraining.com case n when 0  ...
What is Looping?                                                        http://eglobiotraining.comIn computer programming,...
Looping In computer programming a loop structure,                                              http://eglobiotraining.com ...
Three types of looping:The For loop                                                                          http://eglobi...
FOR                                                      http://eglobiotraining.comFor ( variable initialization; conditio...
For Loop• In computer programming for looping structure is a definite  repetition structure that makes use of a counter.• ...
Example:  #include <iostream>  using namespace std; // So the program can see cout and endl  int main()  {     // The loop...
While LoopIn most computer                                http://eglobiotraining.comprogramming languages,a while loop is ...
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 {                                 ...
DO..WHILE• In computer programming the do while loop works same                                                           ...
Example:   #include <iostream>   using namespace std;   int main()   {      int x;                                        ...
CODES AND EXPLANATIONS OF                    http://eglobiotraining.comTHE PROGRAMS HAVE BEENTESTED
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 endlint main(){  // The loop...
LOOPING STATEMENT 6#include <iostream>using namespace std;int main(){  int x;    x = 0;                                   ...
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 ()                                              ht...
SWITCH CASE 2else if (score <= 50) {     cout << "nYou score aint great mate.."; } else if (score <= 75)                  ...
SWITCH CASE 3 else    {        cout << "nYou cant score higher than 100!!! Cheater!!!!";    }                             ...
#include <iostream>using namespace std;int main(){cout << "Enter a number between 1 and 5!" << endl;int number;cin >> numb...
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 OUTPUT                        http://eglobiotraining.comPROGRAM USING DEV C++
http://eglobiotraining.comIn this looping statement, I used “while” looping, and Ichoose to show MDAS just as an example f...
http://eglobiotraining.comI have came up with this by just starting to write thiscode: #include <iostream> and then enter ...
http://eglobiotraining.comI noticed that sometimes if the program does not run, itis because some braces are not included ...
http://eglobiotraining.comWhen I learned that programming is very sensitiveand at the same time very detailed when it come...
http://eglobiotraining.comSo much codes that should be entered that even thespaces are needed programming is very specific...
http://eglobiotraining.comBecause of so many experiences I had before thisprogram run, I found programming is also interes...
http://eglobiotraining.comIn this switch case missing out a break statement causescontrol to fall through to the next case...
http://eglobiotraining.comUnlike if-then and if-then-else statements,the switch statement can have a number of possibleexe...
http://eglobiotraining.comThe switch statement evaluates its expression, thenexecutes all statements that follow thematchi...
http://eglobiotraining.comThe switch statement can include any numberof case instances, but no two case constants within t...
http://eglobiotraining.comSubmitted by :Survelaine S. MurilloBM10203 Submitted to: Prof. Erwin Globio
Upcoming SlideShare
Loading in...5
×

Survelaine murillo ppt

142

Published on

a special presentation about Switch case and Looping statement. :)

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

  • Be the first to like this

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

No notes for slide

Transcript of "Survelaine murillo ppt"

  1. 1. Switch Case andLooping http://eglobiotraining.comA final requirement
  2. 2. What is Programming?Programming is instructing a computer to do something for http://eglobiotraining.comyou with the help of a programming language. The role of aprogramming language can be described 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. is a creative process done by programmers to instruct acomputer on how to do a task. Hollywood has helped instillan image of programmers as uber techies who can sit downat a computer and break any password in seconds or makehighly tuned warp engines improve performance by 500%with just one tweak. Sadly the reality is far lessinteresting!
  3. 3. http://eglobiotraining.com Programming skills are regarded as crucial todevelop a thriving economy (Silicon Valley being the primeproponent of said argument), but on a more fundamentallevel it teaches us skills that underline the contemporarycondition. 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.
  4. 4. http://eglobiotraining.com In the beginning , programming is quite confusingbecause you have so much to learn and understand aboutcodes that will operate to run a program. Computerprogramming (often shortened to programming or coding) isthe process of designing writing, testing, debugging andmaintaining the source code of computer programs• Fundamentally programs manipulate numbers and text. These are the building blocks of all programs. Programming languages let you use them in different ways, eg adding numbers, etc, or storing data on disk for later retrieval.• These numbers and text are called variables and can be handled singly or in structured collections. In C++, a variable can be used to count numbers, or a struct) variable hold payroll details for an employee such as
  5. 5. http://eglobiotraining.com A programming language is a notation forwriting programs, which are specifications of a computationor algorithm . Some, but not all, authors restrict the term"programming language" to those languages that canexpress all possible algorithms C++pronounced "see plus plus") is a statisticallytyped, free form, multi-paradigm, compiled, general-purpose programming languageC++ is one of the most popular programming languages and isimplemented on a wide variety of hardware and operatingsystem platforms.
  6. 6. Switch Case http://eglobiotraining.comIn programming,a switch, case, select or inspect statement is a type ofselection control mechanism that exists inmost imperative programming languages suchas ,Pascal, Ada, C/C++, C#, Java, and so on. The main reasons for using a switch include improvingclarity, by reducing otherwise repetitive coding, and (ifthe heuristics permit) also offering the potential forfaster execution through easier compiler optimization inmany cases.
  7. 7. Basic Formation of Switch Case:switch ( <variable> ) {case this-value: Code to execute if <variable> == this-value break; http://eglobiotraining.comcase that-value: Code to execute if <variable> == that-value break;...default: Code to execute if <variable> does not equal the value following any ofthe cases break;} The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.
  8. 8. Typical Syntax In most languages, a switch statement is defined across http://eglobiotraining.com many individual lines using one or two keywords. A typical syntax is:• The first line contains the basic keyword, usually switch, case or select, followed by an expression which is often referred to as the control expression or control variable of the switch statement.• Subsequent lines define the actual cases (the values) with corresponding sequences of statements that should be executed when a match occurs.
  9. 9. Typical Syntax http://eglobiotraining.comIn many languages, every case must also be preceded bya keyword such as case or when. An optional default caseis typically also allowed, specified bya default or else keyword; this is executed when none ofthe other cases matches the control expression.In languages derived from C, a break keyword is used togo to the end of the switch, thus completing executionof the switch statement. In such languages, programexecution "falls through" to the statements associatedwith the next case in the source text when no break ispresent, thereby behaving like a GOTO mechanism.
  10. 10. Advantages :In some languages and programming environments, the http://eglobiotraining.comuse of a case or switch statement is considered superiorto an equivalent series of if-else statements because itis: ☺ easier to debug (e.g. setting breakpoints on codevs. a call table, if the debugger has no conditionalbreakpoint capability) ☺ easier to read (subjective) ☺ easier to understand and therefore ☺ easier to maintain ☺ faster execution potential
  11. 11. Simple examples that use switch statement the use examples C#switch (n){case 0: Console.WriteLine("You typed zero."); break;case 1:case 4:case 9: Console.WriteLine("n is a perfect square."); break; http://eglobiotraining.comcase 2: Console.WriteLine("n is an even number.");goto case 3;case 3: case 5:case 7: Console.WriteLine("n is a prime number."); break;case 6:case 8: Console.WriteLine("n is an even number."); break;default: Console.WriteLine("Only single-digit numbers are allowed."); break;}
  12. 12. Ruby uses case, when, and else http://eglobiotraining.com case n when 0 puts You typed zero when 1, 9 puts n is a perfect square when 2 puts n is a prime number puts n is an even number when 3, 5, 7 puts n is a prime number when 4, 6, 8 puts n is an even number else puts Only single-digit numbers are allowed„ end
  13. 13. What is Looping? http://eglobiotraining.comIn computer programming, a loop is a sequenceof instructions that is continually repeated until a certaincondition is reached.It is one of the three basic logic structures in computerprogramming. The other two logic structures are selectionand sequence.
  14. 14. Looping In computer programming a loop structure, http://eglobiotraining.com the program asks a question, and theanswer requires an action, it is performed and theoriginal question is asked again until the answer issuch that the action is no longer required.Loops constitute one of the most basic and powerfulprogramming concepts.
  15. 15. Three types of looping:The For loop http://eglobiotraining.com = This is the most common loop type. For loops are executed a fixed number of times, determined by a count. They terminate when the count is exhausted.The Repeat Loop = is used for loops where we do not know in advance how many times we will execute. For example, when we keep asking a user for a value until one is provided, or the user aborts. Here, we are more concerned with the loop termination condition.While loops = are very similar to Repeat loops except that they have the exit condition at the start. This means that we use them when we wish to avoid loop execution altogether if the condition for exit is satisfied at the start.
  16. 16. FOR http://eglobiotraining.comFor ( variable initialization; condition; variable update ) { Code to execute while the condition is true}
  17. 17. For Loop• In computer programming for looping structure is a definite repetition structure that makes use of a counter.• http://eglobiotraining.com • The three expressions in the for• loop have the following role:• –expression 1• is an arithmetic expression that initializes the counter,• –expression 2• is a logical expression that tests the counter against its final value,• –expression 3• is an arithmetic expression that modifies the value of the counter. Careful: there is no
  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 http://eglobiotraining.com // 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.
  19. 19. While LoopIn most computer http://eglobiotraining.comprogramming languages,a while loop is a controlFlow statement that allows codeto be executed repeatedly basedon a given boolean condition.The while loop can be thought ofas a repeating if statement
  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 http://eglobiotraining.com cout<< x <<endl; x++; // Update x so the condition can be met eventually } cin.get(); } In programming 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.
  21. 21. DO..WHILE are useful for things that want to loop at least once.The Structure: do { http://eglobiotraining.com } while ( condition ) ;
  22. 22. DO..WHILE• In computer programming the do while loop works same http://eglobiotraining.com as the while loop and the loop is iterated as long as condition remains true. The do while loop checks the condition at the bottom of the loop while for and while loop checks the condition at the beginning of the loop and as a result the body of the loop is executed at least once. The general form of the do while loop is: -•• do{• statement;• } while(condition);•
  23. 23. Example: #include <iostream> using namespace std; int main() { int x; http://eglobiotraining.com 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.
  24. 24. CODES AND EXPLANATIONS OF http://eglobiotraining.comTHE PROGRAMS HAVE BEENTESTED
  25. 25. LOOPING STATEMENT 1#include <iostream>int main(){ using namespace std; // nSelection must be declared outside do/while loop int nSelection; do { http://eglobiotraining.com 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;}
  26. 26. LOOPING STAEMENT 2#include <iostream>using namespace std; int main(){ int nSelection; double var1, var2; do { cout << "Please make a selection: " << endl; http://eglobiotraining.com 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; }
  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 "; http://eglobiotraining.com 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; }
  28. 28. LOOPING STATEMENT 3#include <iostream>using namespace std; int main(){ int nSelection; double var1, var2; while (1) { do { http://eglobiotraining.com 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; }
  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) { http://eglobiotraining.com 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; } }}
  30. 30. LOOPING STATEMENT 5#include <iostream>using namespace std; // So the program can see cout and endlint 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. http://eglobiotraining.com // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; } cin.get();}
  31. 31. LOOPING STATEMENT 6#include <iostream>using namespace std;int main(){ int x; x = 0; http://eglobiotraining.com do { // "Hello, world!" is printed at least one time // even though the condition is false cout<<"Hello, world!n"; } while ( x != 0 ); cin.get();}
  32. 32. LOOPING STATEMENT 7#include <iostream>using namespace std;int main (){int n;cout << "Enter the starting number > ";cin >> n;while (n>0) { http://eglobiotraining.comcout << n << ", ";--n;}cout << "FIRE!n";return 0;}
  33. 33. SWITCH CASE 1SWITCH CASE#include <iostream>using namespace std;int main () http://eglobiotraining.com{ int score; cout << "What was your score?"; cin >> score; if (score <= 25) { cout << "nOuch, less than 25...!"; }
  34. 34. SWITCH CASE 2else if (score <= 50) { cout << "nYou score aint great mate.."; } else if (score <= 75) http://eglobiotraining.com { cout << "nYour pretty good, wel done man!"; } else if (score <= 100) { cout << "nYou got to the top!!!"; }
  35. 35. SWITCH CASE 3 else { cout << "nYou cant score higher than 100!!! Cheater!!!!"; } http://eglobiotraining.com cin.ignore(); cin.get(); return 0;}
  36. 36. #include <iostream>using namespace std;int main(){cout << "Enter a number between 1 and 5!" << endl;int number;cin >> number;if(number == 1){ http://eglobiotraining.comcout << "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");}
  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"; http://eglobiotraining.comswitch (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;}
  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"); http://eglobiotraining.com 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;}
  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" http://eglobiotraining.com << 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;}
  40. 40. AN OUTPUT http://eglobiotraining.comPROGRAM USING DEV C++
  41. 41. http://eglobiotraining.comIn this looping statement, I used “while” looping, and Ichoose to show MDAS just as an example for the program torun. If logical Expression evaluates to true, thestatement executes. The logical Expression isreevaluated. The body of the loop continues to execute untilthe logical Expression is false
  42. 42. http://eglobiotraining.comI have came up with this by just starting to write thiscode: #include <iostream> and then enter thesucceeding codes, compiled and run.
  43. 43. http://eglobiotraining.comI noticed that sometimes if the program does not run, itis because some braces are not included and Iaccidentally put braces on the same line and it causesthe program not to read its contents. Programming issensitive, when there is missing variable or braces orsome words it does not run.
  44. 44. http://eglobiotraining.comWhen I learned that programming is very sensitiveand at the same time very detailed when it comes toentering codes, I make sure that it is clear meansthat I put everything important codes in it so thatthe program would run.
  45. 45. http://eglobiotraining.comSo much codes that should be entered that even thespaces are needed programming is very specific thatwhatever you have entered in to it you should specifybecause when the statement is false it wouldn‟t let yourun the program, I have experienced it before I arrivedat this result.
  46. 46. http://eglobiotraining.comBecause of so many experiences I had before thisprogram run, I found programming is also interestingfor the more you are practicing to make a program run,the more questions that came up in my mind and trysomething that will fit to this or entering new codes tomake matrix etc… that I know is possible.
  47. 47. http://eglobiotraining.comIn this switch case missing out a break statement causescontrol to fall through to the next case label. Switchescan always be replaced by nested if-else statements, butin some cases this may be more clumsy.Each break statement terminates theenclosing switch statement. Control flow continues withthe first statement
  48. 48. http://eglobiotraining.comUnlike if-then and if-then-else statements,the switch statement can have a number of possibleexecution paths, A switch works with the byte, short, char,and in primitive data types.
  49. 49. http://eglobiotraining.comThe switch statement evaluates its expression, thenexecutes all statements that follow thematching case label.
  50. 50. http://eglobiotraining.comThe switch statement can include any numberof case instances, but no two case constants within thesame switch statement can have the same value.Execution of the statement body begins at the selectedstatement and proceeds until the jump-statement transfers control out of the case body.
  51. 51. http://eglobiotraining.comSubmitted by :Survelaine S. MurilloBM10203 Submitted to: Prof. Erwin Globio
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×