1 / 31

CS 240: Data Structures

CS 240: Data Structures. Tuesday, June 3 rd Programming Semantics, Software Life Cycle. Control Structures if else switch while do { } while; for break continue return. Other manipulators & * = == && || static_cast<type>() When do we use these things?. Common Code Structures.

nellieroger
Download Presentation

CS 240: Data Structures

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. CS 240: Data Structures Tuesday, June 3rd Programming Semantics, Software Life Cycle

  2. Control Structures if else switch while do { } while; for break continue return Other manipulators & * = == && || static_cast<type>() When do we use these things? Common Code Structures

  3. if • Use if when you have a simple decision to make: • Is a number negative? if(number < 0) { } • If the user inputs “y”: if(userinput == “y”) { } Remember: Number and userinput must be variables of an appropriate type!

  4. In the previous two examples, we run additional code if the condition is met. Consider this function: If number is 4, what makes this inefficient? bool isPositive(int number) { bool returnvalue = 0; if(number >= 0) { returnvalue = 1; } if(number < 0) { returnvalue = 0; } return returnvalue; } if

  5. Possibility #2 This is better, but we can do more. At this point, what can number be? Then, we don’t need the second if statement! bool isPositive(int number) { if(number >= 0) { return 1; } if(number < 0) { return 0; } //Compiler may complain if no return is at the //end of this function } if

  6. Possibility #2 - Revised This also gets rid of the compiler warning. We can do this because of “control flow”. Let’s try this with the first code we had. bool isPositive(int number) { if(number >= 0) { return 1; //is positive } return 0; //is negative } if

  7. Possibility #2 – Revision: Applied to earlier code. This doesn’t work the way we want! What is wrong with this code? Why isn’t it a problem on the previous slide? bool isPositive(int number) { bool returnvalue = 0; if(number >= 0) { returnvalue = 1; } returnvalue = 0; return returnvalue; } if

  8. If we examine what number can be we know that number is negative if it failed “if(number>=0)” Therefore, code after the if is only reached when we fail the first if. This is because “return 1” stops us from progressing further. Alternatively, we can use else. The compiler won’t complain about a missing return in this case. bool isPositive(int number) { if(number >= 0) { return 1; //is positive } else { return 0; //is negative } } If and else

  9. Multiple conditions • Be careful when testing multiple conditions: • If we wanted a program to describe the temperature:

  10. int tempinF; tempinF = gettemperature(); if(tempinF<32) { cout << “It is freezing!” <<endl; } else if((tempinF>=32) && (tempinF <60)) { cout << “It is cold!” <<endl; } else if((tempinF>=60) && (tempinF < 80)) { cout << “It is cozy!” <<endl; } else if(tempinF>=80) { cout << “It is hot!” <<endl; } //below 32, or [-,32) //32 -> 60, or [32,60) //60 -> 80, or [60,80) //greater than 80, or [80,-]

  11. Get used to having {} for your if and else and other relevant statements. • It makes it easier to read and quicker to edit later. • It will make the code longer; but, you don’t need to worry about that. • This is part of the reason we separate code out into separate files.

  12. switch • Use switch when you have a bunch of equality decisions to make: • switch replaces a set of if statements.

  13. int number; cin >> number; if(number == 5) { cout << “apple” <<endl; } if(number == 7) { cout << “tomato” <<endl; } if(number == 10) { cout << “biscuit” <<endl; } int number; cin >> number; switch(number) { case 5: cout << “apple” <<endl; break; case 7: cout << “tomato” <<endl; break; case 10: cout << “biscuit” <<endl; break; } switch Becomes

  14. switch • Switch isn’t terribly good for a range of values. • If you had an if condition: • if((number > 5) && (number < 10)) • How would you represent this with a switch? • However, switch does have an equivalent statement to “else”.

  15. int number; cin >> number; if(number == 5) { cout << “apple” <<endl; } else if(number == 7) { cout << “tomato” <<endl; } else if(number == 10) { cout << “biscuit” <<endl; } else { cout << “Not food!” <<endl; } int number; cin >> number; switch(number) { case 5: cout << “apple” <<endl; break; case 7: cout << “tomato” <<endl; break; case 10: cout << “biscuit” <<endl; break; default: cout << “Not food!” <<endl; break; } switch Becomes The cases in a switch isolate the conditions from each other. (unless you forget “break;”

  16. while • You use a while for the following: • Repeat code until some condition is met • Repeat code forever – need control flow to exit

  17. Repeat forever: while(1) { int number; cin >> number; if(number == 10) break; } Repeat condition: int counter = 4; while(counter>0) { int number; cin >> number; counter--; }

  18. A “while” is a different form of “for”

  19. int conditionvariable = initialvalue; while(condition) { //stuff change(conditionvariable); } for(int conditionvariable=initialvalue;condition;change(conditionvariable)) { //stuff } for vs while

  20. for • “For” is generally used to repeat code a certain number of times (infinite is possible), or iterate over some data.

  21. for(int i=0;i<10;i++) { //stuff } for(;;) { //stuff } string userinput; cin >> userinput; for(int i=0;i<userinput.size();i++) { stuff(userinput[i]); }

  22. Control Flow Statements • continue; • Ends the current loop. • break; • Terminate the current loop. • return X; • Terminate the current function, return X to the caller.

  23. for(int i=0;i<10;i++) { cout << i << endl; cout << “I printed!” <<endl; } for(int i=0;i<10;i++) { cout << i << endl; continue; cout << “I printed!” <<endl; } Using continue:

  24. int i=0; while(i<10) { cout << i << endl; cout << “I printed!” <<endl; i++; } int i=0; while(i<10) { cout << i << endl; continue; cout << “I printed!” <<endl; i++; } Using continue: This example has problems!

  25. continue, break for(int i=0;i<10;i++) { cout << i << endl; break; cout << “I printed!” <<endl; } • break leaves the loop. • Continue and break are useful, just be careful when you use them.

  26. Return • All functions return something (even void functions). • Return is how we get stuff done! • A function completes some work and returns to its caller whatever it needs to return. • Return ends the current function, no other code in that function will be executed!

  27. Some style issues • Indentation: • Provides readability of the code (required by some languages, namely Python) • Generally, 1 tab for each preceding { without a } • For clarity, you can put { and } alone on their own line • A line with “}” has 1 less tab.

  28. int main() { int a; { int b; { int c; { int d; { int e; } int f; { int g; } } } int h; { int I; } } }

  29. Globals • Globals aren’t all bad, but they are generally bad. • There are some reasons to use globals that we won’t discuss here. • So, what is wrong with using a global variable? • How does it violate information hiding and encapsulation?

  30. double *shorty; double jimmy; jimmy = 10; shorty = &jimmy; cout << shorty << endl; What does this code output? What does: “cout << *shorty <<endl;” output? Pointers and reference

  31. double *shorty; double jimmy; jimmy = 10; shorty = &jimmy; cout << shorty << endl; cout << *shorty << endl; Pointers and reference Name: shorty Value: Uninitialized Location: Topmost! Name: shorty Value: The Bottom Location: Topmost! Output: The Bottom Output: 10 Name: jimmy Value: 10 Location: The Bottom Name: jimmy Value: Uninitialized Location: The bottom

More Related