Introduction to Computer Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Computer Programming

Description:

Introduction to Computer Programming CS 126 Lecture 7 Zeke Maier Plan for Today Questions Administrivia Conditionals AD Exercises 4.2 & 4.3 Assignment Questions ... – PowerPoint PPT presentation

Number of Views:159
Avg rating:3.0/5.0
Slides: 13
Provided by: ejm3
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Computer Programming


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
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
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 if() //Statements else //Statements
8
Example
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)
9
Nested 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?

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
  • http//students.cec.wustl.edu/ejm3/CS126/web/code
    .htmlconditionals

12
Assignments
  • Lab 1 due today!
  • Lab 2 assigned today
  • Readings
  • Friday
  • AD Appendix A B
  • KG Notes
Write a Comment
User Comments (0)
About PowerShow.com