如果/else如果/elseif
-
10-07-2019 - |
题
如果我有一个发言块这样的:
if (/*condition here*/){ }
else{ }
或者像这样:
if (/*condition here*/)
else if (/*condition here*/) {}
else if (/*condition here*/) {}
有什么区别?
这似乎与若/否则,如果部分是真实状态和其他的部分是对于所有其他可能的选择(false)。一个人如果将有助于一些条件。这是我的了解,是否有任何更多的我应该知道的吗?
解决方案
现状一个:
if( condition )
{
}
else
{
}
当在上述语句中的条件为假,则在else块中的语句将总是被执行。
情况B:
if( condition )
{
}
else if( condition2 )
{
}
else
{
}
当“状况”为假,则在else语句如果块将仅当条件2为真执行。 当条件2为假在else块中的语句将被执行。
其他提示
如果没有“elseif的”语法,你会写链if语句处理的几种可能的结果之一是这样的:
if( str == "string1" ) {
//handle first case
} else {
if( str == "string2" ) {
//handle second case
} else {
if( str == "string3" ) {
//handle third case
} else {
//default case
}
}
}
代替可以写
if( str == "string1" ) {
//handle first case
} else if( str == "string2" ) {
//handle second case
} else if( str == "string3" ) {
//handle third case
} else {
//default case
}
这是完全一样的前一个,但看起来更漂亮和更容易阅读
许多语言都有一个语法样本(这里: 写语言规范, ,因此JavaScript):
IfStatement :
if (
表达)
声明else
声明
if (
表达)
声明声明 :
块
VariableStatement
EmptyStatement
ExpressionStatement
IfStatement
IterationStatement
ContinueStatement
BreakStatement
ReturnStatement
WithStatement
LabelledStatement
SwitchStatement
ThrowStatement
TryStatement块 :
{
StatementList选择}
StatementList :
声明
StatementList 声明
这样的分支机构的 ifStatement 可以包含一块的声明(块)或其他报表(其他比 块).这意味着这是有效的:
if (expr)
someStatement;
else
otherStatement;
和为 StatementList 可能只是含有一个单一的声明,这些例子是相当于前:
if (expr) {
someStatement;
} else {
otherStatement;
}
if (expr)
someStatement;
else {
otherStatement;
}
if (expr) {
someStatement;
} else
otherStatement;
当我们的替换 otherStatement
通过一个附加的 IfStatement, 我们得到这个:
if (expr) {
someStatement;
} else
if (expr) {
someOtherStatement;
}
剩下的只是代码格式:
if (expr) {
someStatement;
} else if (expr) {
someOtherStatement;
}
强调什么秋葵说。
此外,如果语言具有真实的elif / ELSIF / ELSEIF(比方说,一个“真正的” elseif的指令,而不是一种嵌套链的通过格式化隐藏起来,),则编译器可以发射伊斯利单个节点在一个抽象语法树(或类似的,请参阅 http://en.wikipedia.org/wiki/Abstract_syntax_tree )而不是嵌套它们。
要举个例子:
说在C / C ++您有:
if (a) {
X
} else if (b) {
Y
} else if (c) {
Z
} else {
0
}
然后,编译器将建立一个像这样的AST-节点:
a
/ \
X b
/ \
Y c
/ \
Z 0
但是,如果所选择的语言有一个真正的if-else:
if (a) {
X
} elif (b) {
Y
} elif (c) {
Z
} else {
0
}
然后,AST可以更容易地看起来像这样:
(a--b--c)
/ / / \
X Y Z 0
在这样的语言中,“如果别的”只会如果括号都不是强制性的是可能的:
if (a) {
X
} elif (b) {
Y
} else if (c) { // syntax error "missing braces" if braces mandatory
Z
} else {
0
}
通讯AST(如果括号都不是强制性的):
(a--b)
/ / \
X Y c
/ \
Z 0
这可能使CFG-分析( http://en.wikipedia.org/wiki/Control_flow_graph )更容易的实施的(虽然有可能没有实际的优化效益;所以恕我直言只希望受益懒惰的程序员:d)
else if
基本上意味着else
的if
部分是另一个<=>语句。
**if/else**
if(condition)
statement;
else
statement;
如果/其他的if / else 强>
if(condition)
{
if(condition)
statement;
else
statement;
}
else if(condition)
{
if(condition)
statement;
else
statement;
}
else
statement;
如果/和其他的if / else如果使用的话也是这样
给定一个变量,将要使用的简单if-else
结构。当有多个变量,你必须为不同的可能性执行不同的路径,你会用if-else if-...-else
。注意,后者还与else
语句结束。
您已经给自己的答案。如果/其他为真/假结果,像是一个int = 2或任何其他可能的int值,并且如果/ elseif的是超过2分的结果,如一个int = 2,并且INT = 3等等。
此外,它基团的变量的情况下。你可以检查每一个结果,如
if (a == 2) { do one thing };
if (a == 3) { do another thing };
...
if (a != 2 && a != 3 ...) { do something else };
使用的if / else / ELSEIF这是更好的可读性。
如果您要检查我们可以用更if..elseif条件。单一的条件那么我们可以如果或者使用其他...。搜索结果 在这里我可以不能够与上传的例子充分说明,所以请经过以下几个环节。搜索结果 if..else语句详情点击 http://allinworld99.blogspot.in/2016 /02/ifelse-flow-chart-with-easy-example.html 结果 如果... ELSEIF详情点击 http://allinworld99.blogspot.in /2016/02/flow-chart-with-example-for-if-then.html
import java.util.*;
public class JavaApplication21 {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.println("You are watching an example of if & else if statements");
int choice, a, b, c, d;
System.out.println(" Enter 1-Addition & 2-Substraction");
int option = obj.nextInt();
switch (option) {
case (1):
System.out.println("how many numbers you want to add.... it can add up to 3 numbers only");
choice = obj.nextInt();
if (choice == 2) {
System.out.println("Enter 1st number");
a = obj.nextInt();
System.out.println("Enter 2nd number");
b = obj.nextInt();
c = a + b;
System.out.println("Answer of adding " + a + " & " + b + " is= " + c);
} else if (choice == 3) {
System.out.println("Enter 1st number");
a = obj.nextInt();
System.out.println("Enter 2nd number");
b = obj.nextInt();
System.out.println("Enter 3rd number");
c = obj.nextInt();
d = a + b + c;
System.out.println("Answer of adding " + a + " , " + b + " & " + c + " is= " + d);
}
case (2):
System.out.println("how many numbers you want to substract.... it can substract up to 3 numbers only");
choice = obj.nextInt();
if (choice == 2) {
System.out.println("Enter 1st number");
a = obj.nextInt();
System.out.println("Enter 2nd number");
b = obj.nextInt();
c = a - b;
System.out.println("Answer of substracting " + a + " & " + b + " is= " + c);
} else if (choice == 3) {
System.out.println("Enter 1st number");
a = obj.nextInt();
System.out.println("Enter 2nd number");
b = obj.nextInt();
System.out.println("Enter 3rd number");
c = obj.nextInt();
d = a - b - c;
System.out.println("Answer of substracting " + a + " , " + b + " & " + c + " is= " + d);
}
default:
System.out.println("no option you have chosen" + option);
}
}
}