Your SlideShare is downloading. ×
0
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Switch case and looping
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Switch case and looping

276

Published on

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

  • Be the first to like this

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  1. http://eglobiotraining.com
  2.  A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely. http://eglobiotraining.com
  3.  A programming language is a notation for writing programs, which are specifications of a computation or algorithm. http://eglobiotraining.com
  4.  A computer programming language is a language used to write computer programs, which involve a computer performing some kind of computation or algorithm and possibly control external devices such as printers, disk drives, robots, and so on. http://eglobiotraining.com
  5.  Programming languages usually contain abstractions for defining and manipulating data structures or controlling theflow of execution. http://eglobiotraining.com
  6.  As a student, I have learned that programming is difficult to understand because it has so many scripts and applications that can be used to run a program. http://eglobiotraining.com
  7.  First, I find programming very hard and confusing because it has lots of codes that you need to understand for you to run a program. http://eglobiotraining.com
  8.  Programming is a creative process done by programmers to instruct a computer on how to do a task. Programming languages let you use them in different ways, e.g. adding numbers, etc… or storing data on disk for later retrieval. http://eglobiotraining.com
  9.  You need to consider languages to run your own program. DEV C++ is the most language that we use in programming. C++ is one of the most used programming languages in the world. Also known as "C with Classes". http://eglobiotraining.com
  10.  In programming, a switch, case, select or inspect statement i s a type of selection control mechanism that exists in most imperative programminglanguages such as Pascal, Ada, C/C++, C#, Java, and so on Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "goto", one of several labels). http://eglobiotraining.com
  11. 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 following any of thecases break;} The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions. http://eglobiotraining.com
  12. The default case is optional, but it is wise to include it as it handles any unexpected cases. Switchstatements serves as a simple way to write long if statements when the requirements are met.Often it can be used to process input from a user.Above is a sample program, in which not all of the proper functions are actually declared, butwhich shows how one would use switch in a program. http://eglobiotraining.com
  13.  This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (albeit simple) for processing input. If you do not understand this then try mentally putting in if statements for the case statements. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. You could easily make a few small functions if you wish to test the code. http://eglobiotraining.com
  14.  LOOP is a pedagogical programming language designed by Uwe Schöning, along with GOTO and WHILE. The only operations supported in the language are assignment, addition and looping. http://eglobiotraining.com
  15.  A loop lets you write a very simple statement to produce a significantly greater result simply by repetition. Before going further, you should understand the concept of C++s true and false, because it will be necessary when working with loops (the conditions are the same as with if statements). http://eglobiotraining.com
  16.  For While and Do http://eglobiotraining.com
  17. For ( variable initialization; condition; variable update ) { Code to execute while the condition is true} http://eglobiotraining.com
  18.  Here statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop. http://eglobiotraining.com
  19.  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. http://eglobiotraining.com
  21.  The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. Having the test condition at the end, guarantees that the body of the loop always executes at least one time. http://eglobiotraining.com
  22. do { block of code; } while (test condition); http://eglobiotraining.com
  23.  Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says "Loop while the condition is true, and execute this block of code", a do..while loop says "Execute this block of code, and loop while the condition is true". http://eglobiotraining.com
  24. http://eglobiotraining.com
  25.  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
  26. http://eglobiotraining.com
  27. #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
  28. #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
  29. 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
  30. 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
  31. #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. // consequently, when x equals 10 the loop breaks. // x is updated before the condition is checked. cout<< x <<endl; } cin.get();} http://eglobiotraining.com
  32. #include <iostream>using namespace std;int main (){ int score; cout << "What was your score?"; cin >> score; if (score <= 30) { cout << "nOuch, less than 30...!"; } http://eglobiotraining.com
  33. else if (score <= 50) { cout << "nYou score aint great mate.."; } else if (score <= 80) { cout << "nYour pretty good, well done man!"; } else if (score <= 100) { cout << "nYou got to the top!!!"; } http://eglobiotraining.com
  34. else { cout << "nYou cant score higher than 100!!! Cheater!!!!"; } cin.ignore(); cin.get(); return 0;} http://eglobiotraining.com
  35. #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
  36. #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
  37. http://eglobiotraining.com
  38. http://eglobiotraining.com
  39. http://eglobiotraining.com
  40. http://eglobiotraining.com
  41. http://eglobiotraining.com
  42. http://eglobiotraining.com
  43. http://eglobiotraining.com
  44. http://eglobiotraining.com
  45. http://eglobiotraining.com
  46.  Submitted to: Professor Erwin Globio Submitted by: Charlaine J. AstillasBM 10203 http://eglobiotraining.com

×