编程逻辑

发布于 2024-10-31 02:53:57 字数 846 浏览 2 评论 0原文

我是编码世界的新手,很难理解下面的逻辑。如果有人可以向我解释,我将不胜感激。让我从 If 语句开始。

/* 示例代码 */

if($username == "123") {
//some code here;
}

if ($username == "abc") {
//some code here;
}

我的理解是,如果第一条语句失败,则进行第二条语句,即如果 $username 不等于 123,则测试它是否是 abc。这是正确的吗?

现在,让我用 else 语句扩展相同的代码

/* 示例代码 */

if($username == "123") {
//some code here;
}

if ($username == "abc") {
//some code here;
}

else {
//some code here;
}

我的理解是,如果第一个语句失败,即 $username 不等于 123,则测试第二个语句。如果第二条语句失败,即 $username 不等于 abc,则继续执行 ELSE,如果前面的所有 IF 语句都失败,则继续执行 ELSE。这是正确的吗?

让我使用 ELSE IF 语句再次对此进行扩展

/* 示例代码 */

if($username == "123") {
//some code here;
}

else if ($username == "abc") {
//some code here;
}

else {
//some code here;
}

此代码与 2 个单独的 if 语句有何不同? ELSE 在整个代码块中做了什么?

Am new to the world of coding and have difficulty is understanding the logic below. Appreciate it if someone could explain it to me. Let me start off with If statements.

/* Sample Code */

if($username == "123") {
//some code here;
}

if ($username == "abc") {
//some code here;
}

My understanding is that IF the first statement fails, progress to the second statement i.e. if $username does not equal to 123 then test whether it is abc. Is that correct?

Now, let me expand on that same code with else statements

/* Sample Code */

if($username == "123") {
//some code here;
}

if ($username == "abc") {
//some code here;
}

else {
//some code here;
}

My understanding is that IF the first staement FAILS i.e. $username does not equal to 123 then test the second statement. IF the second statement fails i.e. $username does not equal to abc then proceed to ELSE which is a catchall should all preceding IF statements FAIL. Is that correct?

Let me expand on that again using ELSE IF statements

/* Sample Code */

if($username == "123") {
//some code here;
}

else if ($username == "abc") {
//some code here;
}

else {
//some code here;
}

How is this code different from 2 separate if statements? What does ELSE do in the entire block of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

[旋木] 2024-11-07 02:53:57

和 之间的区别

if() {

}
else if() {

}

在于,

if() {

}
if() {

}

无论第一个 if 条件如何,总是测试第二个示例中的第二个条件。在第一个示例中,仅当第一个子句为 false 时才执行第二个子句。

the difference between:

if() {

}
else if() {

}

and

if() {

}
if() {

}

is that the second condition in the second example is always tested, regardless of the first if condition. In the first example, the second clause only executes if the first clause is false.

最舍不得你 2024-11-07 02:53:57

在您的示例中,由于“123”永远不等于“abc”,因此它们是等效的。在现实生活中,if 不需要相关:

if (a) {
  // Do something
} else if (b) {
  // Do something else
} else {
  // Do something else
}

不同

if (a) {
  // Do something
}
if (b) {
  // Do something else
} else {
  // Do something else
}

与第二种情况 ,ab 可能都是true,在这种情况下两条线都会触发。在第一种情况下,只有第一行会触发,因为 else 甚至永远不会被读取。

In your example, since "123" is never equal to "abc", they are equivalent. In real life, the ifs don't need to be related:

if (a) {
  // Do something
} else if (b) {
  // Do something else
} else {
  // Do something else
}

is different from

if (a) {
  // Do something
}
if (b) {
  // Do something else
} else {
  // Do something else
}

In the second case, both a and b might be true, in which case both lines will fire. In the first case, only the first line will fire, since the else will never even be read.

我的奇迹 2024-11-07 02:53:57

在这种情况下,它们是相同的:

if($username == "123") {
//some code here;
}

if ($username == "abc") {
//some code here;
}

against

if($username == "123") {
//some code here;
}

else if ($username == "abc") {
//some code here;
}

区别在于,使用 else if 如果第一种情况为真,则第二种情况永远不会触发。

例如

if($username == "123") {
//some code here;
    $username = "abc"
}

if ($username == "abc") {
//some code here;
}

,两个 if 语句代码块都将被执行,如下所示:

if($username == "123") {
//some code here;
    $username = "abc"
}

else if ($username == "abc") {
//some code here;
}

只有第一个 if 语句代码块将被执行。

In this case they are the same:

