流程控制语句
顺序结构
- java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行
- 顺序结构是最简单的算法结构
- 语句与语句之间,框与框之间是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。
public class Demo { //创建demo类
public static void main(String[] args){ //main方法
System.out.println("1"); //顺序结构将从上往下依次进行
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
}
}
分支语句
if语句
一个 if 语句包含一个布尔表达式和一条或多条语句。
单if 语句
/**
if (logic expression){
statments
}
*/
public class Demo {
public static void main(String[] args){
int a = 10 ;
if (a > 1) { //单选择if
System.out.println("a > 1 ") ;
}
}
}
if – else语句
if 和else之间不能有任何其他语句(else if 这样的选择语句除外)
/**
if (logic expression){ // logic expression为布尔类型
statments
}
else{
statments
}
*/
public class Demo {
public static void main(String[] args) {
int a = 10 ;
int b = 5 ;
boolean c = true ;
boolean d = false ;
if (a < b) { //if 语句分支
System.out.print(c);
}
else { // else 语句分支
System.out.println(d);
}
}
}
if – else if – else 语句
/**
if (logic expression){ // logic expression为布尔类型
statments
}
else if (logic expression){
statments
}
else{
statments
}
*/
public class Demo {
public static void main(String[] args) {
int a = 10 ;
int b = 5 ;
int c = 10 ;
boolean d = true ;
boolean e = false ;
if (a == b) { // if分支
System.out.print(d);
}
else if (a == c ) { // else if 分支
System.out.println("a == 10");
}
else { // else 分支
System.out.println(e);
}
}
}
嵌套if语句
/**
if (logic expression){
if (logic expression){
statments
}
}
*/
public class Demo {
public static void main(String[] args){
int a = 9 ;
if (a == 9){ // if语句A
if (a != 10){ // if语句B`
System.out.println("Ture");
}
}
}
}
switch语句
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch 语句中的变量类型可以是: byte、short、int 或者 char。 switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
/**
switch (expression){
case value1 :
statments
break;//用于控制单个选项输出,否则就会输出剩下的语句
case value2 :
statments
break;
default:{ //前面case都没有执行的话运行
statments
}
*/
public class Demo {
public static void main(String[] args) {
int a = 9 ;
switch (a){ //switch语句
case 8 : //case a
System.out.println("a = 8");
break;//用于控制单个选项输出
case 9 : //case b
System.out.println("a = 9");
break;
default :{
System.out.println("a = default");
}
}
}
}
循环语句
while循环语句
while循环特点:先判断是否满足循环条件,然后进入循环
/**
while (条件) {
//语句
}
*/
public class Demo {
public static void main(String[] args) {
int a = 0 ;
while (a < 10) { //while循环,判断变量a 小于零,执行循环
a++;
System.out.println(a);
}
}
}
while循环练习
100以内的奇数和以及偶数和
public class Demo1 {
public static void main(String[] args) {
int x = 0 ;
int x1 = 0 ;
int y1 = 0 ;
while (x<100){
if (x % 2 == 0) { //偶数
x1 = x1 + x ;
}
else {
y1 = y1 + x ; //奇数
}
x++ ;
}
System.out.println("偶数和" + x1);
System.out.println("奇数和" + y1);
}
}
do – while循环语句
do – while 循环特点 :至少循环一次,先执行后判断。先do再while
至少循环一次是do – while语句和while语句主要的差别
/**
do{
//代码
}while(不布尔表达式);
*/
public class Demo {
public static void main(String[] args) {
int a = 0 ;
int b = 0 ;
do{
++a ; //是do - while语句,a将输出10
}while (a<10);
System.out.println(a);
while(a<0); //while语句,直接跳出循环,b为0
}
System.out.println(b);
}
}
do-while 循环练习
100以内的奇数和以及偶数和
public class Demo1 {
public static void main(String[] args) {
int x = 0;
int x1 = 0;
int y1 = 0;
do {
if (x % 2 == 0) {
x1 = x1 + x;
}
else {
y1 = y1 + x;
}
x++;
}while (x < 100) ; //)~100
System.out.println("偶数和" + x1);
System.out.println("奇数和" + y1);
}
}
for循环语句
格式
for(初始化,布尔表达式,迭代){
代码语句
}
/**
for (初始化,布尔表达式,迭代){
//代码语句
}
*/
public class Demo {
public static void main(String[] args) {
for ( int a = 0 ; a<10 ; a++){ //for (初始化,布尔表达式,迭代)
System.out.println(a); //输出a
}
}
}
多变量
public class Demo1 {
public static void main(String[] args) {
for(int a=0,b=0,c=0;a<10 && b<10 && c<10;a++,b++,c++) {
System.out.println(a) ;
System.out.println(b) ;
System.out.println(c) ;
}
}
}
for循环练习
100以内的奇数和以及偶数和
public class Demo1 {
public static void main(String[] args) {
int x1 = 0 ;
int y1 = 0 ;
for (int x = 0;x < 100; x++){
if (x % 2 == 0){
x1 = x1 + x ;
}
}
for (int y = 0 ;y < 100 ;y++){
if (y % 2 == 1){
y1 = y1 + y ;
}
}
System.out.println("偶数和" + x1);
System.out.println("奇数和" + y1);
}
}
嵌套for循环打印九九乘法表
public class TTT {
public static void main(String[] args) {
for (int a = 1;a < 10;a++){
for (int b = 1; b <= a;b++){
System.out.print(b + "X" + a + "=" + a*b + "\t");
}
System.out.println();
}
}
}
增强for循环
格式
for (声明语句 : 表达式){
//语句
}
public class Demo1 {
public static void main(String[] args) {
int [] num = {1,2,3,4,5,6,7,8,9,0} ; //定义数组
for ( int x : num ){ //for (声明语句:表达式)
System.out.println(x);
}
}
}
break 语句
用来终止循环
public class Demo1 {
public static void main(String[] args) {
for (int x = 0 ;x <=100 ;x++){
System.out.println(x);
if (x==10){
break;
}
}
}
}
输出结果:
continue 语句
终止循环,跳到下一个循环
public class Demo1 {
public static void main(String[] args) {
for (int x = 0 ;x <=11 ;x++){
if (x %10 == 0){
System.out.println("ZZZ");//x被10整除的时候输出,并且跳出if。进入循环
continue;
}
System.out.println(x);
}
}
}
输出结果: