Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.

Similar presentations


Presentation on theme: "Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number."— Presentation transcript:

1 Arrays, Conditionals & Loops in Java

2 Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number of slots, each of which holds an individual item. Arrays in Java are actual objects that can be passed around and treated just like other objects

3 Creating arrays in Java 1.Declare a variable to hold the array. 2.Create a new array object and assign it to the array variable. 3.Store things in that array.

4 Declaring arrays Declare a variable that will hold the array. Declaration includes the type of object the array will hold and the name of the array, followed by empty brackets ([]). For example, string difficultWords[]; int temps[]; float y[];

5 Accessing Array Elements To get at a value stored within an array, use the array subscript expression ([]): myArray[subscript];

6 Multi-Dimensional Arrays In Java, multidimensional arrays are actually arrays of arrays. You can create a nonrectangular multidimensional array by having elements of an array refer to arrays of different sizes.

7 Multi-Dimensional Arrays (contd..) For example, to initialize a two-dimensional array of which the first element has two sub-elements and the second one has three sub-elements, you can declare it as int [] [] a = new int [2] [3]; or you can both declare and initialize it to some values like : int [] [] a = { { 1, 2}, {3, 4, 5} };

8 Control Statements in Java Java’s program control statements can be put into the following categories: 1.Selection 2.Iteration 3.Jump

9 Java’s Selection statements The if statement is Java’s conditional branch statement if (conditional_expression) statement1 else statement2 Here, if the condition is true then statement1 is executed Otherwise, the statement or block of statements after the else part will be executed.

10 if statement in Java For Example, int a, b; //…. if(a<b) a=0; else b=0; Here, if a is less than b, then a is set to zero. Otherwise, b is set to zero.

11 Nested ifs A nested if is an if statement that is the target of another if or else. if(i==10) { if(j<20) a=b; if(k>100) c=d; // this if is else a = c; // associated with this else } else a=d; //this else refers to if(i==10)

12 The conditional Operator ( ?:) An alternative to use the if and else keywords in a conditional statement is to use the conditional operator also called the ternary operator.

13 The conditional Operator ( ?:) (contd..) The conditional operator is most useful for very short or simple conditionals and looks like this: test ? trueresult : falseresult ; test is a boolean expression, meaning that it returns true or false If the test is true, the conditional operator returns the value of trueresult, if it’s false, it returns the value of falseresult.

14 Switch Conditionals The switch statement provides an easy way to dispatch execution to different parts of your code based on the value of an expression. It provides a better alternative than a large series of if-else-if statements.

15 Switch Conditionals (contd..) Syntax of the switch statement switch(test) { case valueOne: resultOne; break; case valueTwo: resultTwo; break;

16 Switch Conditionals (contd..) case valueThree: resultThree; break; …… default : defaultresult; }

17 Iteration Statements Java’s iteration statements can be categorized as 1. for loop 2. while loop 3. do-while loop

18 for Loop The simplest form of the for loop is shown here for (initialization; condition; iteration) statement

19 while Loop The while loop repeats a statement or block while its controlling expression is true. Here is its general form: while (condition) { // body of loop }

20 do-while Loop The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its general form is do { // body of loop }while (condition);


Download ppt "Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number."

Similar presentations


Ads by Google