Notes on Selection Statement | Introduction-Computer Science | CS 110, Study notes of Computer Science

Material Type: Notes; Professor: Tanner; Class: Introduction-Computer Science; Subject: Computer Science; University: West Virginia University; Term: Unknown 2009;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-upw-1
koofers-user-upw-1 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 110 Notes – Selection Statements 1
Copyright C. Tanner 2009
Selection Statements
o Alternative execution paths based on the result of a conditional expression
o Decision making statements
Conditional Expression
o Boolean expression/logical expression
o Has a value of true or false
o relational operators (ro):
< > <= >= == !=
Variable ro variable
a < b a==b a!=b
decimals and == doesn’t always work when expected due to
rounding
the relational operators only work on primitive data types (int,
double, boolean, char) not on class types such as String
x = -5 power = 1024 maxPower=1024 y=7 item =1.5
minItem = -999.9 momOrDad = ‘m’ num=999 sentinel =999
x <=0
power > maxPow
item > minItem
x >= y
momOrDad == ‘M’
num != sentinel
Logical operators
o && and
o || or
o !
salary < minSalary || numDependants > 5
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Notes on Selection Statement | Introduction-Computer Science | CS 110 and more Study notes Computer Science in PDF only on Docsity!

  • Selection Statements o Alternative execution paths based on the result of a conditional expression o Decision making statements
  • Conditional Expression o Boolean expression/logical expression o Has a value of true or false o relational operators (ro): ƒ < > <= >= == != ƒ Variable ro variable ƒ a < b a==b a!=b ƒ decimals and == doesn’t always work when expected due to rounding ƒ the relational operators only work on primitive data types (int, double, boolean, char) not on class types such as String

x = -5 power = 1024 maxPower=1024 y=7 item =1.

minItem = -999.9 momOrDad = ‘m’ num=999 sentinel =

x <=

power > maxPow

item > minItem

x >= y

momOrDad == ‘M’

num != sentinel

  • Logical operators o && and o || or o!

salary < minSalary || numDependants > 5

dewPoint > 68 && humidity > 82 score >=80 && score <

&& T F || T F!

T T F T T T T F

F F F F T F F T

  • Boolean Variables o Variable which has the value of true or false o boolean leapYear; o boolean found; !found o boolean more;

winningRecord && !onProbation

  • Operator Precedence

! –

  • / %

< > <= >= == != && || =

x+a < y+b Î (x+a) < (y+b)

min<= x && y <=max Î (min <=x) && ( y <= max)

flag || leapYear && switch Î flag || (leapYear && switch)

x= 3.0 y = 4.0 z = 2.0 flag = false

!flag

x+y/7 <+ 3.

(!flag) || (y+z) >= (x-z)

  • Reference is the memory location actually holding the data
  • New operator Î acquires memory to hold the data for the class type and stores the data in that memory and the memory’s address in the reference
  • Called objects, instance of a class (String)

String s; s = new String (“CS 110”);

Memory address Variable Data 100 s 116 104 q 112 108 112 CS 101 116 CS 110

  • Assignment operator and references o = Î stores the memory location of the instance in the reference variable o s =q; ƒ s now contains 112 Examples

String S= “CS”; String T = “CpE”; String Q = “Dual”;

S T Q Z R X

String Z = “CS”; String R = new String (Z); String X = R;

  • When = is used the system looks to see if the string exists and if so points the reference to it

CS

CpE Dual

String s1 = new String (“Happy”); String s2 = “Happy”;

s s

Consider String s2 =”Happy”; String s1 = new “Happy”;

s s

  • Comparing Class Objects o == and != compare references - They determine if two class objects “point” to the same memory not if the data is equivalent - To compare the data objects of class references .equals - S.equals(R) - S.equalsIgnoreCase(R) - For .compareTo - S.compareTo(Z) - Negative value if S < Z - Positive Value if S > Z - 0 if ==
  • Strings
    • Can’t change the value of a string through assignment
    • String s =”abc”;
    • Can’t later say s=”xyz”; you would have to say s=new String (“xyz”);
  • Happy

    Happy Happy

    Evaluate the condition Æ if true execute 1st^ block, if false execute the else block

    if (hours <=40) pay = ratePerHour * hours; else pay = ratePerHour * 40 + ratePerHour 1.5(hours -40);

    Write a method that returns the larger of two values passed as parameters

    public static int max (int x, int y) { int maxValue;

    if (x>y) maxValue = x; else maxValue =y;

    return maxValue; }

    Sample Programs: Piglatin2.java, Roulette.java, Pyth3.java, Division.java, Ckbook.java

    Homework set 4: Write the JAVA statements to accomplish the following

    a). If item is non-zero, multiply product by item and save the result in product ; otherwise, skip the multiplication. Either way, display the value of product.

    b). Store the absolute difference of x and y in y , where the absolute difference is (x-y) or (y-x) whichever is positive.

    c). If x is 0, add 1 to zeroCount. If x is negative add x to minusSum. If x is greater than 0 add x to plusSum.

    d). Write a method sameSign() that, given two double parameters return true if they are of the same sign and false if they have difference signs.

    e). Rewrite sameSign() which returns the actual sign ‘+’ or ‘-‘ if they are the same sign and a blank if they are different signs.

    • Multiple Alternative Decisions o Need more than 2 alternatives ƒ Example 0 + - if (x==) stmt //x = else if (x > 0) stmt //x > 0 else stmt //x<

    ƒ Membership dues set on a sliding salary scale if (salary <=10000) dues= .05 * salary; else if (salary <= 30000) dues = .08 * salary; else if ( salary <= 50000) dues = .1 * salary; else if (salary <= 70000) dues = .15 * salary; else dues = .2 * salary

    o Comparison of using separate if’s o 0 + - if (x ==0) stmt if (x<0) stmt if(x>0) stmt

    each is tested when if one is true the others are not true and should not be tested – else construct does not retest o Membership dues if (salary < 10000) dues = if (salary >=10000 && salary < 30000) dues = if (salary >=30000 & salary < 500000) dues = if (salary >=50000 & salary <700000) dues = if (salary >=70000) dues

    switch (n) { case 1: case 2:System.out.print (“Buckle my Shoe”); break; case 3: case4: System.out.print(“Shut the Door”); break; case 5: case 6: System.out.print (“Pick up Sticks”); break; case 7: case 8: System.out.print (“Shut the Gate”); break; case 9: case 10: System.out.print (“Start Again”); break; default: System.out print(“Invalid value”); }

    • Sample ProgramsÆ Switch.java, Vowels.java