Similar presentations:
Типы, переменные, управляющие инструкции. (Тема 2.1)
1.
II. Типы, переменные,управляющие инструкции
1. Управляющие инструкции
1
2.
Управляющие инструкции включаютусловные инструкции if, if else, switch
циклы for, while, do while и инструкции
перехода break, continue, return.
2
3.
Условные инструкции3
4. Условная инструкция if
ifif (condition)
(condition)
true-statement
true-statement
if
if (condition)
(condition) {{
true-statement
true-statement11
}}
true-statement
true-statement22
.. .. ..
4
5. Условная инструкция if
publicpublic class
class IfDemo
IfDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
Scanner
Scanner in
in == new
new Scanner(System.in);
Scanner(System.in);
System.out.println("Enter
System.out.println("Enter your
your sales:");
sales:");
double
double yourSales
yourSales == in.nextDouble();
in.nextDouble();
System.out.println("Enter
System.out.println("Enter your
your target:");
target:");
double
target
=
in.nextDouble();
double target = in.nextDouble();
if
if (yourSales
(yourSales >=
>= target)
target) {{
double
double bonus
bonus == 10
10 ++ 0.05
0.05 ** (yourSales
(yourSales -- target);
target);
}}
}}
}}
System.out.println("Your
System.out.println("Your performance
performance is
is Satifactory\n"
Satifactory\n"
++ "Your
"Your bonus:
bonus: "" ++ bonus);
bonus);
Enter
Enter your
your sales:
sales:
100
100
Enter
Enter your
your target:
target:
50
50
Your
Your performance
performance is
is Satifactory
Satifactory
Your
Your bonus:
bonus: 12.5
12.5
5
6. Условная инструкция if else
ifif (condition)
(condition) statement
statement11 else
else statement
statement22
if
if (condition)
(condition) {{
statement_sequence
statement_sequence11
}}
else
else {{
}}
statement_sequence
statement_sequence22
6
7. Условная инструкция if else
publicpublic class
class IfElseDemo
IfElseDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
Scanner
Scanner in
in == new
new Scanner(System.in);
Scanner(System.in);
System.out.println("Enter
System.out.println("Enter your
your sales:");
sales:");
double
yourSales
=
in.nextDouble();
double yourSales = in.nextDouble();
System.out.println("Enter
System.out.println("Enter your
your target:");
target:");
double
double target
target == in.nextDouble();
in.nextDouble();
String
String
double
double
}}
}}
performance;
performance;
bonus;
bonus;
if
if (yourSales
(yourSales >=
>= target)
target) {{
performance
performance == "Satisfactory";
"Satisfactory";
bonus
=
10
+
0.05
bonus = 10 + 0.05 ** (yourSales
(yourSales -- target);
target);
}} else
{
else {
performance
performance == "Unsatisfactory";
"Unsatisfactory";
bonus
bonus == 0;
0;
}}
System.out.println("Your
System.out.println("Your performance
performance is
is "" ++ performance
performance
++ "\nYour
bonus:
"
+
bonus);
"\nYour bonus: " + bonus);
Enter
Enter your
your sales:
sales:
100
100
Enter
Enter your
your target:
target:
200
200
Your
Your performance
performance is
is Unsatisfactory
Unsatisfactory
Your
Your bonus:
bonus: 0.0
0.0
7
8. Тернарный оператор if else
conditioncondition ?? expression
expression11 :: expression
expression22
8
9. Тернарный оператор if else
publicpublic class
class TernaryDemo
TernaryDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
Scanner
Scanner in
in == new
new Scanner(System.in);
Scanner(System.in);
System.out.println("Enter
System.out.println("Enter your
your sales:");
sales:");
double
double yourSales
yourSales == in.nextDouble();
in.nextDouble();
System.out.println("Enter
System.out.println("Enter your
your target:");
target:");
double
target
=
in.nextDouble();
double target = in.nextDouble();
String
String performance
performance == yourSales
yourSales >=
>= target
target ?? "Satisfactory"
"Satisfactory" :: "Unsatisfactory";
"Unsatisfactory";
double
double bonus
bonus == yourSales
yourSales >=
>= target
target ?? 10
10 ++ 0.05
0.05 ** (yourSales
(yourSales -- target)
target) :: 0;
0;
}}
}}
System.out.println("Your
System.out.println("Your
++ "\nYour
"\nYour bonus:
bonus: ""
performance
performance is
is "" ++ performance
performance
++ bonus);
bonus);
Enter
Enter your
your sales:
sales:
100
100
Enter
Enter your
your target:
target:
50
50
Your
Your performance
performance is
is Satisfactory
Satisfactory
Your
Your bonus:
bonus: 12.5
12.5
9
10. Инструкция множественного выбора switch
switchswitch (expression)
(expression) {{
case
case value
value11::
statement_sequence
statement_sequence11
break;
break;
case
case value
value22::
statement_sequence
statement_sequence22
break;
break;
..
..
..
case
case value
valueNN::
statement_sequence
statement_sequenceNN
break;
break;
}}
[default:
[default:
default_statement_sequence]
default_statement_sequence]
10
11. Инструкция множественного выбора switch
publicpublic class
class SwitchDemo
SwitchDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) throws
throws IOException
IOException {{
System.out.println("Enter
System.out.println("Enter your
your grade:");
grade:");
char
char grade
grade == (char)
(char) System.in.read();
System.in.read();
}}
}}
switch
switch (grade)
(grade) {{
case
case 'A':
'A':
System.out.println("Excellent!");
System.out.println("Excellent!");
break;
break;
case
case 'B':
'B':
System.out.println("Well
System.out.println("Well done");
done");
break;
break;
case
case 'C':
'C':
System.out.println("You
System.out.println("You need
need to
to improve
improve your
your grade");
grade");
break;
break;
default:
default:
System.out.println("Invalid
System.out.println("Invalid grade");
grade");
}}
System.out.println("Your
System.out.println("Your grade
grade is
is "" ++ grade);
grade);
Enter
Enter your
your grade:
grade:
BB
Well
Well done
done
Your
Your grade
grade is
is BB
11
12.
Циклы12
13. Цикл for
forfor (( initialization
initialization ;; expression
expression ;; update
update ){
){
statement
statement11
}}
statement
statement22
.. .. ..
13
14. Цикл for
publicpublic class
class ForDemo
ForDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
Scanner
Scanner in
in == new
new Scanner(System.in);
Scanner(System.in);
System.out.println("Enter
System.out.println("Enter the
the number
number of
of years
years till
till retirement:");
retirement:");
int
int years
years == in.nextInt();
in.nextInt();
System.out.println("Enter
System.out.println("Enter your
your retirement
retirement payment:");
payment:");
double
double payment
payment == in.nextDouble();
in.nextDouble();
System.out.println("Enter
System.out.println("Enter interest
interest rate:");
rate:");
double
interestRate
=
in.nextDouble();
double interestRate = in.nextDouble();
double
double balance
balance == 0;
0;
for(int
for(int yy == 0;
0; yy << years
years ;; y++)
y++) {{
}}
}}
}}
balance
balance +=
+= payment;
payment;
double
double interest
interest == balance
balance ** interestRate
interestRate // 100;
100;
balance
balance +=
+= interest;
interest;
System.out.println("After
System.out.println("After year
year "" ++ (y+1)
(y+1) ++ "" your
your balance
balance is:
is: "" ++ balance);
balance);
14
15. Цикл for
EnterEnter
10
10
Enter
Enter
100
100
Enter
Enter
10
10
After
After
After
After
After
After
After
After
After
After
After
After
After
After
After
After
After
After
After
After
the
the number
number of
of years
years till
till retirement:
retirement:
your
your retirement
retirement payment:
payment:
interest
interest rate:
rate:
year
year
year
year
year
year
year
year
year
year
year
year
year
year
year
year
year
year
year
year
11 your
your balance
balance is:
is: 110.0
110.0
22 your
your balance
balance is:
is: 231.0
231.0
33 your
balance
is:
364.1
your balance is: 364.1
44 your
your balance
balance is:
is: 510.51
510.51
55 your
your balance
balance is:
is: 671.561
671.561
66 your
your balance
balance is:
is: 848.7171000000001
848.7171000000001
77 your
your balance
balance is:
is: 1043.5888100000002
1043.5888100000002
88 your
balance
is:
1257.9476910000003
your balance is: 1257.9476910000003
99 your
your balance
balance is:
is: 1493.7424601000002
1493.7424601000002
10
10 your
your balance
balance is:
is: 1753.1167061100002
1753.1167061100002
15
16. Цикл “for each” или “enhanced for”
forfor (( Type
Type element
element :: collectionOfType
collectionOfType )) {{
statement
statement11
}}
statement
statement22
.. .. ..
16
17. Цикл “for each” или “enhanced for”
publicpublic class
class EnhancedDemo
EnhancedDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
String[]
String[] fruitArray
fruitArray == {{ "Apple",
"Apple", "Grapes",
"Grapes", "Mango",
"Mango", "Orange",
"Orange", "Melon",
"Melon", "Kiwi"
"Kiwi" };
};
for
for (String
(String aa :: fruitArray)
fruitArray) {{
}}
}}
}}
System.out.println(a);
System.out.println(a);
Apple
Apple
Grapes
Grapes
Mango
Mango
Orange
Orange
Melon
Melon
Kiwi
Kiwi
17
18. Цикл while
whilewhile (condition)
(condition) {{
statement
statement11
}}
statement
statement22
.. .. ..
18
19. Цикл while
publicpublic class
class WhileDemo
WhileDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
Scanner
Scanner in
in == new
new Scanner(System.in);
Scanner(System.in);
System.out.println("Enter
System.out.println("Enter your
your retirement
retirement goal:");
goal:");
double
goal
=
in.nextDouble();
double goal = in.nextDouble();
System.out.println("Enter
System.out.println("Enter your
your retirement
retirement sallary:");
sallary:");
double
double payment
payment == in.nextDouble();
in.nextDouble();
System.out.println("Enter
System.out.println("Enter interest
interest rate:");
rate:");
double
interestRate
=
in.nextDouble();
double interestRate = in.nextDouble();
double
double balance
balance == 0;
0;
int
int years
years == 0;
0;
while
while (balance
(balance << goal)
goal) {{
balance
balance +=
+= payment;
payment;
double
double interest
interest == balance
balance ** interestRate
interestRate // 100;
100;
balance
balance +=
+= interest;
interest;
System.out.println("Your
System.out.println("Your balance
balance is:
is: "" ++ balance);
balance);
years++;
years++;
}}
}}
}}
System.out.println("Your
System.out.println("Your will
will be
be able
able to
to retire
retire in
in "" ++ years
years ++ "" years\n");
years\n");
19
20. Цикл while
EnterEnter your
your retirement
retirement goal:
goal:
1000
1000
Enter
Enter your
your retirement
retirement sallary:
sallary:
100
100
Enter
Enter interest
interest rate:
rate:
10
10
Your
Your balance
balance is:
is: 110.0
110.0
Your
Your balance
balance is:
is: 231.0
231.0
Your
Your balance
balance is:
is: 364.1
364.1
Your
balance
is:
510.51
Your balance is: 510.51
Your
Your balance
balance is:
is: 671.561
671.561
Your
Your balance
balance is:
is: 848.7171000000001
848.7171000000001
Your
Your balance
balance is:
is: 1043.5888100000002
1043.5888100000002
Your
Your will
will be
be able
able to
to retire
retire in
in 77 years
years
20
21. Цикл do while
dodo {{
statement
statement1
1
statement
statement22
.. .. ..
}}
while
while (condition);
(condition);
21
22. Цикл do while
publicpublic class
class DoWhileDemo
DoWhileDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
Scanner
Scanner in
in == new
new Scanner(System.in);
Scanner(System.in);
System.out.println("How
System.out.println("How much
much money
money will
will you
you contribute
contribute every
every year?
year? ");
");
double
payment
=
in.nextDouble();
double payment = in.nextDouble();
System.out.println("Interest
System.out.println("Interest rate
rate in
in %:
%: ");
");
double
double interestRate
interestRate == in.nextDouble();
in.nextDouble();
double
double balance
balance == 0;
0;
int
year
=
0;
int year = 0;
String
String input;
input;
}}
}}
do
do {{
balance
balance +=
+= payment;
payment;
double
double interest
interest == balance
balance ** interestRate
interestRate // 100;
100;
balance
balance +=
+= interest;
interest;
year++;
year++;
System.out.printf("After
System.out.printf("After year
year %d,
%d, your
your balance
balance is
is %,.2f%n",
%,.2f%n", year,
year, balance);
balance);
System.out.println("Ready
to
retire?
(Y/N)
");
System.out.println("Ready to retire? (Y/N) ");
input
input == in.next();
in.next();
}} while
while (input.equals("N"));
(input.equals("N"));
22
23. Цикл do while
HowHow much
much money
money will
will you
you contribute
contribute every
every year?
year?
100
100
Interest
Interest rate
rate in
in %:
%:
10
10
After
After year
year 1,
1, your
your balance
balance is
is 110.00
110.00
Ready
Ready to
to retire?
retire? (Y/N)
(Y/N)
NN
After
After year
year 2,
2, your
your balance
balance is
is 231.00
231.00
Ready
to
retire?
(Y/N)
Ready to retire? (Y/N)
NN
After
After year
year 3,
3, your
your balance
balance is
is 364.10
364.10
Ready
Ready to
to retire?
retire? (Y/N)
(Y/N)
NN
After
After year
year 4,
4, your
your balance
balance is
is 510.51
510.51
Ready
to
retire?
(Y/N)
Ready to retire? (Y/N)
YY
23
24.
Инструкции перехода24
25. Инструкция break
{{}}
statement_sequence
statement_sequence11
...
...
if
if (condition)
(condition) {{
statement_sequence
statement_sequence22
break;
break;
}}
...
...
statement_sequence
statement_sequence33
25
26. Инструкция break
publicpublic class
class BreakDemo
BreakDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
String[]
String[] people
people == {{ "Tom",
"Tom", "Alice",
"Alice", "Bob",
"Bob", "John",
"John", "Harry",
"Harry", "Don",
"Don", "Tony",
"Tony", "Carol"
"Carol" };
};
for
for (int
(int ii == 0;
0; ii << people.length;
people.length; i++)
i++) {{
System.out.println("Checking
System.out.println("Checking next
next person:
person: "" ++ people[i]);
people[i]);
if
if (people[i].equals("Don"))
(people[i].equals("Don")) {{
System.out.println("Found
System.out.println("Found criminal
criminal Don");
Don");
break;
break;
}}
}}
}}
}}
if
if (people[i].equals("John"))
(people[i].equals("John")) {{
System.out.println("Found
System.out.println("Found criminal
criminal John");
John");
break;
break;
}}
Checking
Checking next
next person:
person:
Checking
Checking next
next person:
person:
Checking
Checking next
next person:
person:
Checking
next
person:
Checking next person:
Found
Found criminal
criminal John
John
Tom
Tom
Alice
Alice
Bob
Bob
John
John
26
27. Инструкция continue
{{statement_sequence
statement_sequence11
...
...
if
if (condition)
(condition) {{
statement_sequence
statement_sequence22
continue;
continue;
}}
}}
...
...
statement_sequence
statement_sequence33
27
28. Инструкция continue
publicpublic class
class ContinueDemo
ContinueDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
String[]
String[] people
people == {{ "Tom",
"Tom", "Alice",
"Alice", "Bob",
"Bob", "John",
"John", "Harry",
"Harry", "Don",
"Don", "Tony",
"Tony", "Carol"
"Carol" };
};
}}
}}
Hello
Hello
Hello
Hello
Hello
Hello
Found
Found
Hello
Hello
Found
Found
Hello
Hello
Hello
Hello
for
for (int
(int ii == 0;
0; ii << people.length;
people.length; i++)
i++) {{
if
if (people[i].equals("Don"))
(people[i].equals("Don")) {{
System.out.println("Found
System.out.println("Found criminal
criminal Don");
Don");
continue;
continue;
}}
if
if (people[i].equals("John"))
(people[i].equals("John")) {{
System.out.println("Found
System.out.println("Found criminal
criminal John");
John");
continue;
continue;
}}
System.out.println("Hello
System.out.println("Hello "" ++ people[i]
people[i] ++ "" !");
!");
}}
Tom
Tom !!
Alice
Alice !!
Bob
Bob !!
criminal
criminal John
John
Harry
Harry !!
criminal
criminal Don
Don
Tony
!
Tony !
Carol
Carol !!
28
29. Инструкция return
...... type
type method{
method{
statement_sequence
statement_sequence11
if
if (condition)
(condition) {{
statement_sequence
statement_sequence22
return
return [value];
[value];
}}
}}
statement_sequence
statement_sequence33
29
30. Инструкция return
publicpublic class
class ReturnDemo
ReturnDemo {{
public
public static
static void
void main(String[]
main(String[] args)
args) {{
}}
}}
String[]
String[] names
names == {{ "Tom",
"Tom", "Alice",
"Alice", "Bob",
"Bob", "John",
"John", "Alex",
"Alex", "Don",
"Don", "Carol"
"Carol" };
};
System.out.println(foundCriminal(names));
System.out.println(foundCriminal(names));
static
static String
String foundCriminal(String[]
foundCriminal(String[] people)
people) {{
for
for (int
(int ii == 0;
0; ii << people.length;
people.length; i++)
i++) {{
if
if (people[i].equals("Don"))
(people[i].equals("Don")) {{
System.out.print("Found
System.out.print("Found criminal
criminal ");
");
return
"Don";
return "Don";
}}
if
if (people[i].equals("John"))
(people[i].equals("John")) {{
System.out.print("Found
System.out.print("Found criminal
criminal ");
");
return
return "John";
"John";
}}
}}
return
return "";
"";
}}
Found
Found criminal
criminal John
John
30