Java , C++.
, Java.
if-else
:
if ( ) 1; [ else 2;]
else . , . , boolean.
int bytesAvailable;
// ...
if (bytesAvailable > 0) {
ProcessData();
bytesAvailable -= n;
} else
waitForMoreData();
, , , if-else.
class IfElse {
public static void main(String args[]) { int month = 4;
String season;
if (month == 12 || month == 1 || month == 2) {
season = "Winter";
} else if (month ==3 || month == 4 || month == 5) {
season = "Spring";
} else if (month == 6 || month == 7 || month == 8) {
season = "Summer";
} else if (month == 9 || month == 10 || month == 11) {
season = "Autumn";
} else {
season = "Bogus Month";
}
System.out.println( "April is in the " + season + ".");
} }
:
: \> java IfElse
April is in the Spring.
break
Java goto. , goto, Java break. , , . Java . break switch . .
, , . break, , , b. println.
class Break {
public static void main(String args[]) { boolean t = true;
a: { b: { c: {
System.out.println("Before the break"); // break
if (t)
break b;
System.out.println("This won't execute"); // He }
System.out.println("This won't execute"); // He }
System.out.println("This is after b"); // b
} } }
:
Before the break
This is after b
break . break goto , .
switch
switch . :
switch ( ) { case 1:
break;
case 2:
break;
case :
break;
default:
}
, , case, switch. . case , .
case, , default. , default . , case switch default , switch.
switch ( , ) break , switch. break , case . switch case, break.
class SwitchSeason { public static void main(String args[]) {
int month = 4;
String season;
switch (month) {
case 12: // FALLSTHROUGH
case 1: // FALLSTHROUGH
case 2:
season = "Winter";
break;
case 3: // FALLSTHROUGH
case 4: // FALLSTHROUGH
case 5:
season = "Spring";
break;
case 6: // FALLSTHROUGH
case 7: // FALLSTHROUGH
case 8:
season = "Summer";
break;
case 9: // FALLSTHROUGH
case 10: // FALLSTHROUGH
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
System.out.println("April is in the " + season + ".");
} }
, switch . , .
class WordCount {
static String text = "Now is the tifne\n" +
"for all good men\n" +
"to come to the aid\n" +
"of their country\n"+
"and pay their due taxes\n";
static int len = text.length();
public static void main(String args[]) {
boolean inWord = false;
int numChars = 0;
int numWords = 0;
int numLines = 0;
for (int i=0; i < len; i++) {
char = text.charAt(i);
numChars++;
switch () {
case '\n': numLines++; // FALLSTHROUGH
case '\t': // FALLSTHROUGH
case ' ' : if (inWord) {
numWords++;
inWord = false;
}
break;
default: inWord = true;
}
}
System.out.println("\t" + numLines +"\t" + numWords + "\t" + numChars);
, . 9.
return
, Java , . main, -. return, , . , return , Java.
class ReturnDemo {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return"); // return
if (t) return;
System.out.println("This won't execute"); //
} }
if (t)? , , Java , println . Java , if .
4 , , . Java : while ( -), do-while ( -) for ( ).
while
, true. while:
[ ; ]
while ( ) {
;
[;] }
. while tick.
class WhileDemo {
public static void main(String args[]) {
int n = 10;
while (n > 0) {
System.out.println("tick " + n);
n--;
}
} }
do-while
, false. Java do-while. :
[ ; ] do { ; [;] } while ( );
. :
class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
} while (--n > 0);
} }
for
. for.
for ( ; ; ) ;
, for, while, . , , . for .
class ForDemo {
public static void main(String args[]) {
for (int i = 1; i <= 10; i++)
System.out.println("i = " + i);
} }
, .
class ForTick {
public static void main(String args[]) {
for (int n = 10; n > 0; n--)
System.out.println("tick " + n);
} }
for. , for, .
, for.
class Months {
static String months[] = {
"January", "February", March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
static int monthdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static String spring = "spring";
static String summer = "summer";
static String autumn = "autumn";
static String winter = "winter";
static String seasons[] = { winter, winter, spring, spring, spring, summer, summer, summer, autumn, autumn, autumn, winter };
public static void main(String args[]) {
for (int month = 0; month < 12; month++) {
System.out.println(months[month] + " is a " +
seasons[month] + " month with " + monthdays[month] + " days.");
} } }
:
January is a winter month with 31 days.
February is a winter month with 28 days.
March is a spring month with 31 days.
April is a spring month with 30 days.
May is a spring month with 31 days.
June is a summer month with 30 days.
July is a summer month with 31 days.
August is a summer month with 31 days.
September is a autumn month with 30 days.
October is a autumn month with 31 days.
November is a autumn month with 30 days.
December a winter month with 31 days.
, for . for , Java . (,) for. for, .
class Comma {
public static void main(String args[]) {
int a, b;
for (a = 1, b = 4; a < b; a++, b--) {
System.out.println("a = " + a);
System.out.println("b = " + b);
}
} }
, .
= 1
b = 4
= 2
b = 3
continue
, , . Java continue. , continue , .
class ContinueDemo {
public static void main(String args[]) {
for (int i=0; i < 10; i++) {
System.out.print(i + " ");
if (i % 2 == 0) continue;
System.out.println("");
}
}}
, . :
: \> java ContinueDemo
0 1
2 3
4 5
5 7
8 9
break, continue , , . , continue 0 9:
class ContinueLabel {
public static void main(String args[]) {
outer: for (int i=0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j > i) {
System.out.println("");
continue outer;
}
System.out.print(" " + (i * j));
}
}
}}
continue j i. :
:\> Java ContinueLabel
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
Java . try, catch, throw finally. 10 .
Java . , . Java . , , - .
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |