310 likes | 337 Views
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.
E N D
CS 240: Data Structures Tuesday, June 3rd 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
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!
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
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
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
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
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
Multiple conditions • Be careful when testing multiple conditions: • If we wanted a program to describe the temperature:
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,-]
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.
switch • Use switch when you have a bunch of equality decisions to make: • switch replaces a set of if statements.
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
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”.
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;”
while • You use a while for the following: • Repeat code until some condition is met • Repeat code forever – need control flow to exit
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--; }
int conditionvariable = initialvalue; while(condition) { //stuff change(conditionvariable); } for(int conditionvariable=initialvalue;condition;change(conditionvariable)) { //stuff } for vs while
for • “For” is generally used to repeat code a certain number of times (infinite is possible), or iterate over some data.
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]); }
Control Flow Statements • continue; • Ends the current loop. • break; • Terminate the current loop. • return X; • Terminate the current function, return X to the caller.
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:
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!
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.
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!
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.
int main() { int a; { int b; { int c; { int d; { int e; } int f; { int g; } } } int h; { int I; } } }
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?
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
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