Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1 -- 1Computer Science I - Martin Hardwick Odds And Ends – Switch Statement rIt is often necessary in programs to set up a set of cases, where.

Similar presentations


Presentation on theme: "Lecture 1 -- 1Computer Science I - Martin Hardwick Odds And Ends – Switch Statement rIt is often necessary in programs to set up a set of cases, where."— Presentation transcript:

1 Lecture 1 -- 1Computer Science I - Martin Hardwick Odds And Ends – Switch Statement rIt is often necessary in programs to set up a set of cases, where at most one of the cases is executed. l we have done this with nested IF-ELSE statements rThere is a special statement in C++ that makes is easier to set up cases in some situations. l it is called the switch statement 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; } if (score < 60) { F = F + 1; } else if (score < 70) { D = D + 1; } else if (score < 80) { C = C + 1; }... Example Counting letter grades for exam scores.

2 Lecture 1 -- 2Computer Science I - Martin Hardwick Switch Statement rSyntax: switch ( ) { case : case :... statement1; statement 2;... break; case : case :... statement10; statement20;... break;... default: statement100; statement200;... } Meaning: == ||... == ||......... yes no statement 1 statement 2... statement 10 statement 20... statement 100 statement 200...

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

4 Lecture 1 -- 4Computer Science I - Martin Hardwick Putting it all together rThis program uses everything l A while loop to get data until the user is finished l A for loop to iterate over the array until the end l An if to decide what to do with each value l A break to handle an exception rNow we need to start thinking about writing programs that contain many functions l When main gets too complicated, debugging gets very hard. l Smaller functions are easier to reuse. l The best programmers are always reusing code #include 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; }

5 Lecture 1 -- 5Computer Science I - Martin Hardwick 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 SOUND cow moo pig oink cow moo

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

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

8 Lecture 1 -- 8Computer Science I - Martin Hardwick New Version Of Old MacDonald (continued) 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; } rThe syntax for a parameter declaration in a function is: type name rThe parameter name can be any valid identifier. rAny place where the parameter name appears in the function, the value of the parameter is substituted.

9 Lecture 1 -- 9Computer Science I - Martin Hardwick Old MacDonald (continued) 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; } rWhen a function with parameters is called, the call must specify a value (argument) for each parameter. l place the arguments between the parentheses in the function call l if a function requires no parameters, then nothing is placed between the parentheses in the function call rIf 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.

10 Lecture 1 -- 10Computer Science I - Martin Hardwick Functions With Multiple Parameters rRather than the main function calling all the other functions, we can create a function Verse to do this. l displays one complete verse of the song l requires two parameters, the name of an animal and its sound rThe new main function now calls function Verse for each verse of the song to generate l 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; }

11 Lecture 1 -- 11Computer Science I - Martin Hardwick Functions that return a value rA functions that returns a value l Says what type of value it is going to return in its header l Returns that type of value using a return l 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; }


Download ppt "Lecture 1 -- 1Computer Science I - Martin Hardwick Odds And Ends – Switch Statement rIt is often necessary in programs to set up a set of cases, where."

Similar presentations


Ads by Google