Presentation is loading. Please wait.

Presentation is loading. Please wait.

In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.

Similar presentations


Presentation on theme: "In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program."— Presentation transcript:

1 In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program

2 We will learn about: Boolean Shorthand's for adding, subtracting Constants If, for, while and scope

3 Just like readInt is used for reading integers readDouble is used to reading doubles

4 Lets have a look at an example of area of a circle. double PI =3.14; PI is a variable and it can be changed which means the whole equation will change. In order to define a constant we must use: private static final double PI=3.14; You could come up with a constant called SEVENTY_TWO=72

5 import acm.program.*; public class CalculateAreaOfCircle extends ConsoleProgram { public void run() { double pi = 3.14; println(“This program will calculate the area of a circle”); double r = readDouble(“Enter the radius”); double area = pi * pi * r; println(“The area of the circle is:” + area + “.”); }

6 Seems like x = x +1 is very commonly used in programming languages There are short hands for doing this: x+=1; x++; x-=1; x--; x*=2; x/=2;

7 Boolean is a data type which contains true or false P=(3>5) P=False George Boole was the person who came up with the data type Boolean. He was an English Mathematician. He died of pneumonia.

8 Boolean expression is just a test for a condition Essentially, evaluates to a true or false Value Comparisons == equals != not equals > greater than < less than >= greater than equal to <= less than equal to

9 Boolean expressions in order of precedence: ! “not” !p if p is true than p is false and vice versa && “and” P && q (only true if both p and q are true) || “OR” P || Q “either p or q or both are true”

10 Stop evaluation Boolean expressions as soon as we know the answer Consider: P = (5 > 3) || (4<=2); The test (4 <= 2 ) is not performed!

11 A compound statement (or block) is a set of statements enclosed in braces { int x = 5; double y=readDouble(“y:”); println(“y=“ + y); } Variables scope is a block in which it is declared Scope is the lifetime of a variable When the variable is available to be used.

12 General form: if (condition) { statements } With else: if (condition) { statements } else if (conditon) { } Its always a good idea to use braces even if there is only one statement

13 If ( (num % 2) ==0 ) { println(“num is even”); } else { println(“number is odd”); println(“and so are you!”); } Its always a good idea to use braces even if there is only one statement

14 If (score>=90) { Println(“A”); } else if (score>=80) { Println(“B”); } else if )score>=70) { Println(“C”); } else { Println (“Bad times!”); }

15 Int day = readInt(“Day of the week as int:”); Switch (day) { Case 0: Println(“Sunday”); Break; Case 6: Println(“Saturday”); Break; Default: Println(“Weekday”); break

16 General form: For (init; condition; step) { Statements; } Init: done once at the start of the loop (initialization) Condition: checked before every iteration through loop Statements get executed if conditions is true

17 Example: for (int i=0; i<5; i++) { println(i); } 0 1 2 3 4 As computer scientists we count from 0 The variable I dies after the brace

18 Example: For (int i=6; i>0; i=-2) { Println(i); } 6 4 2 0 is not displayed

19 General form: While (condition) { statements;} Example: int x=15; while (x>1) { x/=2; println(x); } 7 3 1 0 is not displayed

20 Before beginning to use ACM library you need to download it from: http://www-cs-faculty.stanford.edu/~eroberts/jtf/acm.jar


Download ppt "In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program."

Similar presentations


Ads by Google