java学习笔记(三) -- 判断 循环语句

一)判断语句

if语句(顺序执行的判断语句)

只有if

class ifDemo {
    
    public static void main(String[] args) {
        
        int x = 3;
        if (x <= 3) {
            System.out.println("true");
        } 
    }
}

 
 

if-else

class ifDemo {
    
    public static void main(String[] args) {
        
        int x = 3;
        if (x > 2) {
            System.out.println("true");
        } else {
            System.out.println("false"); 
        }
    }
}


if - else if  - else 

class ifDemo {
    
    public static void main(String[] args) {
        
        int x = 3;
        if (x == 1) {
            System.out.println("false");
        } else if (x == 2){
            System.out.println("false"); 
        } else {
            System.out.println("true");
        }
    }
}

switch语句

break表示跳出整个switch语句。

break是防止执行到下一条语句。

default是上记case以外的场合。

switch对应的数据类型,只能是 byte short int char,以外的数据类型,需要用 if。

oper可以是表达式,也可以是变量。 case 对应的表达式和变量的对应的比较结果。

public class T {
	// 完成一个四则运算的功能
	public static void main(String args[]){
		int x = 3 ;
		int y = 6 ;
		char oper = '+' ;
		switch(oper){
			case '+':{	// 执行加法操作
				System.out.println("x + y = " + (x + y )) ;
				break ;
			}
			case '-':{	// 执行减法操作
				System.out.println("x - y = " + (x - y )) ;
				break ;
			}
			case '*':{	// 执行乘法操作
				System.out.println("x * y = " + (x * y )) ;
				break ;
			}
			case '/':{	// 执行除法操作
				System.out.println("x / y = " + (x / y )) ;
				break ;
			}
			default:{
				System.out.println("未知的操作!") ;
				break ;
			}
		}
	}
}

二)循环语句

while 和 do - while

不知道循环次数的时候


while循环语句:如果判断条件x <= 10 是false的时候,一次循环也不执行

public class T {
	// 完成一个四则运算的功能
	public static void main(String args[]){
		int x = 1; 
		int sum = 0 ;	// 保存累加的结果
		while(x<=10){ // 循环条件
			sum += x ;	// 进行累加操作
			x++ ;		// 修改循环条件
		}
		System.out.println("1 --> 10 累加的结果为:" + sum) ;
	}
}

 

do - while 循环语句 无论whie的判断条件是否正确,循环都至少会执行一次。

public class T {
	public static void main(String args[]){
		int x = 1; 
		int sum = 0 ;	// 保存累加的结果
		do{
			sum += x ;	// 执行累加操作

			x++ ;           // 循环条件变更
		}while(x<=10) ;         // 循环条件判断

		System.out.println("1 --> 10 累加的结果为:" + sum) ;
	}
}

for:

已知循环次数的时候

public class T {
	public static void main(String args[]){
		int sum = 0 ;	// 保存累加的结果
                
                // 给变量赋初值;判断条件;赋值增减量
		for(int x=1;x<=10;x++){
			sum += x ;
		}
		System.out.println("1 --> 10 累加的结果为:" + sum) ;
	}
}


foreach:一种更为简洁的for循环,用于数组和容器。

public class ForeachFloat {
	public static void main(String[] args) {
		Random rand = new Random(47);

		float f[] = new float[10];

		for (int i = 0; i < 10; i++) {
			f[i] = rand.float[10];
		 }

		for (float x : f) {
			System.out.println(x);
		 }
	 }
 }


while(true) 和for(;;)都是无限循环。对于编译器来说都是一回事,怎么写根据个人习惯。

可以通过break,return,continue来中断循环。


三)break 和 continue 

break

命令可以终止循环的运行,然后执行循环之外的代码

public class T {
	public static void main(String args[]){
		int sum = 0 ;	// 保存累加的结果
		for(int x=1;x<=10;x++){
                        if (x == 5) {
                           break;
                        }
			sum += x ;
		}
		System.out.println("1 --> 10 累加的结果为:" + sum) ;
	}
}

1 --> 10 累加的结果为:10


continue

命令可以跳过当前循环,执行下次循环。

public class T {
	public static void main(String args[]){
		int sum = 0 ;	// 保存累加的结果
		for(int x=1;x<=10;x++){
                        if (x == 5) {
                           continue;
                        }
			sum += x ;
		}
		System.out.println("1 --> 10 累加的结果为:" + sum) ;
	}
}

1 --> 10 累加的结果为:50

全篇内容转自

http://blog.csdn.net/itmyhome1990/article/details/8918564

http://blog.csdn.net/qinqinnibaobaoni/article/details/8540124


个人补充了一下部分。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值