基本结构

顺序结构

​ 从上到下,顺序执行


选择结构

单if语句

​ if(条件表达式){语句块}

1
2
3
4
5
6
7
8
9
public class test010 {
public static void main(String[] args) {
int a = 10,b = 11;
if(a < b)
{
System.out.println(a);
}
}
}

标准if else语句

​ if(条件表达式){语句块;}else{语句块}

1
2
3
4
5
6
7
8
9
10
11
public class test010 {
public static void main(String[] args) {
int a = 10,b = 11;
if(a < b)
{
System.out.println(a);
}else{
System.out.println(b);
}
}
}

多层if语句

​ if(条件表达式){语句块;}else if(条件表达式){语句块}else{语句块}

1
2
3
4
5
6
7
8
9
10
11
12
13
public class test010 {
public static void main(String[] args) {
int a = 10,b = 11,c =12;
if(a > b && a > c)
{
System.out.println(a);
}else if(b > c && b > a){
System.out.println(b);
}else{
System.out.println(c);
}
}
}

switch

​ 选择对应于的常量,并执行对应常量的语句
​ switch(表达式)
{
​ case 常量1:
​ 语句;
​ break;
​ case 常量2:
​ 语句;
​ break;
​ default:
​ 语句;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class test010 {
public static void main(String[] args) {
char c = 'A';

switch(c)
{
case 'A':
System.out.println("X>=90");
break;
case 'B':
System.out.println("80>=X<90");
break;
default:
System.out.println("X<79");
}
}
}
常量类型
整型

​ byte,char,short,int

引用数据类型

​ String,enum
​ 常量值不能重复


循环结构

for循环语句

for(变量初始化;条件判断;改变变量值)
{
语句块;
}

1
2
3
4
5
6
7
8
public class test010 {
public static void main(String[] args) {
for(int i = 0;i < 10;i++)
{
System.out.println(i);
}
}
}

while循环语句

while(条件)
{
语句块;
}

1
2
3
4
5
6
7
8
9
10
public class test010 {
public static void main(String[] args) {
int i = 0;
while(i < 10)
{
System.out.println(i);
i++;
}
}
}

do while循环语句

初始化表达式
do{

​ 语句块;

}while(条件表达式);

1
2
3
4
5
6
7
8
9
public class test010 {
public static void main(String[] args) {
int i = 0;
do{
System.out.println(i);

}while(i++ < 10);
}
}

break

​ 中止循环

continue

​ 结束本次循环

死循环

​ java可以有死循环

1
2
3
4
5
6
7
8
public class test010 {
public static void main(String[] args) {
while(1)
{
//我是死循环
}
}
}

循环嵌套

​ 一个循环体嵌套着另一个循环体这就叫做循环嵌套

1
2
3
4
5
6
7
8
9
10
11
12
public class test010 {
public static void main(String[] args) {
for(int i = 0;i < 10;i++)
{
for(int j = 0;j < 10;j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}