1 / 11

Odds And Ends – Switch Statement

Odds And Ends – Switch Statement. It is often necessary in programs to set up a set of cases, where at most one of the cases is executed. we have done this with nested IF-ELSE statements There is a special statement in C++ that makes is easier to set up cases in some situations.

alaura
Télécharger la présentation

Odds And Ends – Switch Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Odds And Ends – Switch Statement • It is often necessary in programs to set up a set of cases, where at most one of the cases is executed. • we have done this with nested IF-ELSE statements • There is a special statement in C++ that makes is easier to set up cases in some situations. • it is called the switch statement if (score < 60) { F = F + 1; } else if (score < 70) { D = D + 1; } else if (score < 80) { C = C + 1; } . . . switch (score/10) { case 0: case 1: case 2: case 3: case 4: case 5: F = F + 1; break; case 6: D = D + 1; break; case 7: C = C + 1; break; case 8: B = B + 1; break; case 9: case 10: A = A + 1; } Example Counting letter grades for exam scores. Computer Science I - Martin Hardwick

  2. Switch Statement • Syntax: switch ( <expr> ) { case <a>: case <b>: . . . statement1; statement 2; . . . break; case <x>: case<y>: . . . statement10; statement20; . . . break; . . . default: statement100; statement200; . . . } Meaning: <expr>==<a> || <expr> == <b> || . . . yes statement 1 statement 2 . . . no <expr>==<x> || <expr> == <y> || . . . yes statement 10 statement 20 . . . no . . . statement 100 statement 200 . . . Computer Science I - Martin Hardwick

  3. Break • Break is used to break out of a For, While and Switch • In a Switch if there is no break then the code will continue into the next case • This can be a good thing for some programs • However it is a frequent cause of errors for beginners • Make sure that your Cases end in a break or a return. Computer Science I - Martin Hardwick

  4. Putting it all together #include <iostream> using namespace std; int main() { int grades[100]; int i = 0; double val; cin >> val; while (val != 0) { val >> grades[i++]; cin >> val; } int count = i; double sum; cout << count << " grades" << endl; for (i=0; i<count; i++) { if (grades[i] > 1000) break; sum += grades[i]; } // more code here return 0; } • This program uses everything • A while loop to get data until the user is finished • A for loop to iterate over the array until the end • An if to decide what to do with each value • A break to handle an exception • Now we need to start thinking about writing programs that contain many functions • When main gets too complicated, debugging gets very hard. • Smaller functions are easier to reuse. • The best programmers are always reusing code Computer Science I - Martin Hardwick

  5. oink moo moo oink moo oink moo oink moo oink moo oink moo oink oink moo Parameterization Old MacDonald had a farm, Ee-igh, Ee-igh, oh! And on this farm he had a , Ee-igh, Ee-igh, oh! With a here And a there Here a , there a , Everywhere a . Old MacDonald had a farm, Ee-igh, Ee-igh, oh! ANIMAL pig cow SOUND SOUND SOUND SOUND SOUND SOUND SOUND SOUND cow moo pig oink Computer Science I - Martin Hardwick

  6. HadA ( “cow”); WithA ( “moo”); A function in C++ can be parameterized. When the function is called, values (arguments) for the parameters are supplied as part of the call. Parameters are placed between the parentheses in the function header. The arguments and parameters must be listed in the same order. parameter argument Parameters And C++ Functions HadA ( Animal ) { And on that farm he had a Animal , } WithA ( Sound ) { With a Sound Sound here And a Sound Sound there Here a Sound , there a Sound Everywhere a Sound Sound . } Computer Science I - Martin Hardwick

  7. // Program to generate the words for // Old MacDonald had a farm. #include <iostream> #include <string> using namespace std; void EiEio() { cout << "Ee-igh, Ee-igh, oh!" << endl; } void Refrain() { cout << "Old MacDonald had a farm, "; EiEio(); } Since we will be using string parameters, we need to include <string>. The functions EiEio() and Refrain() don’t need parameters. Note that the parentheses with nothing between them indicate that these functions require no parameters. Old MacDonald Program Computer Science I - Martin Hardwick

  8. void HadA(string ANIMAL) { cout << "And on his farm he had a " << ANIMAL << ", "; EiEio(); } void WithA(string SOUND) { cout << "With a " << SOUND << " " << SOUND << " here" << endl; cout << "And a " << SOUND << " " << SOUND << " there" << endl; cout << "Here a " << SOUND << ", " << "there a " << SOUND << ", " << "everywhere a " << SOUND << " " << SOUND << "." << endl; } The syntax for a parameter declaration in a function is: type name The parameter name can be any valid identifier. Any place where the parameter name appears in the function, the value of the parameter is substituted. New Version Of Old MacDonald (continued) Computer Science I - Martin Hardwick

  9. int main () { // Verse for a cow Refrain(); HadA("cow"); WithA("mow"); Refrain(); cout << endl; // Verse for a pig Refrain(); HadA("pig"); WithA("oink"); Refrain(); cout << endl; // Verse for a duck Refrain(); HadA("duck"); WithA("quack"); Refrain(); cout << endl; return 0; } When a function with parameters is called, the call must specify a value (argument) for each parameter. place the arguments between the parentheses in the function call if a function requires no parameters, then nothing is placed between the parentheses in the function call If a function requires more than one parameter, the order of the arguments in the call must match the order of the parameters in the function header. Old MacDonald (continued) Computer Science I - Martin Hardwick

  10. Rather than the main function calling all the other functions, we can create a function Verse to do this. displays one complete verse of the song requires two parameters, the name of an animal and its sound The new main function now calls function Verse for each verse of the song to generate it passes both the animal name and sound for the verse as arguments // Display one verse of the song void verse(string name, string sound) { Refrain(); HadA( name ); WithA( sound ); Refrain(); cout << endl; } // Generate the song with three verses int main () { Verse( “cow”, “moo”); Verse( “pig”, “oink”); Verse( “duck”, “quack”); return 0; } Functions With Multiple Parameters Computer Science I - Martin Hardwick

  11. Functions that return a value • A functions that returns a value • Says what type of value it is going to return in its header • Returns that type of value using a “return” • The compiler will complain if the type returned by the “return” is not compatible with the type defined for the function. // Compute a sum int sum (int a, int b) { return a + b; } // Compute two sums int two_sum (int a, int b, int c, int d) { int sum1 = sum (a, b); int sum2 = sum (b, c); return sum (sum1, sum2); } // double version double sum_d (double a, double b) { return a + b; } Computer Science I - Martin Hardwick

More Related