0% found this document useful (0 votes)
11 views

Lecture 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture 2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

+

Introduction to java

Lecture 2

Kandahar University
Faculty of Computer Science
Zubair Ahmad “Khairy”
1
+ 2

Today’s Lecture

◼ Control Statements
◼ Java If-else
◼ Java Switch
◼ Loops
◼ Java For Loop
◼ Java While Loop
◼ Java Do While Loop
◼ Java Break
◼ Java Continue

◼ Arrays

◼ Question
+ 3

If-else statements

◼ The Java if statement is used to test the condition. It checks Boolean


condition: true or false. There are various types of if statement in
java.
◼ if statement
◼ if-else statement
◼ if-else-if ladder
◼ nested if statement

◼ Syntax:
+ 4

Switch Statement

◼ The Java switch statement executes one statement from multiple


conditions. It is like if-else-if ladder statement. The switch
statement works with byte, short, int, long, enum types, String and
some wrapper types like Byte, Short, Int, and Long. Since Java 7,
you can use strings in the switch statement.

◼ Syntax:
+ 5

Nested Switch
+ 6

Loops in Java

◼ In programming languages, loops are used to execute a set


of instructions/functions repeatedly when some conditions
become true. There are three types of loops in java.

◼ for loop

◼ while loop

◼ do-while loop
+ 7

What is the deference between while, for, do-while loops?


+ 8
+ 9

For Loop

◼ The Java for loop is used to iterate a part of the program


several times. If the number of iteration is fixed, it is
recommended to use for loop.

◼ There are three types of for loops in java.


◼ Simple For Loop
◼ For-each or Enhanced For Loop
◼ Labeled For Loop
+ 10

for-each Loop

◼ The for-each loop is used to traverse array or collection in


java. It is easier to use than simple for loop because we don't
need to increment value and use subscript notation.

◼ It works on elements basis not index. It returns element one


by one in the defined variable.
+ 11

Labeled For Loop

◼ We can have a name of each Java for loop. To do so, we use


label before the for loop. It is useful if we have nested for
loop so that we can break/continue specific for loop.

◼ Usually, break and continue keywords breaks/continues the


innermost for loop only.
+ 12

Labeled for loop


+ 13

Java While Loop

◼ The Java while loop is used to iterate a part of the program


several times. If the number of iteration is not fixed, it is
recommended to use while loop.
+ 14

Java do-while Loop

◼ The Java do-while loop is used to iterate a part of the program


several times. If the number of iteration is not fixed and you
must have to execute the loop at least once, it is
recommended to use do-while loop.

◼ The Java do-while loop is executed at least once because


condition is checked after loop body.
+ 15

Java Break Statement

◼ When a break statement is encountered inside a loop, the loop is


immediately terminated and the program control resumes at the
next statement following the loop.

◼ The Java break is used to break loop or switch statement. It breaks


the current flow of the program at specified condition. In case of
inner loop, it breaks only inner loop.

◼ We can use Java break statement in all types of loops such as for
loop, while loop and do-while loop.
+ 16

Break Statement
+ 17

Java Continue Statement

◼ The continue statement is used in loop control structure when you


need to jump to the next iteration of the loop immediately. It can be
used with for loop or while loop.

◼ The Java continue statement is used to continue the loop. It continues


the current flow of the program and skips the remaining code at the
specified condition. In case of an inner loop, it continues the inner
loop only.

◼ We can use Java continue statement in all types of loops such as for
loop, while loop and do-while loop.
18
+ 19

Java Arrays

◼ Normally, an array is a collection of similar type of elements which


have a contiguous memory location.

◼ Java array is an object which contains elements of a similar data


type. Additionally, The elements of an array are stored in a
contiguous memory location. It is a data structure where we store
similar elements. We can store only a fixed set of elements in a Java
array.
+ 20

Java Arrays

◼ Advantages
◼ Code Optimization: It makes the code optimized, we can
retrieve or sort the data efficiently.
◼ Random access: We can get any data located at an index
position.

◼ Disadvantages
◼ Size Limit: We can store only the fixed size of elements in the
array. It doesn't grow its size at runtime. To solve this problem,
collection framework is used in Java which grows automatically.
+ 21

Types of Array in java

◼ Single Dimensional Array


◼ Syntax to Declare an Array in Java
◼ dataType[] arr; (or)
◼ dataType []arr; (or)
◼ dataType arr[];
◼ Instantiation of an Array in Java
◼ arrayRefVar=new datatype[size];

◼ Multidimensional Array
+ 22

Single Dimensional
+ 23

For-each Loop for Java Array


+ 24

Anonymous Array in Java


+ 25

Returning Array from Functions


+ 26

Multidimensional Array

◼ In such case, data is stored in row and column based index


(also known as matrix form).
◼ Syntax to Declare Multidimensional Array in Java
◼ dataType[][] arrayRefVar; (or)
◼ dataType [][]arrayRefVar; (or)
◼ dataType arrayRefVar[][]; (or)
◼ dataType []arrayRefVar[];

◼ Example to instantiate Multidimensional Array in Java


◼ int[][] arr=new int[3][3];//3 row and 3 column
27
+ 28

Jagged array

◼ If we are creating odd number of columns in a 2D array, it is


known as a jagged array. In other words, it is an array of
arrays with different number of columns.
29
30

You might also like