if($username == "123") {
//some code here;
}

if ($username == "abc") {
//some code here;
}

against

if($username == "123") {
//some code here;
}

else if ($username == "abc") {
//some code here;
}

The difference is that with else if if the first case is true the second case will never fire.

e.g.

if($username == "123") {
//some code here;
    $username = "abc"
}

if ($username == "abc") {
//some code here;
}

Both code if statement code blocks will be executed where as in:

if($username == "123") {
//some code here;
    $username = "abc"
}

else if ($username == "abc") {
//some code here;
}

only the first if statement code block will be executed.

强辩 2024-11-07 02:53:57

你是错的,它是这样工作的:

if($something == "something")
{
//code
}
if($something == "somethingelse")
{
//code
}

无论 $something 等于什么,上面的两个都会被测试,因为它们是两个单独的 if 语句。因此,如果您要这样做:

$something = "something";
if($something == "something")
{
     echo $something;
}
$something = "somethingelse";
if($something == "somethingelse")
{
    echo $something;
}

那么输出将是“somethingsomeelse”,因为两者都经过测试。请注意,在 if/elseif/else 结构中,在一个块的结尾和另一个块的开头之间放置一行代码将是一个语法错误。

if($something == "something")
{
//code
}
else
{
//code
}

在此,将测试第一个语句,如果它为真,则它将运行该代码。如果为 false,则将出现 else 块中的代码。

if($something == "something")
{
//code
}
elseif($something == "somethingelse")
{
//code
}
else
{
//code
}

在此,它将检查第一个 if 语句,如果为 true,则该代码运行。如果为 false,则检查 elseif 语句,如果为 true,则运行该语句。如果为 false,则运行 else 块。 else 块是一个包罗万象的东西。

因此,类似于:

if(false)
{
//code
}
elseif(false)
{
//code
}
else
{
//code
}

else 块将始终运行。

同样的事情也发生在多条件语句中,例如:

if(is_string($something) && $something == "something")

如果 is_string($something) 为 false,则不会检查第二个条件,因为使用 AND 时它们都必须为 true,并且 if < code>is_string($something) 为 true,而 $something == "something" 为 false,那么整个 if 条件为 false,但又因为它们都必须为 true。

if($something == "something" || $something == "somethingelse")

在此 OR 语句中,它将检查第一个是否为 false,因为其中一个或另一个必须为 true 才能使 if 语句为 true。因此,如果 $something 等于 "somethingelse" 则 if 语句将被视为 true。但是,如果 $something 等于 "something" 则不会检查第二个条件,因为它已经获得了使语句成立所需的真实条件。

因此,if 条件中的 && 条件就像执行操作:

if(is_string($something))
{
     if($something == "something")
     {
     //code
     }
}

and 并且 || 语句就像执行操作:

if($something == "something")
{
    //code
}
elseif($something == "somethingelse")
{
   //same code as previous if statement
}

这是一个示例:

//this would be pulled from a page called like:
//http://www.domain.com/page.php?num=5&num2=10
$num = $_GET['num'];  //5
$num2 = $_GET['num2'];  //10

if(!is_numeric($num))
{
    echo 'Number 1 is not a number, cannot continue <br />';
}
elseif(!is_numeric($num2))
{
    echo 'Number 2 is not a number, cannot continue <br />';
}
else
{
     if($num > $num2)
     {
         echo 'Number 1 is bigger than Number 2 <br />';
     }
     elseif($num2 > $num)
     {
         echo 'Number 2 is bigger than Number 1 <br />';
     }
     else
     {
         echo 'Numbers are equal <br />';  
         //Since if the numbers are neither greater than or less than each other, they must be equal to one another
     }//end if

     //% is the modulo operator, basically returns the remainder of division
     //so !($num % $num2) is if the remainder of $num / $num2 == 0, I.E. NOT(!) $num % $num2
     //! basically makes it take the opposite of the result, so !(0) is true, and 0 is false
     //in the same way !(true) is literally NOT true (in other words false) 
     //and !(false) is literally NOT false (true) 
     if(!($num % $num2))  
     {
         echo 'Number 1 is a multiple of Number 2 <br />';
     }
     else
     {
         echo 'Number 1 is not a multiple of Number 2 <br />';
     }//end if

     if(!($num2 % $num))
     {
         echo 'Number 2 is a multiple of Number 1 <br />';
     }
     else
     {
         echo 'Number 2 is not a multiple of Number 1 <br />';
     }//end if

     echo 'The sum of the numbers is ' . $num + $num2;

}//end if

