2017年7月24日 星期一

CS106A - Programming Methodology - Lecture 6

int x = readInt("......");
double y = readDouble("......");

^for getting input from users

operator
+
-
*
/
%





division: if you use integer divide integer that you use two integers to do the operation, it'll give you an integer response. 
ex. 5/2 = 2 (throw away the remainder!)
if you need the remainder you should use the remainder operator "%"
But if you use a number looks like read value, it'll give you a real value response:
ex. 5/2.0 = 2.5 (Hooray!)


operator order
()
*, /, %
+, -

int x;
x= 1+3*5/2;

x= 1+15/2(and these are integers so it only give you an integer response)
x= 1+7 = 8

int x = 5;
double y = x/2;

the program evaluates the right hand side first so x and 2 are both integers, gets integer response. you will get "y = 2.0"

CAST: pretend something is another type just for a while, does NOT change what it really is

double y = (double) x/2;

it'll operate x and 2 as doubles and the answer would be "2.5"

also you can CAST a double into an integer:

int z = (int) y;

then the z would be "2" 

In the life of computer science, no such a thing called round up!!
just drop everything behind the dot so 2.5 -> 2 but NOT 3


average two integers program

int x;
x=3;
x=x+1;
//this is done so often so there's a short way to do it:
x+=1;
x++;
//only for plus one
The last expression is the same as the one in Karel program: for loop
and also the name of language C++ from XD

on the conversed way:

x=x-1;
x-=1;
x--;

also in multiple:

x=x*2;
x*=2;

and divide:

x=x/2;
x/=2;

These are some short hands for operators.
Both work on integers and doubles.



"CONSTANT"

A variable varies and a constant remains the same. 

private static final double Pi = 3.14;

"private" means the thing we define now is only in this class
"static" means the objects in the class share the same version of this particular thing
"final" means this is the final version/this is a constant value
"double" the type
"Pi" the name



"boolean"

the value of this type is either true or false

boolean p;
p= (3>5);

since 3 is not greater than 5, the value of p is false.

Boolean expression is just a test for a condition
Essentially evaluates to true or false

Value comparisons:
== "equals" (note: not single =, single = means assignment!!!!)
!= "not equals" (cannot say <>) (in other language it is)
> "greater than"
< "less than"
>= "greater than or equal to"
<= "less than or equal to"

Boolean comparisons (in order of precedence)
! "not"
!p if p is true, then !p is false and vice versa

& (ampersand) 

&& "and"
p&&q only true if p and q are both true

| (vertical bar) (鍵盤最右上角,右斜線的shift)

|| "or"
p||q true if p or q is true

boolean p = (x != 1) || (x != 2);

THIS IS BUGGY!
The statement is always true because while one of them is true it'll be true.

you really want:

boolean p = (x != 1) && (x != 2);


中文解釋一下,邏輯裡面p敘述錯或q敘述錯指的是其中一個情況成立就成立
但是英文講起來會有點怪因為英文會說當p敘述錯或是q敘述錯的時候就blahblah
而這樣寫起來是不對的,因為你其實是指當兩個狀況都成立的時候
所以比方說,我不吃牛肉或不吃豬肉,中文可能很奇怪但你會知道這個人的意思是他牛豬都不吃


Short Circuit Evaluation

Stop evaluating boolean expression as soon as we know the answer
consider:

p = (5>3) || (4<=2);

because this statement is always true as long as the first part is true, so actually java never really evaluate the second part (4>=2) at all!! this is "short" circuit evaluation.

example of useful case:

p = (x != 0) && ((y/x) == 0);

avoid division by 0 since the second part is not performed.

越來越難了QQ 總之這裡的意思是說,當boolean驗證敘述的時候他檢查到第一個對或不對,不需要往下檢查的時候他就會停住
而這個好處是我們有時候可以用來避免一些我們不想發生的事情,比方除以零
所以當我們寫出一個像上面栗子的敘述時,只要 x 是零就根本不會驗證第二個敘述
就不會發生除以零的事情




Statement Blocks

A compound statement (or block) is a set of statements enclosed in braces

{
    int x = 5;
    double y = readDouble ("y: ")
    println ("y = " +y);
}

This is a block

Variable's SCOPE is block in which it is declared
Scope: lifetime of variables
when the variable is available to be used

當你建立一個 block,放了左括號開始,你在裡面建立的變數都有一個 scope
意思是這個變數的生命,變數存在的時間就是在這個 block 裡面
當放了右括號下去,完成一個 block,裡面的變數就不存在了


if statement (just like Karel)

General form: 

if (condition) {
    statement
}

if ((num % 2) == 0)
    println("num is even");

if ((num % 2) == 0) {
    println("num is even");
    println("Oh, and Beat Cal!");
}

But try to use braces even if there is only one statement in the if
To define the block
(So what exactly the oh and beat cal is?(okay I know its like soukei sen keisou sen and NTHU vs NCTU)

if else statement

General form: 

if (condition) {
    statement
} else {
    statement
}

if ((num % 2) == 0)
    println("num is even");
} else {
    println("num is odd");
    println("and so are you.");
}

(幹嘛這樣人身攻擊QQ)
just as same as Karel

"Cascading if"

if (score >= 90) {
    println ("A");
} else if (score >= 80) {
    println ("B");
} else if (score >= 70) {
    println ("C");
} else {
    println ("Bad Times");

to check several situations
this is an exception for the good habit to put braces even there is only one statement
because actually else should have another brace here but there is only one if statement
(though contains other things inside)
so we don't use braces here (or it'll be too complicated)

"switch statement"
you can do it with if statement as well

int day = readInt ("Day of week as int: ");
switch (day) {
    case 0:
        println("Sunday");
        break;
    case 6:
        println("Saturday");
        break;
    default:
        println("Weekday")
        break;
}

there are no braces in the statement (block), but use break to stop the statement.
the time you can pick one of the situation of them


"for loop"

General form:

    for (init ; condition ; step) {
        statement
    }
    


init does once and only once at the start of loop
condition checked before every iteration through loop
we execute statements if the condition is true
step every time through loop after statements

Example:

    for (int i = 0; i < 5; i++) {
        println(i);
    }

to do something five times
note that when you want to count something 5 times, you use 0 to 4 instead of 1 to 5
the scope of i is just in this for loop

Another example:

    for (int i = 6; i < 0; i -= 2) {
        println(i);
    }

count down instead of count up
not that 0 would not be displayed as the 5 in last example

while loop
the same as Karel




沒有留言:

張貼留言