728x90
반응형
Boolean can only be either true or false. Do not use double quotes.
&& = and, || = or
Control flow
class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
if(condition) {
//Run this code;
} else if (condition) {
//Run this code;
} else {
//Run this code;
}
}
}
|
cs |
Switch statements.
class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
switch (value of contidion) {
case value1 :
//Run this code;
break;
case value2 :
//Run this code;
break;
case value3 :
//Run this code;
break;
}
}
}
|
cs |
Default case
In switch statements, you can set a default case for when none of the cases match.
default :
//Run this code;
break;
}
|
cs |
While loops
class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
while (condition) {
//Run this code;
}
}
}
|
cs |
for loops
class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
for (int i=1; i<=5 ; i++) {
//Run this code;
}
}
}
|
cs |
Using continue : continue statements skip the loop
ex ))
class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
for (int i=1; i<=10 ; i++) {
if ( i % 3 == 0) {
continue; //skip the rest of the code in the loop
//if the number is divisible by 3
}
System.out.println(i);
}
}
}
|
cs |
Arrays [ ]
When declaring a variable for an array, you have to put [ ] after the type of each element.
ex ) int [ ] - array of integers, int [ ] numbers = { 5, 13, 29 };
String [ ] - array of strings
Array has a method called length, which counts the number of elements in an array.
ex ) String [ ] names = { "Jones", "Ken" };
System.out.println( names.length ); -> 2
Enhanced for loops
class Main {
public static void main(String[] args) {
System.out.println("Hello Java");
for ( dataType variableName : arrayName ) {
//Run this code;
}
ex)
String [] names = {"John", "Ken"};
for (String name : names) {
System.out.println( name );
}
}
}
|
cs |
출처 : progate.com/dashboard
728x90
반응형