在Java中为for循环赋予多个条件 [英] Giving multiple conditions in for loop in Java

查看:977
本文介绍了在Java中为for循环赋予多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搜索如何在for循环中提供多个条件?但是没有给出直接答案。

I was searching for "How to give multiple conditions in a for loop?" But there are no direct answers given.

经过一番研究,我找到了正确的方法。条件不应以逗号(,)或分号(;)分隔。我们可以使用&&运算符一起加入两个条件。

After some research I found the correct way. Conditions should not be comma(,) or semicolon(;) separated. We can use the && operator to join both the conditions together.

for(初始化; condition1&& condition2; increment)

for( initialization ; condition1 && condition2 ; increment)

示例:

for(int j= 0; j < 6 && j < ((int)abc[j] & 0xff) ; j++ ) 
{
//
}

希望这有助于新的Java开发人员。

Hope this helps the new Java developers.

推荐答案

你也可以使用或运算符,

You can also use "or" operator,

for( int i = 0 ; i < 100 || someOtherCondition() ; i++ ) {
  ...
}

这篇关于在Java中为for循环赋予多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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