Idiot's Note Four: 循环中continue和“if ”的细节注意

<p><span style="color: #ff0000;">先查看代码:</span></p>
<pre name="code" class="java">public class ContinueTesting {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("peter piper picked a peck of pickled peppers");
int x=0;
System.out.println(sb.length());
for (int i=0;i<sb.length();i++){
if(sb.charAt(i)!='p')
continue;
x++;
sb.setCharAt(i, 'O');
}
System.out.println("found "+x+" p's in the string");
System.out.println(sb);
}

}</pre>
<p>?输出结果:</p>
<pre name="code" class="java">44
found 9 p's in the string
Oeter OiOer Oicked a Oeck of Oickled OeOOers</pre>
<p>?</p>
<p><span style="color: #ff0000;">含continue的代码块:</span></p>
<pre name="code" class="java">for (int i=0;i<sb.length();i++){
if(sb.charAt(i)!='p')
continue;
x++;
sb.setCharAt(i, 'O');
}</pre>
<p>?</p>
<p><span style="color: #ff0000;">continue的功能:</span></p>
<p><span style="color: #0000ff;"><strong>终止当前的这轮循环,即跳过continue语句后面的剩余的语句,并计算和判断循环条件,决定是否进入下一轮循环。</strong></span></p>
<p>?</p>
<p><span style="color: #ff0000;">注意:</span></p>
<p><span style="color: #0000ff;"><strong>if(sb.charAt(i)!='p')</strong></span>?后面不接<span style="color: #0000ff;"><strong>{}</strong></span>也是可以通过编译的,默认<span style="color: #0000ff;"><strong>if</strong></span>后面的statement为<span style="color: #0000ff;"><strong>{}</strong></span>内容.. 此处程序相当于</p>
<p><span style="color: #0000ff;"><strong>if(sb.charAt(i)!='p')??{continue;}</strong></span></p>
<p>?</p>
<p>?</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值