Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240: Data Structures Tuesday, June 5 th Programming Semantics, Software Life Cycle.

Similar presentations


Presentation on theme: "CS 240: Data Structures Tuesday, June 5 th Programming Semantics, Software Life Cycle."— Presentation transcript:

1 CS 240: Data Structures Tuesday, June 5 th Programming Semantics, Software Life Cycle

2 Programming Structure To solve a programming problem: To solve a programming problem: Define the problem Define the problem Break it up into manageable parts Break it up into manageable parts Split up the problem into smaller tasks Split up the problem into smaller tasks If a task can be immediately realized, it is small enough If a task can be immediately realized, it is small enough Code the parts Code the parts Integrate parts together Integrate parts together

3 Converting ideas to code: Sometimes, the ideas are easy. Sometimes, the ideas are easy. Getting the code down – or approaching the problem – becomes the challenge. Getting the code down – or approaching the problem – becomes the challenge. Most code revolves around the following statements: Most code revolves around the following statements:

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

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

6 if In the previous two examples, we run additional code if the condition is met. In the previous two examples, we run additional code if the condition is met. Consider this function: Consider this function: If number is 4, what makes this inefficient? 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; }

7 if Possibility #2 Possibility #2 This is better, but we can do more. This is better, but we can do more. At this point, what can number be? At this point, what can number be? Then, we don’t need the second if statement! 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 }

8 if Possibility #2 - Revised Possibility #2 - Revised This also gets rid of the compiler warning. 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. 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 }

9 if Possibility #2 – Revision: Applied to earlier code. Possibility #2 – Revision: Applied to earlier code. This doesn’t work the way we want! This doesn’t work the way we want! What is wrong with this code? What is wrong with this code? Why isn’t it a problem on the previous slide? 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; }

10 If and else If we examine what number can be we know that number is negative if it failed “if(number>=0)” 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. Therefore, code after the if is only reached when we fail the first if. This is because “return 1” stops us from progressing further. This is because “return 1” stops us from progressing further. Alternatively, we can use else. Alternatively, we can use else. The compiler won’t complain about a missing return in this case. 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 }}

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

12 int tempinF; tempinF = gettemperature(); if(tempinF<32){ cout << “It is freezing!” <<endl; } else if((tempinF>=32) && (tempinF =32) && (tempinF <60)){ cout << “It is cold!” <<endl; } else if((tempinF>=60) && (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,-]

13 Get used to having {} for your if and else and other relevant statements. 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 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. 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. This is part of the reason we separate code out into separate files.

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

15 switch 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;} Becomes

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

17 switch 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;} Becomes The cases in a switch isolate the conditions from each other. (unless you forget “break;”

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

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

20 A “while” is a different form of “for” A “while” is a different form of “for”

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

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

23 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]);}

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

25 Using continue: 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; }

26 Using continue: 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++;} This example has problems!

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

28 Return All functions return something (even void functions). All functions return something (even void functions). Return is how we get stuff done! Return is how we get stuff done! A function completes some work and returns to its caller whatever it needs to return. 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! Return ends the current function, no other code in that function will be executed!

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

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

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

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

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

34 Other manipulators Other manipulators = == == && && || || static_cast () static_cast ()


Download ppt "CS 240: Data Structures Tuesday, June 5 th Programming Semantics, Software Life Cycle."

Similar presentations


Ads by Google