2. Java Basics. Java Statements

Содержание

Слайд 2

Comments /* . . . */ - multi line comment //

Comments

/* . . . */ - multi line comment
// .

. . - single-line comment
/** . . . */ - comment for documentation. It can include some additional tags (e.g. @version, @author, @param, @return).

*

Infopulse Training Center

Слайд 3

How to Use Comments Comments should be used to give overviews

How to Use Comments

Comments should be used to give overviews of

code and provide additional information that is not readily available in the code itself
Comments should contain only information that is relevant to reading and understanding the program
Doc comments describe Java classes, interfaces, constructors, methods, and fields
Java associates documentation comments with the first declaration after the comment

*

Infopulse Training Center

Слайд 4

Variables Variables declaration: int k; double x = 2.1; Local variable

Variables

Variables declaration: int k;
double x = 2.1;
Local variable should be

initialized before it will be used.
Variable scope is a block where it was declared.
Sub block can’t contain duplicated variable declaration.

*

Infopulse Training Center

Слайд 5

1. What will this program output? public class InitTest { public

1. What will this program output?

public class InitTest {
public static

void main (String[] args) {
int a = 5;
int b;
if (a < 0) b = 10;
if (a >= 0) b = 50 ;
System.out.println(b);
}
}

*

Infopulse Training Center

Слайд 6

2. What will this program output? public class Oblzm { public

2. What will this program output?

public class Oblzm {
public static

void main (String args[]){
int i = 5;
{
int j = 2;
System.out.println("Result is " + i * j);
}
}
}

*

Infopulse Training Center

Слайд 7

3. What will this program output? public class Oblzm { public

3. What will this program output?

public class Oblzm {
public static

void main (String args[]){
int i = 5;
{
int j = 2;
}
System.out.println("Result is “ + i * j);
}
}

*

Infopulse Training Center

Слайд 8

4. What will this program output? public class Oblzm { public

4. What will this program output?

public class Oblzm {
public static

void main (String args[]){
int i = 5;
{
int j = 2;
int i = 4;
System.out.println("Result is " + i * j);
}
}
}

*

Infopulse Training Center

Слайд 9

Constant Declaration final modifier: final int a = 42; // a

Constant Declaration

final modifier: final int a = 42;
// a =

12; - compile error

*

Infopulse Training Center

Слайд 10

If-Then_Else Statement I if (boolean_expression) { statements } if (boolean_expression) {

If-Then_Else Statement I

if (boolean_expression) {
statements
}
if (boolean_expression) {
statements
} else {
statements
}

*

Infopulse Training

Center
Слайд 11

If-Then_Else Statement II if (boolean_expression) { statements; } else if (boolean_expression)

If-Then_Else Statement II

if (boolean_expression) {
statements;
} else if (boolean_expression) {
statements;
} else if

(boolean_expression) {
statements;
}

*

Infopulse Training Center

Слайд 12

If-Then-Else Example int a = 10; if (a > 0) {

If-Then-Else Example

int a = 10;
if (a > 0) {
System.out.println("Positive");
}
else if (a

< 0) {
System.out.println("Negative");
}
else {
System.out.println("Zero");
}

*

Infopulse Training Center

Слайд 13

Switch Statement switch(integral-expression) { case integer-value1 : statements; case integer-value2 :

Switch Statement

switch(integral-expression) {
case integer-value1 :
statements;
case integer-value2 :


statements;
break;
case integer-value3 :
statements;
break;
default:
statements;
}

*

Infopulse Training Center

Слайд 14

Switch Example char c = ’b’; switch(c) { case ’a’: case

Switch Example

char c = ’b’;
switch(c) {
case ’a’:
case ’e’:
case

’i’:
case ’o’:
case ’u’:
print("vowel");
break;

case ‘y’:
case ‘w’:
print("Sometimes a vowel");
break;
default:
print("consonant");
}

*

Infopulse Training Center

Слайд 15

Switch Statement Expressions You can use enumerations as switch expression and

Switch Statement Expressions

You can use enumerations as switch expression and case

values
You can use strings as switch expression and case values (since Java 7 only!)

*

Infopulse Training Center

Слайд 16

While and Do-While Statement while (boolean_expression) { statements; } do {

While and Do-While Statement

while (boolean_expression) {
statements;
}
do {
statements;
} while (boolean_expression);

*

Infopulse

Training Center
Слайд 17

While Example double a = 100.; while (a >= 1) {

While Example

double a = 100.;
while (a >= 1) {
System.out.println(a);

a /= 2.;
}

*

Infopulse Training Center

Слайд 18

Do-While Example double a = 0.5; do { System.out.println(a); a /=

Do-While Example

double a = 0.5;
do {
System.out.println(a);
a /= 2.;

} while (a >= 1);

*

Infopulse Training Center

Слайд 19

For Statement for (initialization; Boolean-expression; step) { statements; } * Infopulse Training Center

For Statement

for (initialization; Boolean-expression; step) {
statements;
}

*

Infopulse Training Center

Слайд 20

For Examples for (int i = 10, k = 20; i

For Examples

for (int i = 10, k = 20; i <

k; i++, k--) {
System.out.println(i + " " + k);
}
for (char i = 's'; i >= 'b'; i--) System.out.println(i);

*

Infopulse Training Center

Слайд 21

Break and Continue Statements break quits the loop without executing the

Break and Continue Statements

break quits the loop without executing the rest

of the statements in the loop.
continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration.
You can use a label before an iteration statement and in break / continue statements interrupt the loops up to where the label exists

*

Infopulse Training Center

Слайд 22

Use Break with a Label L: for … { for…{ break

Use Break with a Label

L: for … {
for…{
break L;

}
}

*

Infopulse Training Center

Слайд 23

Exercise 2.2.1. Find and print all divisors of a given natural

Exercise 2.2.1.

Find and print all divisors of a given natural number

n.

*

Infopulse Training Center

Слайд 24

Exercise 2.2.1. See 221Divisors project for the full text. * Infopulse Training Center

Exercise 2.2.1.

See 221Divisors project for the full text.

*

Infopulse Training Center

Слайд 25

Exercise 2.2.2. Find and print all prime divisors of a given

Exercise 2.2.2.

Find and print all prime divisors of a given natural

number n.

*

Infopulse Training Center

Слайд 26

Exercise 2.2.3. Find sum of an infinite row for a given

Exercise 2.2.3.

Find sum of an infinite row for a given x
Compare

result with a Math.exp(x) method value

*

Infopulse Training Center