Final requirement


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Final requirement

  1. 1. Dimaculangan, Arjoy Gemel G. FM09205
  2. 2.
  3. 3.  In programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most imperative programming languages such as Pascal, Ada, C/C++, C#, Java, and so on. It is also included in several other types of Programming languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multi way branch (or "go to", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases. NEXT
  4. 4.  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). The basic format for using the switch case in the programming is outlined below. 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.  The switch-case statement is a multi-way decision statement. Unlike the multiple decision statement that can be created using if-else, the switch statement evaluates the conditional expression and tests it against numerous constant values. The branch corresponding to the valueT that the expression matches is taken during execution.OPI BACK NEXTC
  5. 5.  Switch is used to choose a fragment of template depending on the value of an expression  This has a similar function as the If condition - but it is more useful in situations when there is many possible values for the variable. Switch will evaluate one of several statements, depending on the value of a given variable. If no given value matches the variable, the default statement is executed.  The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. Float and double are not allowed.TOPI BACK NEXTC
  6. 6. Switch case 1 It is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program. 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. BACK NEXTC
  7. 7. T BACK NEXTC
  8. 8. This switch statement performs the same statements for more than one case label. This switch statement contains several case clauses and one default clause. Each clause contains a function call and a break statement. The break statements prevent control from passing down through each statement in the switch body. help/v8v101/index.jsp? p8a.doc%2Flanguage%2Fref%2Fssits.htmTOPIC BACK
  9. 9.
  10. 10. Firstly we declare a switch statement followed by Circular Brackets „(‟ and „)‟.The switch-case body starts with „{‟ and ends with „}‟ all the conditions should be placed inside the Curly Brackets only.Then we declare a „case‟ statement which is followed by an integral value and a colon „:‟.After the colon we start the case body under which the specified code is executed if the case condition evaluates to true. The Integral value is compared with the variable (which we added in the switch() statement). Then at the end of our code we declare a „default‟ case/statement followed by a colon , this case is executed if all above conditions evaluate to false. This statement can be considered same as else statement in if- else structure. The default statement is optional.
  11. 11.
  12. 12. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. The switch statementThe switch statement is almost the same as an “if statement”. The switch statement can have many conditions. You start the switch statement with a condition. If one of the variables equals the condition, the instructions are executed. It is also possible to add a default. If none of the variables equals the condition the default will be executed. The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute some instructions. If not true, execute these instructions.
  13. 13.
  14. 14. The switch case statement is a better way of writing a program whena series of if else occurs. The general format for this is, switch ( expression ) { case value1: program statement; program statement; ...... break; case valuen: program statement; ....... break; default: ....... ....... break; }
  15. 15. The keyword break must be included at the end of each casestatement. The default clause is optional, and is executed if the casesare not met. The right brace at the end signifies the end of the caseselections.Rules for switch statements values for case must be integer or character constants theorder of the case statements is unimportant the default clausemay occur first (convention places it last) you cannot use expressions orranges
  16. 16.
  17. 17. There may be a situation when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most ofTO the programming languages.PIC LOOP NEXT
  18. 18. C++ programming language provides following types of loop to handle looping requirements:TOPIC BACK
  19. 19. TOPIC  A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.  The statements in the for loop repeat continuously for a specific number of times. The while and do-while loops repeat until a certain condition is met. The for loop repeats until a specific count is met. Use a for loop when the number of repetition is know, or can be supplied by the user.LOOP BACK NEXT
  20. 20. T O P I C #include <iostream> #include <cmath> } using namespace std; cout <<"nSeconds //prototype falling distancen"; int fallingdistance(); cout <<"--------------------------------------- n"; //main function for ( count = 1; count <= time; int main() count++) { { int count = 1 ; distance = .5 * 9.8 * int time; pow(time, 2.0); double distance ; cout << count << " " << distance <<" meters"<< endl; cout << "Please enter time in 1 through 10 seconds.nn"; } system ("pause"); time = fallingdistance(); return 0; } while ( time < 1 || time > 10) // falling distance function for a return value in seconds transfer to time { cout << "Must enter between 1 int fallingdistance () NEXT and 10 seconds, please re-enter.n"; { time = fallingdistance();LOOP int seconds; BACK cin >> seconds; return seconds;
  22. 22. TOPIC  The while loop allows programs to repeat a statement or series of statements, over and over, as long as a certain test condition is true.  The while loop can be used if you don‟t know how many times a loop must run.  A while loop statement repeatedly executes a target statement as long as a given condition is true.LOOP BACK NEXT
  23. 23. TOPIC #include <iostream.h> int main(void) { int x = 0; int y = 0; bool validNumber = false; while (validNumber == false) { cout << "Please enter an integer between 1 and 10: "; cin >> x; cout << "You entered: " << x << endl << endl; if ((x < 1) || (x > 10)) { cout << "Your value for x is not between 1 and 10!" << endl; cout << "Please re-enter the number!" << endl << endl; } else validNumber = true; } cout << "Thank you for entering a valid number!" << endl; return 0;  }LOOP BACK NEXT
  25. 25. TOPIC  In most computer programming languages, a do while loop, sometimes just called a while loop, is a control flow statement that allows code to be executed once based on a given Boolean condition.  The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed.LOOP BACK NEXT
  26. 26. TOPIC  Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop.  A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.  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.LOOP BACK NEXT
  27. 27. TOPIC  #include <iostream>  using namespace std;  main()  { int num1, num2;  char again = y;  while (again == y || again == Y) {  cout << "Enter a number: ";  cin >> num1;  cout << "Enter another number: ";  cin >> num2;  cout << "Their sum is " << (num1 + num2) << endl;  cout << "Do you want to do this again? ";  cin >> again; }  return 0;  }LOOP BACK NEXT
  29. 29. Loops are used to loop back and execute the same blockof code over and over again until a certain condition ismet.This code takes a value from the user and runs a while loopthat many times. The conditions used for the while loop arethe same as the if-then-else statements, same goes forevery loop. Here since I only put "a" the program will read"While a is true execute this block" and as long as a is apositive integer it is considered to be true.
  30. 30.
  31. 31. Accidentally putting a ; at the end of a for loop or if statement so that thestatement has no effect - For example:for (x=1; x<10; x++); print f("%dn",x);only prints out one value because the semicolon after the for statementacts as the one line the for loop executes.You can see that the declaration for a has been changed to a float, andthe %f symbol replaces the %d symbol in the print f statement. In addition,the %f symbol has some formatting applied to it: The value will be printedwith six digits preceding the decimal point and two digits following thedecimal point.Now lets say that we wanted to modify the program so that thetemperature 98.6 is inserted in the table at the proper position. That is, wewant the table to increment every 10 degrees, but we also want the tableto include an extra line for 98.6 degrees F because that is the normal bodytemperature for a human being. The following program accomplishes thegoal:
  32. 32. #include <stdio.h>int main(){ float a; a = 0; while (a <= 100) { if (a > 98.6) { printf("%6.2f degrees F = %6.2f degrees Cn", 98.6, (98.6 - 32.0) * 5.0 / 9.0); } printf("%6.2f degrees F = %6.2f degrees Cn", a, (a - 32.0) * 5.0 / 9.0); a = a + 10; } return 0;}This program works if the ending value is 100, but if you change the endingvalue to 200 you will find that the program has a bug. It prints the line for98.6 degrees too many times. We can fix that problem in several differentways.
  33. 33.
  34. 34. As these cases demonstrate, often the structure of what your program isdoing can usually be expressed without using gotos. Undisciplined use ofgotos can create unreadable, un maintainable code when more idiomaticalternatives (such as if-elses, or for loops) can better express your structure.Theoretically, the goto construct does not ever have to be used, but thereare cases when it can increase readability, avoid code duplication, or makecontrol variables unnecessary. You should consider first mastering theidiomatic solutions, and use goto only when necessary. Keep in mind thatmany, if not most, C style guidelines strictly forbid use of goto, with the onlycommon exceptions being the following examples.One use of goto is to break out of a deeply nested loop. Since break will notwork (it can only escape one loop), goto can be used to jump completelyoutside the loop. Breaking outside of deeply nested loops without the use ofthe goto is always possible, but often involves the creation and testing ofextra variables that may make the resulting code far less readable than itwould be with goto. The use of goto makes it easy to undo actions in anorderly fashion, typically to avoid failing to free memory that had beenallocated.Another accepted use is the creation of a state machine. This is a fairlyadvanced topic though, and not commonly needed.
  35. 35.
  36. 36. The break command allows you to terminate and exit a loop (that is, do, for,and while). You can place a break command only in the body of a loopingcommand or in the body of a switch command. The break keyword must belowercase and cannot be abbreviated.In a looping statement, the break command ends the loop and movescontrol to the next command outside the loop. Within nested statements,the break command ends only the smallest enclosing do, for, switch, or whilecommands.In a switch body, the break command ends the execution ofthe switch body and transfers control to the next command outsidethe switch body. Output will be displayed as: 0 1 2 3 4 5 6 7 8 9
  37. 37.
  38. 38. Try changing the Fahrenheit-to-Celsius program so that ituses scan f to accept the starting, ending and incrementvalue for the table from the user. Add a heading line to thetable that is produced. Try to find a different solution to thebug fixed by the previous example. Create a table thatconverts pounds to kilograms or miles to kilometres.If you run this program, it will produce a table of valuesstarting at 0 degrees F and ending at 100 degrees F.
  39. 39. The output will look like this: 0 degrees F = -17 degrees C 10 degrees F = -12 degrees C 20 degrees F = -6 degrees C 30 degrees F = -1 degrees C 40 degrees F = 4 degrees C 50 degrees F = 10 degrees C 60 degrees F = 15 degrees C 70 degrees F = 21 degrees C 80 degrees F = 26 degrees C 90 degrees F = 32 degrees C 100 degrees F = 37 degrees CThe tables values are in increments of 10 degrees. You can see thatyou can easily change the starting, ending or increment values ofthe table that the program produces.
  40. 40.
  41. 41.
  42. 42.