三元运算符和增量运算符 [英] Ternary operator and increment operator

查看:92
本文介绍了三元运算符和增量运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不满足条件,这是否是重置计数器的有效/合理方法?这是我能想到的最紧凑的方法.

  int计数器= 0;int a,b;//执行..并分配a和b计数器=((a< b)?计数器++:0); 

解决方案

您已经分配给计数器,因此也不要使用 ++ .

>

  counter =条件?(计数器+ 1):0; 

Is this a valid / sound way of resetting a counter if a condition is not met? It is the most compact way I could think of.

int counter = 0;
int a,b;

// Do .. and assign a and b 

counter = ((a<b) ? counter++ : 0); 

解决方案

You are already assigning to counter, so don't use ++ as well.

counter = condition ? (counter + 1) : 0;

这篇关于三元运算符和增量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