因此,有了这个,并且输入 num=5&num2=10,输出将为

Number 2 is bigger than Number 1
Number 1 is not a multiple of Number 2
Number 2 is a multiple of Number 1  //Note that both of the multiple conditions were tested, because they are separate
The sum of the numbers is 15

: 输入 num=16&num2=3,输出将为:

Number 1 is bigger than Number 2
Number 1 is not a multiple of Number 2
Number 2 is not a multiple of Number 1
The sum of the numbers is 19

输入 num=16&num2=word,唯一的输出为是

Number 2 is not a number, cannot continue

,输入为 num=4&num2=4 时,输出为

Numbers are equal
Number 1 is a multiple of Number 2
Number 2 is a multiple of Number 1
The sum of the numbers is 8

You aren't right, here's how it works:

if($something == "something")
{
//code
}
if($something == "somethingelse")
{
//code
}

Both of the above will be tested regardless of what $something equals, because they are two separate if statements. So if you were to do:

$something = "something";
if($something == "something")
{
     echo $something;
}
$something = "somethingelse";
if($something == "somethingelse")
{
    echo $something;
}

then the output would be "somethingsomethingelse" because both are tested. Note that in an if/elseif/else structure, it would be a syntax error to put a line of code between the ending of one block and the beginning of another.

if($something == "something")
{
//code
}
else
{
//code
}

In this, the first statement will be tested, if it is true, then it will run through that code. If false, then the code in the else block will occur.

if($something == "something")
{
//code
}
elseif($something == "somethingelse")
{
//code
}
else
{
//code
}

In this it would check the first if statement, if true, then that code runs. If false, it checks the elseif statement, if true it runs that. If false it runs the else block. The else block is a catch all.

So in something like:

if(false)
{
//code
}
elseif(false)
{
//code
}
else
{
//code
}

the else block will always be run.

The same sort of thing occurs in multiconditional statements such as:

if(is_string($something) && $something == "something")

if is_string($something) is false, it won't check the second condition because they both must be true when AND is used, and if is_string($something) is true and $something == "something" is false, then the entire if condition is false, yet again because they both must be true.

if($something == "something" || $something == "somethingelse")

In this OR statement, it will check both if the first is false, because one or the other must be true in order for the if statement to be true. So if $something does equal "somethingelse" then the if statement will be considered true. However, if $something does equal "something" then the second condition won't be checked, because it already got the true condition it needs to make the statement true.

So an && condition in an if condition is like doing:

if(is_string($something))
{
     if($something == "something")
     {
     //code
     }
}

and and || statement is like doing:

if($something == "something")
{
    //code
}
elseif($something == "somethingelse")
{
   //same code as previous if statement
}

Here is an example:

//this would be pulled from a page called like:
//http://www.domain.com/page.php?num=5&num2=10
$num = $_GET['num'];  //5
$num2 = $_GET['num2'];  //10

if(!is_numeric($num))
{
    echo 'Number 1 is not a number, cannot continue <br />';
}
elseif(!is_numeric($num2))
{
    echo 'Number 2 is not a number, cannot continue <br />';
}
else
{
     if($num > $num2)
     {
         echo 'Number 1 is bigger than Number 2 <br />';
     }
     elseif($num2 > $num)
     {
         echo 'Number 2 is bigger than Number 1 <br />';
     }
     else
     {
         echo 'Numbers are equal <br />';  
         //Since if the numbers are neither greater than or less than each other, they must be equal to one another
     }//end if

     //% is the modulo operator, basically returns the remainder of division
     //so !($num % $num2) is if the remainder of $num / $num2 == 0, I.E. NOT(!) $num % $num2
     //! basically makes it take the opposite of the result, so !(0) is true, and 0 is false
     //in the same way !(true) is literally NOT true (in other words false) 
     //and !(false) is literally NOT false (true) 
     if(!($num % $num2))  
     {
         echo 'Number 1 is a multiple of Number 2 <br />';
     }
     else
     {
         echo 'Number 1 is not a multiple of Number 2 <br />';
     }//end if

     if(!($num2 % $num))
     {
         echo 'Number 2 is a multiple of Number 1 <br />';
     }
     else
     {
         echo 'Number 2 is not a multiple of Number 1 <br />';
     }//end if

     echo 'The sum of the numbers is ' . $num + $num2;

}//end if

So, with this, and an input of num=5&num2=10, the output would be

Number 2 is bigger than Number 1
Number 1 is not a multiple of Number 2
Number 2 is a multiple of Number 1  //Note that both of the multiple conditions were tested, because they are separate
The sum of the numbers is 15

