本文介绍了如果...其他快速问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我很惊讶地发现Else if在下面的代码位中不需要。 显然是如果和Else if和Else if可互换使用。如果我错了,请纠正我,但它会出现B $ b,Else只是一种风格选择。 for(int i = 1; i< = 100; i ++) { 如果(i == 3) 继续; 如果(i == 5) 休息; Console.WriteLine(i); } //编译得很好,我可以使用这个或else if告诉没有区别在第二个if 声明中。 提前致谢, 杰克 - --------------------------------- --- - - 发布于NewsLeecher v3.9 Beta 5 Web @ http://www.newsleecher.com/?usenet ------------------- ----- - --- - - 解决方案 在你的例子中它是相同的,因为继续;无论如何都将跳回到回路的顶部 ,所以第二个if不会被处理。 但是,一般来说,它们并不相同。考虑: int i = 5; if(i if(i == 5)Console.WriteLine(" equals 5"); 这将打印两行文本,因为每个文本都是如果"处理 独立。 如果你添加else到第二行,然后第二条消息将永远不会打印出来,因为每当第二个条件为真时,第一个条件为真,因为 井,*和*第二个条件仅在else - case中执行,当 时,第一个不是。 Eq。 仅在跳过第二个if或者条件的情况下 不包括没有差异的第一个条件。 当你使用时继续跳过一些代码,它相当于 这段代码: for(int i = 1; i if(i == 3){ //什么都不做 }否则{ 如果(i == 5){ 休息; } Console.WriteLine(i ); } } 请注意,如果不存在,则不存在任何其他构造。它只是一个包含if的其他 。当你这样写的时候: if(something){ ... }否则if(something){ b $ b ... } 实际上是: 如果(某事){ ... }否则{ if(something){ ... } } - G?跑Andersson _____ http://www.guffa.com 你在那里有两个独立且完整的if语句。 也可能是这样的。 如果(i == 3) { 继续; } else { 刹车; '除了3 }之外的其他任何东西} --------- if(i == 3 ) { 继续; } elseif(i 0&& i< 2) { 刹车; } elseif(其他东西) { 做点什么; } I was just surprised to find that "Else if" is not required in the code bit below.Apparently "If" and "Else if" are used interchangeably. Please correct me if I''m wrong, but itappears "Else" is just a stylistic choice.for (int i = 1; i <= 100; i++){if (i == 3)continue;if (i == 5)break;Console.WriteLine(i);}//Compiles just fine and I can tell no difference using this or "else if" in the second ifstatement.Thanks in advance,Jack----------------------------------- --- -- -Posted with NewsLeecher v3.9 Beta 5Web @ http://www.newsleecher.com/?usenet------------------- ----- ---- -- - 解决方案It''s the same in your example, because "continue;" will jump back to the topof the loop anyway, so the second "if" won''t be processed.In general, however, they are not the same. Consider:int i = 5;if (i <= 5) Console.WriteLine("less than or equal to 5");if (i == 5) Console.WriteLine("equals 5");This will print both lines of text, since each "if" is processedindependently.If you add "else" to the second line, then the second message will never beprinted, because whenever the second condition is true, the first is true aswell, *and* the second condition is only performed in the "else"-case, whenthe first was not.Eq.It''s only in situations where the second if is skipped or it''s conditionis exclusive of the first condition that there is no difference.As you are using continue to skip some of the code, it''s equivalent tothis code:for (int i = 1; i <= 100; i++) {if (i == 3) {// do nothing} else {if (i == 5) {break;}Console.WriteLine(i);}}Note that there doesn''t exist any "else if" construct. It''s just an elsecontaining an if. When you write like this:if (something) {...} else if (something) {...}it''s actually:if (something) {...} else {if (something) {...}}--G?ran Andersson_____ http://www.guffa.comYou got two independent and complete if statements up there above there.It could be something like this too.if (i==3){continue;}else{brake; ''anything else but 3}---------if (i == 3){continue;}elseif ( i 0 && i < 2){brake;}elseif ( something else ){do something;} 这篇关于如果...其他快速问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 18:21
查看更多