31
For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers Prof. Learned-Miller

For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

For Loops, Ifs, and math February 14, 2012

CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Prof. Learned-Miller

Page 2: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

1

Assignments: Check OWL

Page 3: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

E-book embedded exercises  They ARE graded.  OWL doesn’t know about them, so it will

say confusing stuff.

2

Page 4: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Bugs in Section 4.4  E-book Section 4.4. Problems have been

fixed.

3

Page 5: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

4

Today  A break from objects, sort of.  Math  For loops   If statements  Fun with graphics

Page 6: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Converting between types  int i=3;! double d;! d=i; // No problem.! d=3.5;! i=d; // Java doesn’t like.! i= (int) d; // This is OK.!

5

Page 7: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Some basic math  int j=3;! j=j+1;! j=j*2;! j=j/j;! j=10-j;!

6

Page 8: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Shortcuts   Instead of

  j=j+1;!  j++;!

7

Page 9: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Shortcuts   Instead of

  j=j-1;!  j--;!

8

Page 10: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Shortcuts   Instead of

  j=j+5;!  j+=5;!

9

Page 11: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Shortcuts   Instead of

  j=j-7;!  j-=7;!

10

Page 12: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Shortcuts   Instead of

  j=j*7;!  j*=7;!

11

Page 13: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean b=true;! boolean c=false;!

12

Page 14: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean b;! b=(3==3); // This is weird!!

13

Page 15: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean b;! b=(3==3); // This is weird!!

 boolean c;! c= (3==4); // This is weirder!!

14

Page 16: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  int x=3;! int y=4;! if (x<y) !S...println(“y is bigger”);!

15

Page 17: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false

16

Page 18: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean x= (3==4 || 3<4);!

17

Page 19: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean x= (5>2 && 10>8);!

18

Page 20: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean x= (5>2 && 10>10);!

19

Page 21: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean x= (5>2 && 10>=10);!

20

Page 22: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean x= !false;!

21

Page 23: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean x= !!false;!

22

Page 24: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false  boolean x= !!!!!!false; // 6!

23

Page 25: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Loops! Finally!  for (int i=0; i<10; i=i+1)

!System.out.println(i);!

24

Page 26: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Loops

25

Page 27: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Booleans: true or false

26

Page 28: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Loops

27

Page 29: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Ifs (or conditionals)

28

Page 30: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

Ifs (or conditionals)

29

Page 31: For Loops, Ifs, and mathelm/Teaching/121_S12/lectures/lectur… · For Loops, Ifs, and math February 14, 2012 CMPSCI 121, Spring 2012 Introduction to Problem Solving with Computers

30

End for today