Title: Introduction to Computer Programming
1Introduction to Computer Programming
- CS 126
- Lecture 7
- Zeke Maier
2Plan for Today
- Questions
- Administrivia
- Conditionals
- AD Exercises 4.2 4.3
- Assignment
3Questions
4Administrivia
- http//students.cec.wustl.edu/ejm3/
- Lab assignment 1 due this afternoon!
- Lab assignment 2 assigned today
5Conditional 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
6Conditionals
- Write a method producing the same output (y
values) given the same input (x values) as the
function below
7Chained Conditionals
- Useful when there are more than 2 discrete cases?
if (condition) //Statements to execute if the
condition is true else if() //Statements
else if() //Statements else //Statements
8Example
if (x gt 0) System.out.println(1) else if(
x 7) System.out.println(2) else
if(false) System.out.println(3) else
if(xlt0 x gt -100) System.out.println(4)
else System.out.println(5)
9Nested Conditionals
If (condition) if (condition2) //Statements
else //Statements else
//Statements
- How would you write a conditional to determine if
a year was a leap year?
10Shortening 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
11AD Exercises 4.2 4.3
- http//students.cec.wustl.edu/ejm3/CS126/web/code
.htmlconditionals
12Assignments
- Lab 1 due today!
- Lab 2 assigned today
- Readings
- Friday
- AD Appendix A B
- KG Notes