Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 10 Recap CSE 115 Spring 2007. For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.

Similar presentations


Presentation on theme: "Week 10 Recap CSE 115 Spring 2007. For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each."— Presentation transcript:

1 Week 10 Recap CSE 115 Spring 2007

2 For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each loop to help us iterate over all elements of that collection. When we have a collection and want to do something to all elements of that collection we use the for-each loop to help us iterate over all elements of that collection.

3 For-each loop syntax for(Type identifier1: identifier2) { //code to execute for each element } Where: Where: –identifier2 is the name of the collection that you are iterating over –Type identifier1 is the creation of a reference to each element of the collection to use as you are iterating.

4 If-statements  Selection based on the value of a boolean expression.

5 If-statement syntax if (booleanExpression) { //code to be executed if //booleanExpression evaluates to //true}

6 Boolean expressions Expressions that evaluate to either true or false Expressions that evaluate to either true or false boolean is a type built into Java boolean is a type built into Java However, boolean is not a class However, boolean is not a class

7 Boolean values and operators Methods can return boolean values Methods can return boolean values Can combine boolean values using logic operators Can combine boolean values using logic operators && (logical and) || (logical or) ! (logical not)

8 Operators that return a boolean value Comparison operators return boolean values as well. These operators work on numeric values. Comparison operators return boolean values as well. These operators work on numeric values. < (less than) < (less than) > (greater than) > (greater than) <= (less than or equal to) <= (less than or equal to) >= (greater than or equal to) >= (greater than or equal to)

9 Operators that return a boolean value There are two equality operators in Java: There are two equality operators in Java: == (equality) != (not equal) These work on numeric values as you would expect from arithmetic These work on numeric values as you would expect from arithmetic But are also defined on references where they compare if the references are the same. But are also defined on references where they compare if the references are the same.


Download ppt "Week 10 Recap CSE 115 Spring 2007. For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each."

Similar presentations


Ads by Google