Module-2.2.pptx
Document Details
Uploaded by TrustingPeridot
Full Transcript
Expressions, Statements and Control-flow Mechanisms, and Array Expressions Expressions perform the work of a Java program. Among other things, expressions are used to compute and assign values to variables and to help control the execution flow of a program. The job of an expression is two-fold: p...
Expressions, Statements and Control-flow Mechanisms, and Array Expressions Expressions perform the work of a Java program. Among other things, expressions are used to compute and assign values to variables and to help control the execution flow of a program. The job of an expression is two-fold: perform the computation indicated by the elements of the expression and return some value. Example: int score; score = 90; Expressions Example: Double a = 2.2, b = 3.4, result; result = a + b - 3.4; Example: if (number1 == number2) System.out.println("Number 1 is larger than number 2"); Statements and Control Flow Java compiler executes the code from top to bottom. The statements in the code are executed according to the order in which they appear. However, Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements. It is one of the fundamental features of Java, which provides a smooth flow of program. Statements and Control Flow Decision Making statements • if statements • switch statement Loop statements • • • • do while loop while loop for loop for-each loop Jump statements • break statement • continue statement Statements and Control Flow If Statement In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted depending upon the specific condition. The condition of the If statement gives a Boolean value, either true or false. In Java, there are four types of if-statements given below. 1.Simple if statement 2.if-else statement 3.if-else-if ladder 4.Nested if-statement Statements and Control Flow Simple if statement It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true Example: int x = 10; int y = 12; if(x+y > 20) { System.out.println("x + y is greater than 20"); } Statements and Control Flow if-else statement The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false Example: int x = 10; int y = 12; if(x+y < 10) { System.out.println("x + y is less than 10"); } else { System.out.println("x + y is greater than 20"); } Statements and Control Flow if-else-if ladder The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can say that it is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the condition is true. We can also define an else statement at the end of the chain. Statements and Control Flow Example: String city = "Delhi"; if(city == "Meerut") { System.out.println("city is meerut"); }else if (city == "Noida") { System.out.println("city is noida"); }else if(city == "Agra") { System.out.println("city is agra"); }else { System.out.println(city); } Statements and Control Flow Nested if-statement In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement. Statements and Control Flow Example: if(address.endsWith("India")) { if(address.contains("Meerut")) { System.out.println("Your city is Meerut"); }else if(address.contains("Noida")) { System.out.println("Your city is Noida"); }else { System.out.println(address.split(",")[0]); } }else { System.out.println("You are not living in India"); } Statements and Control Flow Switch Statement In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. The switch statement is easier to use instead of if-else-if statements. It also enhances the readability of the program. Statements and Control Flow Example: int num = 2; switch (num){ case 0: System.out.println("number is 0"); break; case 1: System.out.println("number is 1"); break; default: System.out.println(num); } Statements and Control Flow Loop Statements In programming, sometimes we need to execute the block of code repeatedly while some condition evaluates to true. However, loop statements are used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition. Statements and Control Flow For loop In Java, for loop is similar to C and C++. It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. We use the for loop only when we exactly know the number of times, we want to execute the block of code Statements and Control Flow Statements and Control Flow Example: int sum = 0; for(int j = 1; j<=10; j++) { sum = sum + j; } System.out.println("The sum of first 10 natural numbers is " + sum); Statements and Control Flow for-each loop Java provides an enhanced for loop to traverse the data structures like array or collection. In the for-each loop, we don't need to update the loop variable. The syntax to use the for-each loop in java is given below. Statements and Control Flow Example: String[] names = {"Java","C","C+ +","Python","JavaScript"}; System.out.println("Printing the content of the array names:\n"); for(String name:names) { System.out.println(name); } Statements and Control Flow while loop The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop. Statements and Control Flow Statements and Control Flow Example: int i = 0; System.out.println("Printing the list of first 10 even numbers \n"); while(i<=10) { System.out.println(i); i = i + 2; } Statements and Control Flow do-while loop The do-while loop checks the condition at the end of the loop after executing the loop statements. When the number of iteration is not known and we have to execute the loop at least once, we can use do-while loop. Statements and Control Flow Statements and Control Flow Example: int i = 0; System.out.println("Printing the list of first 10 even numbers \n"); do { System.out.println(i); i = i + 2; }while(i<=10); Array Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. String[] array = new String[100]; How to declare array? dataType[] arrayName; dataType – it can be primitive data types like int, char, double, byte, etc. arrayName – is the identifier. How to Initialize Arrays in Java? int[] age = {12, 4, 5, 2, 5}; The number or index are known to be identifier of each value that has been initialize on the array. // declare an array int[] age = new int[5]; // initialize array age[0] = 12; age[1] = 4; age[2] = 5; How to Access Elements of an Array in Java? you can access the elements inside of the array using the index that will start from 0. // access array elements array[index] How to Access Elements of an Array in Java? class Main { public static void main(String[] args) { // create an array int[] age = {12, 4, 5, 2, 5}; // access each array elements System.out.println("Accessing Elements of Array:"); System.out.println("First Element: " + age[0]); System.out.println("Second Element: " + age[1]); System.out.println("Third Element: " + age[2]); System.out.println("Fourth Element: " + age[3]); System.out.println("Fifth Element: " + age[4]); } } Access the array using loop class Main { public static void main(String[] args) { // create an array int[] age = {12, 4, 5}; // loop through the array // using for loop System.out.println("Using for Loop:"); for(int i = 0; i < age.length; i++) { System.out.println(age[i]); } } } Multidimensional Arrays Arrays we have mentioned till now are called onedimensional arrays. However, we can declare multidimensional arrays in Java. A multidimensional array is an array of arrays. That is, each element of a multidimensional array is an array itself Multidimensional Arrays Sample code: double[][] matrix = {{1.2, 4.3, 4.0}, {4.1, -1.1} };