程序逻辑控制
一、程序逻辑
程序逻辑是编程语言重要组成部分,Java的结构右三种:顺序结构、选择结构、循环结构。
1、顺序结构:自上而下逐行执行,从开头到末尾。
2、选择结构:根据判断条件的的成立与否在觉得要执行那些语句。
3、循环结构:更具判断条件的成立与否,决定程序循环的次数,而程序的段落称为循环主体
二、分支结构
1.if分支结构
if分支结构主要判断是否执行某段代码,Java中使用if……else可以组成三种形式:
if判断
public class ji {
public static void main(String[] args) {
int a = 9;
if(a<9){
System.out.println(a);
}
}
}
程序输出结果:
不输出
if……else判断
public class ji {
public static void main(String[] args) {
int a = 9;
if(a<9){
System.out.println(a);
}else {
System.out.println("空");
}
}
}
程序输出结果:
空
多条件判断:
public class ji {
public static void main(String[] args) {
int a = 9;
if(a<9){
System.out.println(a);
}else if(a==9){
System.out.println(a);
}else {
System.out.println("空");
}
}
}
程序输出结果:
9
2.switch开关语句
swich是一个开关语句,它主要更具内容进行判断,需要注意的是,switch语句只能判断数据(int、char、枚举、String),而不能用布尔表达式判断。
范例:switch语句的使用
public class ji {
public static void main(String[] args) {
int a = 10; //定义变量
switch (a){ //进行判断
case 1: //匹配内容
System.out.println("Hello");
case 2:
System.out.println("world");
default: //匹配不成功输出
System.out.println("Java");
}
}
}
程序输出结果:
Java
注意:if可以判断布尔表达式,而switch只能判断内容
break:语句中断其余case执行
范例:switch和berak使用
public class ji {
public static void main(String[] args) {
int a = 1; //定义变量
switch (a){ //进行判断
case 1: //匹配内容
System.out.println("Hello");
break;
case 2:
System.out.println("world");
break;
default: //匹配不成功输出
System.out.println("Java");
break;
}
}
}
程序输出结果:
Hello
三、循环结构
Java的循环结构分为两种:while循环和for循环。
1、while循环结构
使用两种循环来算出1~100的和
1、while循环
public class ji {
public static void main(String[] args) {
int a = 0; //最后的计算总和
int sum = 1; //进行循环控制
while (sum <= 100){ //判断条件
a +=sum; //数字累加
sum++; //修改循环条件
}
System.out.println(a);
}
}
程序输出结果:
5050
2、do……while循环
public class ji {
public static void main(String[] args) {
int sum = 0; //保存最终计算总和
int num = 1; //进行循环控制
do { //限制性循环体
sum += num; //累加
num++; //修改条件
}while (num <= 100);
System.out.println(sum);
}
}
程序输出结果:
5050
2、for循环结构
在知道循环次数的条件下,可以使用for循环来实现循环结构
1、使用for循环实现1~100的累加
public class ji {
public static void main(String[] args) {
int sum = 0;
for (int i=1;i<=100;i++){
sum +=i; //循环体实现累加操作
}
System.out.println(sum); //累加输出
}
}
程序输出结果:
5050
注意:主要使用while和for循环
while循环:不确定循环次数,但确定循环条件的情况下使用。
for循环:在确定循环次数下使用。
四、循环控制语句
在循环结构中只要满足循环条件,循环体代码会一直执行,程序也提供两个循环停止语句:continue(退出本次循环)、break(退出整个循环)。
范例:使用continue循环
public class ji {
public static void main(String[] args) {
for (int x=0;x<10;x++){
if (x==7){ //循环判断
continue; //结束本次循环,后续代码不执行
}
System.out.print(x + "、"); //循环输出内容
}
}
}
程序输出结果:
0、1、2、3、4、5、6、8、9、
发现缺少了7但是后面照常执行
范例:使用break循环
public class ji {
public static void main(String[] args) {
for (int x=0;x<10;x++){
if (x==7){ //循环判断
break; //结束全部循环
}
System.out.print(x + "、"); //循环输出内容
}
}
}
程序输出结果:
0、1、2、3、4、5、6、
发现7和7之后的都不执行,程序直接结束
五、循环嵌套
循环结构可以在内部嵌入多个循环结构
范例:打印99乘法表
public class ji {
public static void main(String[] args) {
for (int i=1;i<=9;i++){ //外部循环
for (int j=1;j<=i;j++){ // 内部循环
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}
程序输出结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
概要
1、if语句可以判断结果来决定流程
2、选择结构包含:if、if……else及switch语句
3、需要重复某项功能,最好使用循环结构,可以使用for、while及do……while循环
4、break语句可以强制程序逃离循环。
5、continue语句可以强制程序跳到循环的起始处。