Programming Language II
CSE-215
Prof. Dr. Mohammad Abu Yousuf
yousuf@juniv.edu
1
Comparison between C++ and Java
2
3
4
5
Variable :
• There are three types of variables: local, instance and static.
• Local Variable:
• A variable that is declared inside the method is called local
variable.
• Instance Variable:
• A variable that is declared inside the class but outside the
method is called instance variable . It is not declared as static.
• Static variable:
• A variable that is declared as static is called static variable. It
cannot be local.
6
• Example:
7
Control Statements
8
Java’s Selection Statements
• Java supports two selection statements: if and switch.
• These statements allow you to control the flow of your
program’s execution based upon conditions known only
during run time.
• There are various types of if statement in java.
– if statement
– if-else statement
– nested if statement
– if-else-if ladder
9
• Java If statement:
– if (condition) statement1;
10
• Java IF-else Statement
– if (condition) statement1;
else statement2;
11
• Java IF-else-if ladder Statement
12
13
• Java Nested ifs:
14
• Java Switch Statement:
expression must be of type
byte, short, int, char, or an
enumeration.
Expression can also be of
type String.
Each value specified in the
case statements must be a
unique constant expression
(such as a literal value).
Duplicate case values are
not allowed. The type of
each value must be
compatible with the type of
expression. 15
• Example:
16
• The break statement is optional. If you omit the break,
execution will continue on into the next case.
• It means it executes all statement after first match if
break statement is not used with switch cases.
• It is sometimes desirable to have multiple cases without
break statements between them.
17
Output:
20
30
Not in 10, 20 or 30
18
• For example:
19
• Output:
20
• Nested switch Statements:
21
Iteration Statements
22
• Java’s iteration statements are for, while, and do-while.
These statements create what we commonly call loops.
23
For loop:
24
• Using the Comma in for loop:
25
• Some for Loop Variations
In this example, the for loop continues
to run until the boolean variable done
is set to true. It does not test the value
of i.
Here, the initialization and
iteration expressions have
been moved out of the for.
Thus, parts of the for are
empty.
26
Java 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.
27
• Java For-each Loop:
Output:
12
23
44
56
78
28
Java Labeled For Loop:
• We can have name of each 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.
• Normally, break and continue keywords breaks/continues
the inner most for loop only.
29
If you use break bb;,
it will break inner
loop only which is
the default behavior
of any loop.
30
Using break to Exit a Loop:
• By using break, you can force immediate termination of a
loop, bypassing the conditional expression and any
remaining code in the body of the loop.
• When a break statement is encountered inside a loop,
the loop is terminated and program control resumes at
the next statement following the loop.
31
• Example:
32
• When used inside a set of nested loops, the break
statement will only break out of the innermost loop.
33
• Using continue:
• Sometimes it is useful to force an early iteration of a
loop. That is, you might want to continue running the
loop but stop processing the remainder of the code in its
body for this particular iteration.
• The continue statement performs such an action.
34
• As with the break statement, continue may specify a
label to describe which enclosing loop to continue.
35
return statement:
• The return statement is used to explicitly return from a
method. That is, it causes program control to transfer
back to the caller of the method.
• As you can see, the final println() statement is not
executed. As soon as return is executed, control passes
back to the caller.
Output:
Before the return.
36
Thank you
37