if,else和else之间有什么区别?

我试图辨别它们之间的区别

if else else if 

你什么时候使用它们什么时候不使用?

我有一个大量实例的家庭作业,由于不知道每个实例之间的差异,我遇到了代码错误。

有人可以定义如何使用这些?

 **IF** you are confused read the c# spec **ELSE IF** you are kind of confused read some books **ELSE** everything should be OK. 

🙂

if语句遵循这种结构:

 if (condition) { // executed only if "condition" is true } else if (other condition) { // executed only if "condition" was false and "other condition" is true } else { // executed only if both "condition" and "other condition" were false } 

if部分是唯一绝对强制的块。 else if允许你说“好的话,如果前一个条件不成立,那么如果这个条件是真的……”。 否则说“如果以上条件都不属实……”

else if块, else if可以有多个else if ,但只有一个if块,只有一个(或零) else块。

If-elseif-else可以写成嵌套的if-else。 这些(逻辑上讲)是等价的:

 if (A) { doA(); } else if (B) { doB(); } else if (C) { doC(); } else { doX(); } 

是相同的:

 if (A) { doA(); } else { if (B) { doB(); } else { if (C) { doC(); } else { doX(); } } } 

结果是最终只会评估doXdoBdoCdoX

if,else和else if是否所有构造都有助于“分支”代码。 基本上,只要您想做出决定,就可以使用它们。

一个例子是’如果天气晴朗,我会去外面。 否则,我会呆在里面’

在代码中(忽略额外的东西)

 if (sunny) { goOutside(); } else { stayInside(); } 

如果要添加“附加”条件,可以使用“else if”语句。 扩展前面的例子,“如果天气晴朗,我会去外面。如果暴风雨,我会进入地下室,否则我会呆在里面”

在代码中

 if (sunny) { goOutside(); } else if (stormy) { goDownstairs(); } else { stayInside(); } 

编辑部分:

以下是如何编写多个ifs和条件的方法。 以下示例至少可以用两种方式编写:

‘如果阳光明媚,温暖,就到外面去吧。 如果天气晴朗,冷漠,什么也不做

 if (sunny) { if (warm) { goOutside(); } else if (cold) { doNothing(); } } 

要么

 if (sunny && warm) { goOutside(); } else if (sunny && cold) { doNothing(); } 

没有“ else if ”。 你有以下几点:

 if (condition) statement or block 

要么:

 if (condition) statement or block else statement or block 

在第一种情况下,如果条件为真(不同于0),则执行语句或块。 在第二种情况下,如果条件为真,则执行第一个语句或块,否则执行第二个语句或块。

因此,当您编写“ else if ”时,这是一个“ else statement ”,其中第二个语句是if语句。 如果您尝试这样做,可能会遇到问题:

 if (condition) if (condition) statement or block else statement or block 

这里的问题是你希望“ else ”引用第一个“ if ”,但你实际上指的是第二个。 你通过这样做解决这个问题

 if (condition) { if (condition) statement or block } else statement or block 

死简单伪代码说明:

 /* If Example */ if(condition_is_true){ do_this } now_do_this_regardless_of_whether_condition_was_true_or_false /* If-Else Example */ if(condition_is_true){ do_this }else{ do_this_if_condition_was_false } now_do_this_regardless_of_whether_condition_was_true_or_false /* If-ElseIf-Else Example */ if(condition_is_true){ do_this }else if(different_condition_is_true){ do_this_only_if_first_condition_was_false_and_different_condition_was_true }else{ do_this_only_if_neither_condition_was_true } now_do_this_regardless_of_whether_condition_was_true_or_false 

我认为将“其他”视为“另外”一词是有帮助的。

所以你会这样读:

 if (something is true) { // do stuff } otherwise if (some other thing is true) { // do some stuff } otherwise { // do some other stuff :) } 
 if (condition) { thingsToDo().. } else if (condition2) { thingsToDoInTheSecondCase().. } else { thingsToDoInOtherCase().. } 

他们的意思正是他们在英语中的意思。

如果条件为真,则执行某些操作, ELSE (否则)如果另一个条件为真,则执行某些操作,当其他所有操作都失败时,执行此操作。

请注意,没有其他if构造具体,只是ifelse ,但语法允许你放置其他和如果在一起,并且约定不是在你做的时将它们嵌套得更深。 例如:

 if( x ) { ... } else if( y ) { ... } else { ... } 

在语法上与以下内容相同:

 if( x ) { ... } else { if( y ) { ... } else { ... } } 

两种情况下的语法是:

 if ** else ** 

如果它本身就是一个语句,那么语法本身就支持使用else if

 if (numOptions == 1) return "if"; else if (numOptions > 2) return "else if"; else return "else"; 

if语句的语法是

 if(condition) something; // executed, when condition is true else otherthing; // otherwise this part is executed 

所以,基本上, elseif构造的一部分( 某些东西其他 东西通常是{}包含的复合语句,而else部分实际上是可选的)。 else if是两个if的组合,其中othert是一个if本身。

 if(condition1) something; else if(condition2) otherthing; else totallydifferenthing; 

if语句使用逻辑表达式的结果来确定是否将执行两个代码块之一。

有了这段代码

 if (logical expression) { code block 1; } else { code block 2; } 

如果逻辑表达式为真,则只执行代码块1中的语句; 如果为false,则仅为代码块2中的语句。

如果要进行多个类似的测试(例如,如果我们测试的数字小于零,等于零或大于零)那么第二个测试可以作为else代码的第一个语句放置块。

 if (logical expression 1) { code block 1; } else { if (logical expression 2) { code block 2; } else { code block 3; } } 

在这种情况下,如果逻辑表达式1为真,则执行代码块1; 如果逻辑表达式1为假且逻辑表达式2为真,则代码块2; 如果两个逻辑表达式都为假,则代码块3。

显然,这可以用另一个if语句作为代码块3的第一个语句重复。

else if语句只是该代码的重新格式化版本。

 if (logical expression 1) { code block 1; } else if (logical expression 2) { code block 2; } else { code block 3; } 

else if可以与’if’和’else’一起使用,以进一步细分逻辑

 //if less than zero if( myInt < 0){ //do something }else if( myInt > 0 && myInt < 10){ //else if between 0 and 10 //do something }else{ //else all others //do something } 

这些是大多数编程语言中的基本决策顺序; 它可以帮助您确定您的程序将要执行的操作流程。 if告诉编译器你有问题,问题是括号之间的条件

 if (condition) { thingsToDo().. } 

else部分是对此结构的补充,用于告诉编译器如果条件为false则该怎么做

 if (condition) { thingsToDo().. } else { thingsToDoInOtherCase().. } 

如果第一个条件为假,但你想在做出另一个问题之前决定做什么,你可以将它们组合起来形成一个else

 if (condition) { thingsToDo().. } else if (condition2) { thingsToDoInTheSecondCase().. }else { thingsToDoInOtherCase().. } 

如果,如果两者都用于测试条件。

我以If和其他为例

在if case编译器中检查所有情况是否为真或假。 如果没有一个块执行那么其他部分将被执行。

在else的情况下,如果编译器在获得false值时停止程序流。 它不会读取整个程序。如果我们使用其他更好的性能。

但两者都根据情况具有重要性

我以foor订购菜单为例,如果我使用其他,那么它很适合,因为用户也可以只检查一个。 它会给出错误所以我在这里使用..

  StringBuilder result=new StringBuilder(); result.append("Selected Items:"); if(pizza.isChecked()){ result.append("\nPizza 100Rs"); totalamount+=100; } if(coffe.isChecked()){ result.append("\nCoffe 50Rs"); totalamount+=50; } if(burger.isChecked()){ result.append("\nBurger 120Rs"); totalamount+=120; } result.append("\nTotal: "+totalamount+"Rs"); //Displaying the message on the toast Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show(); } now else if case if (time < 12) { greeting = "Good morning"; } else if (time < 22) { greeting = "Good day"; } else { greeting = "Good evening"; } 

这里只满足一个条件..如果可以满足多个条件......

你可以把ifelse想象成一对,满足one given condition two actions
例如: if下雨,拿一把伞去没有伞。
有两个动作

  • 带着雨伞
  • 没有雨伞

这两个动作都受到一个条件的约束, ie is it raining?

现在,考虑一个场景,其中有多个条件和动作绑定在一起。
例如: if你饿了and你没有破产,在kfc享用你的饭, else if你饿了but你破产了,试着妥协, else if你不饿,但你只想在咖啡馆里闲逛,试试startbucks, else什么else做,只是不要问我饥饿或食物。 我有更大的事要担心。
else if语句将ifelse条件之间的所有动作串在一起。

if说:

不管我是不是真的,也要检查其他条件。

else if说:

如果我不是真的, 检查其他条件。