Groovy 学习之四:运算符和循环、条件语句

一、Groovy运算符

运算符是一个符号,通知编译器执行特定的数学或逻辑操作。

Groovy中有以下类型的运算符:

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 位运算符
  • 赋值运算符

1、算术运算符

Groovy语言支持正常的算术运算符任何语言。以下是在Groovy中可用的算术运算符:

运算符 描述 例子
+ 两个操作数的加法 1 + 2 将得到 3
从第一个操作数中减去第二个操作数 2 - 1 将得到 1
* 两个操作数的乘法 2 * 2 将得到 4
/ 分子除以分母 3 / 2 将得到 1.5
% 模数运算符和整数/浮点除法后的余数 3%2 将得到 1
++ 用于将操作数的值增加1的增量运算符

int x = 5;

x++;

x 将得到 6

- - 用于将操作数的值减1的增量运算符

int x = 5;

X - ;

x 将得到 4

 以下代码段显示了如何使用各种运算符。

class demo4 {
    static void main(String[] args) {
        // Initializing 3 variables
        def x = 5;
        def y = 10;
        def z = 8;

        //Performing addition of 2 operands
        println(x+y);

        //Subtracts second operand from the first
        println(x-y);

        //Multiplication of both operands
        println(x*y);

        //Division of numerator by denominator
        println(z/x);

        //Modulus Operator and remainder of after an integer/float division
        println(z%x);

        //Incremental operator
        println(x++);

        //Decrementing operator
        println(x--);
    }
}

当我们运行上面的程序,我们将得到以下结果。可以看出,结果如从上面所示的操作符的描述所预期的。

15 
-5 
50 
1.6 
3 
5 
6

2、关系运算符

关系运算符允许对象的比较。以下书在Groovy中可用的关系运算符。

运算符 描述 例子
== 测试两个对象之间的等同性 2 == 2将得到true
!= 测试两个对象之间的差异 3!= 2将得到true
< 检查左对象是否小于正确的操作数。 2 <  3将得到true
<= 检查左对象是否小于或等于右操作数。 2 <  3将得到true
> 检查左对象是否大于右操作数。 3 > 2将得到true
>= 检查左对象是否大于或等于右操作数。 3 = 2将得到true

 以下代码段显示了如何使用各种运算符。

class demo5 { 
   static void main(String[] args) { 
      def x = 5;
      def y = 10;
      def z = 8;
		
      if(x == y) { 
         println("x is equal to y"); 
      } else 
         println("x is not equal to y"); 
			
      if(z != y) { 
         println("z is not equal to y"); 
      } else 
         println("z is equal to y"); 
				
      if(z != y) { 
         println("z is not equal to y"); 
      } else 
         println("z is equal to y"); 
					
      if(z<y) { 
         println("z is less than y"); 
      } else 
         println("z is greater than y"); 
						
      if(x<=y) { 
         println("x is less than y"); 
      } else 
         println("x is greater than y"); 
			
      if(x>y) { 
         println("x is greater than y"); 
      } else 
         println("x is less than y"); 
			
      if(x>=y) { 
         println("x is greater or equal to y"); 
      } else 
         println("x is less than y"); 
   } 
} 

当我们运行上面的程序,我们将得到以下结果。可以看出,结果如从上面所示的操作符的描述所预期的。

x is not equal to y 
z is not equal to y 
z is not equal to y 
z is less than y
x is less than y 
x is less than y 
x is less than y 

3、逻辑运算符

逻辑运算符用于计算布尔表达式。以下是在Groovy中提供的逻辑运算符。

运算符 描述 例子
&& 这是逻辑“和”运算符 true && true 将得到 true
|| 这是逻辑“或”运算符 true || true 将得到 true
! 这是逻辑“非”运算符 !false 将得到 true

 以下代码段显示了如何使用各种运算符。

class demo6 {
    static void main(String[] args) {
        boolean x = true;
        boolean y = false;
        boolean z = true;

        println(x&&y);
        println(x&&z);

        prin
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值