Download presentation
Presentation is loading. Please wait.
1
Introduction to Computer Programming
CS 126 Lecture 7 Zeke Maier
2
Plan for Today Questions Administrivia Conditionals
AD Exercises 4.2 & 4.3 Assignment
3
Questions Boolean backed by 1/0? 3 == true (syntax error) Windows 7…
Recovery.gov Microsoft job cuts
4
Administrivia http://students.cec.wustl.edu/~ejm3/
Lab assignment 1 due this afternoon! Lab assignment 2 assigned today
5
Conditional Statements
Allows us to change the behavior of the program based on certain conditions Example: absolute value If (condition) { //Statements to execute if the condition is true } else { //Statements to execute if the condition is false } How would we write a method to calculate the abs value for us Now let’s shorten it: double abs(double x) { if (x >= 0) return x; return -x; } How about even vs. odd method
6
Conditionals Write a method producing the same output (y values) given the same input (x values) as the function below:
7
Chained Conditionals Useful when there are more than 2 discrete cases?
if (condition) { //Statements to execute if the condition is true } else if() { //Statements } else { }
8
Example if (x > 0) { System.out.println(“1”); } else if( x == 7) {
} else if(false) { System.out.println(“3”); } else if(x<=0 && x > -100) { System.out.println(“4”); } else { System.out.println(“5”); } Input is any whole number… Which outputs can we get from this chained conditional? What about if we changed the first if to true What if we removed the else?
9
Nested Conditionals If (condition) { if (condition2){ //Statements } else { } } else { You can also nest a conditional inside another tabbing can help read through the code In general it is a bad idea to have too much nesting… If (year%4 == 0) if (year %100 == 0) if (year %400 ==0) leap year else not Leap year! Else How would you write a conditional to determine if a year was a leap year?
10
Shortening Conditionals
If the body of the conditional is a single line, then braces (‘{‘) are not needed. Avoid using a conditional just to return the value of a test Do not write unnecessary else conditions if (condition) return condition; return true; else return false;
11
AD Exercises 4.2 & 4.3 If you are given three sticks, you may or may not be able to arrange Exercise 4.2 them in a triangle. For example, if one of the sticks is 12 inches long and the other two are one inch long, it is clear that you will not be able to get the short sticks to meet in the middle. For any three lengths, there is a simple test to see if it is possible to form a triangle: “If any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can.” Write a method named isTriangle that it takes three integers as arguments, and that returns either true or false, depending on whether you can or cannot form a triangle from sticks with the given lengths. The point of this exercise is to use conditional statements to write a method that returns a value. a. Write the number 1 next to the first statement of this program that will be executed. Be careful to distinguish things that are statements from things that are not. b. Write the number 2 next to the second statement, and so on until the end of the program. If a statement is executed more than once, it might end up with more than one number next to it. c. What is the value of the parameter blimp when baffle gets invoked? d. What is the output of this program?
12
Assignments Lab 1 due today! Lab 2 assigned today Readings Friday
AD Appendix A & B KG Notes
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.