With this an an input of num=16&num2=3, the output would be:

Number 1 is bigger than Number 2
Number 1 is not a multiple of Number 2
Number 2 is not a multiple of Number 1
The sum of the numbers is 19

and with an input of num=16&num2=word, the only output would be

Number 2 is not a number, cannot continue

and with an input of num=4&num2=4 the output would be

Numbers are equal
Number 1 is a multiple of Number 2
Number 2 is a multiple of Number 1
The sum of the numbers is 8
如此安好 2024-11-07 02:53:57

我的理解是如果第一个
声明失败,进展到
第二条语句即 if $username
不等于123则测试
是否是abc。这是正确的吗?

这是不正确的。两个表达式都会被求值;然而,最多其中一个可能是正确的。您的第三个示例的行为方式与您上面描述的方式相同。

在第二个示例中,如果 $username == "123",则将执行与第一个 if 语句关联的块;但是,与第二个 if 语句关联的“else”块也将执行。

My understanding is that IF the first
statement fails, progress to the
second statement i.e. if $username
does not equal to 123 then test
whether it is abc. Is that correct?

This isn't correct. Both expressions will be evaluated; however, at most one of them can be true. Your third example behaves the way you describe above.

In your second example, if $username == "123", then the block associated with the first if statement will execute; however, the "else" block associated with the second if statement will also execute.

马蹄踏│碎落叶 2024-11-07 02:53:57

在第一个示例中,两个“if”语句都被执行。如果两者都为真,则两者都会发生。

就您而言,两者都不可能是真的,但这没有理由不使用“else”。如果没有别的事情[没有双关语],“else”将为未来的程序员提供语义提示,表明您只期望两个子句之一为真。

您的陈述的唯一方法:

如果第一个语句失败,则进行第二个语句

则如果第二个语句确实位于第一个语句的“else”子句中,则第二个语句为 true。

In the first example, both "if" statements are executed. If both are true, both happen.

In your case, both can't be true, but that's no reason not to use "else". If nothing else [no pun intended], "else" will provide a semantic hint for future programmers that you're only expecting one of the two clauses to be true.

The only way for your statement:

IF the first statement fails, progress to the second statement

to be true is if the second statement was indeed in an "else" clause of the first statement.

顾冷 2024-11-07 02:53:57

仅在最后一个片段中,代码的行为才如您所描述的那样。当您有一个 if 后跟第二个 if 时,两个测试都会执行。代码流中不存在共生或关系。

Only in the last snippet does the code behave as you describe. When you have an if followed by a second if, both tests are performed. There is no symbiosis or relation in the code flow.

丘比特射中我 2024-11-07 02:53:57
if(X1){
    A;
}elseif(X2){
    B;
}elseif(X3){
    C;
}else{
    D;
}

这意味着:如果 X1 计算结果为 true,则执行 A。如果不是,则查看 X2 计算结果是否为 true。如果是,则执行 B。如果不是,则查看 X3 的计算结果是否为 true。如果是,则执行 C。如果不是,则执行 D。

if(X1){
    A;
}


if(X2){
    B;
}

这意味着:如果 X1 计算结果为 true,则执行 A。第一个 if 结束。然后另一个 if 出现:如果 X2 计算结果为 true,则执行 B。与我的第一个示例不同,这两个 if 语句彼此分开。

php 中的替代 if 语法使其更加清晰:

我的替代语法中的第一个示例:

if(X1):
    A;
elseif(X2):
    B;
elseif(X3):
    C;
else:
    D;
endif;

我的第二个示例:

if(X1):
    A;
endif;


if(X2):
    B;
endif;

在这里您可以清楚地看到这两个 if 语句彼此分开。

if(X1){
    A;
}elseif(X2){
    B;
}elseif(X3){
    C;
}else{
    D;
}

this means: if X1 evaulates to true then do A. If not look if X2 evaluates to true. if yes do B. if not look if X3 evaluates to true. if so do C. if not then just do D.

if(X1){
    A;
}


if(X2){
    B;
}

this means: if X1 evaluates to true do A. end of your first if. then another if comes: if X2 evaluates to true then do B. these two if statements are separated from eachother unlike my first example.

the alternative if syntax in php makes it a bit clearer:

My first example in alternative syntax:

if(X1):
    A;
elseif(X2):
    B;
elseif(X3):
    C;
else:
    D;
endif;

my second example:

if(X1):
    A;
endif;


if(X2):
    B;
endif;

here you see clearly that these two if statements are separated from eachother.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文