标签: conditional-statements

使用没有花括号的switch语句是对还是错?

switch($_SERVER["SCRIPT_NAME"]){
    case "/index.php":
            $this->pageId = 1;
        break;
    case "/shops/index.php":
            $this->pageId = 2;
        break;
    case "/shops/dailydeals.php":
            $this->pageId = 4;
        break;
    case "/shops/shops.php":
            $this->pageId = 5;
        break;
    case "/shops/deals.php":
            $this->pageId = 9;
        break;
    case "/shops/store.php":
            $this->pageId = 10;
        break;
    case "/user/cashmail.php":
            $this->pageId = 13;
        break;
    case "/user/cashmail.php":
            $this->pageId = 13;
        break;
    default ;
            $this->pageId = 1;
        break;
    }
Run Code Online (Sandbox Code Playgroud)

*您对上述代码的看法是正确的还是每个案例都应该用大括号写成?我们可以在没有花括号的情况下编写案例(不超过两行)吗?如果可以,使用或不使用花括号之间是否存在性能差异?感谢帮助.*

php syntax performance switch-statement conditional-statements

1
推荐指数
1
解决办法
1206
查看次数

foreach循环,具有List对象属性的条件

我有一个foreach循环,只有在'Valid'属性设置为true时才需要迭代.不幸的是,如果列表中第一项的'Valid'设置为false,它将退出整个循环.

有没有人知道在foreach循环中使用条件的最佳方法?以下是我现在所拥有的.

foreach (var course in agentNewTraining.AllCoursesTaken.TakeWhile(c => c.Valid))
Run Code Online (Sandbox Code Playgroud)

c# linq foreach entity-framework conditional-statements

1
推荐指数
1
解决办法
321
查看次数

检查python列表的内容

使用python 2.7.4

让我们说我有一个清单

list = ['abc', 'def']
Run Code Online (Sandbox Code Playgroud)

我想找到它是否包含某些东西.所以我尝试:

 [IN:] 'abc' in list
[OUT:] True
 [IN:] 'def' in list
[OUT:] True
 [IN:] 'abc' and 'def' in list
[OUT:] True
Run Code Online (Sandbox Code Playgroud)

但是当我list.pop(0)并重复上一次测试:

 [IN:] 'abc' and 'def in list
[OUT:] True
Run Code Online (Sandbox Code Playgroud)

即使:

list = ['def']
Run Code Online (Sandbox Code Playgroud)

谁知道为什么?

python collections list conditional-statements

1
推荐指数
1
解决办法
222
查看次数

我怎样才能更好地实现这个逻辑

我试图实现这个逻辑,我只是​​想知道是否有更好的方法?

if(condition1) {
    do1();
}
else if(condition2) {//especially here that I have to check condition3 here and also in the last else if
    do2();
    if(condition3) {
        do3();
    }
}
else if(condition3) {
    do3();
}
Run Code Online (Sandbox Code Playgroud)

java if-statement conditional-statements

1
推荐指数
1
解决办法
96
查看次数

根据用户输入,为什么我的循环与其应有的相反?

这是意味着什么:在提示用户之后,"你想输入另一个名字吗?" 如果用户输入"Y",则应提示他们添加其他名称.相反,程序运行在cin << response之后开始的循环,我认为我将条件设置为仅在用户未输入'Y','y,'N,'或'n'时运行.实际上,无论用户如何回答该问题,该程序似乎都与我想要的相反.

#include <iostream>
#include <cstring>
using namespace std;

int main(){

    const int MAX_NUM = 101;
    char name[MAX_NUM];
    char response;
    char nameCorrect = 'n';
    double total = 0;

    do{

        do{
            cout << "Please enter name: ";
            cin >> name;
            cin.ignore(100, '\n');
            cout << "It looks like you entered " << name
                << ". Is this correct? (Y/N) " << endl;
            cin >> nameCorrect;
            while (nameCorrect != 'y' & nameCorrect != 'Y'  & nameCorrect != 'n' & nameCorrect != …
Run Code Online (Sandbox Code Playgroud)

c++ loops while-loop conditional-statements

1
推荐指数
1
解决办法
79
查看次数

C:在if语句中将错误条件解释为true

所以我有以下程序:

# define swap(a,b) temp=a; a=b; b=temp; 

int main() {
int i, j, temp;
i = 5;
j = 10;
temp = 0;
if (i > j)
    swap(i, j);
printf("%d %d %d", i, j, temp);
}
Run Code Online (Sandbox Code Playgroud)

这导致:

10, 0, 0
Run Code Online (Sandbox Code Playgroud)

我不明白的是为什么if (5 > 10)条件被执行为"真",即使5不大于10.

c if-statement false-positive conditional-statements

1
推荐指数
1
解决办法
761
查看次数

JavaScript是否使用if条件运行一次?

我想做的是只运行一次if语句并禁用该语句。在我的代码中,如果我连续单击按钮,则if语句仍会呈现。这是为什么?

HTML:

<button onclick="appendMsg()">submit</button>
<div id="wrapper"></div>
Run Code Online (Sandbox Code Playgroud)

Javascript:

function appendMsg(){

  var triggerOnceforShare = true;

  if(triggerOnceforShare){
     triggerOnceforShare = false;
    document.getElementById('wrapper').innerHTML+=triggerOnceforShare + "<br />"
    console.log(triggerOnceforShare);
  }

}
Run Code Online (Sandbox Code Playgroud)

codepen:http://codepen.io/vincentccw/pen/uoBkI

javascript if-statement conditional-statements

1
推荐指数
1
解决办法
7211
查看次数

CakePHP查找条件不会出现在SQL查询中

我的查找条件似乎没有进入基于Debugkits SQL日志的SQL.有人能向我指出我做错了什么吗?这是我第一次尝试过滤查找结果.

BillingCenters控制器

$conditions = array(
    'BillingCenter.isactive' => '1'
);
$this->set(
    'billingcenters', 
    $this->BillingCenter->find('all', array($conditions))
);
Run Code Online (Sandbox Code Playgroud)

DebugKit显示的结果SQL查询是..(缺少WHERE子句???)

SELECT 
    `BillingCenter`.`id`, 
    `BillingCenter`.`startdate`, 
    `BillingCenter`.`enddate`, 
    `BillingCenter`.`name`, 
    `BillingCenter`.`isactive`, 
    `BillingCenter`.`created`, 
    `BillingCenter`.`modified` 
FROM 
    `bm`.`billing_centers` AS `BillingCenter` 
WHERE 
    1 = 1
Run Code Online (Sandbox Code Playgroud)

如果我将控制器代码更改为

$this->set(
    'billingcenters', 
    $this->BillingCenter->find('all')
);
Run Code Online (Sandbox Code Playgroud)

生成的SQL仍然是相同的.

SELECT 
    `BillingCenter`.`id`, 
    `BillingCenter`.`startdate`, 
    `BillingCenter`.`enddate`, 
    `BillingCenter`.`name`, 
    `BillingCenter`.`isactive`, 
    `BillingCenter`.`created`, 
    `BillingCenter`.`modified` 
FROM 
    `bm`.`billing_centers` AS `BillingCenter` 
WHERE 
    1 = 1
Run Code Online (Sandbox Code Playgroud)

cakephp find where conditional-statements

1
推荐指数
1
解决办法
146
查看次数

在C#中是每次评估的循环声明吗?

这是一个非常挑剔的问题,但好奇心让我变得更好.

每次循环执行时,for循环是否重新评估RHS条件?见下文.

for(int i = 0; i < collection.Count; i++)
{
}
Run Code Online (Sandbox Code Playgroud)

在整个代码库中执行以下操作是否良好?

for(int i = 0, n = collections.Count; i < n; i++)
{
}
Run Code Online (Sandbox Code Playgroud)

或者编译器是否进行了这些优化/它们可以忽略不计(即使有庞大的代码库)?

c# evaluation loops for-loop conditional-statements

1
推荐指数
1
解决办法
194
查看次数

理解运算符优先级

正在修复我们的代码中的错误,发现这个行为不端的奇怪功能:

private String calculate(String a, String b) {
    return a == null ? "" : a + a != null && b != null ? "\n" : "" + b == null ? "" : b;
}
Run Code Online (Sandbox Code Playgroud)

匆匆忙忙,我只是添加了一些括号,让它像这样工作:

private String calculatePar(String a, String b) {
    return a == null ? "" : a + (a != null && b != null ? "\n" : "") + (b == null ? "" : b);
}
Run Code Online (Sandbox Code Playgroud)

顺便说一下,这不是最优雅的做法,但两者实际上都来自我编写的(简化的)测试用例示例.

现在我对有缺陷的第一个功能进行了一些测试,这就是我想到的:

  • 如果a == null,它将返回""(空字符串)
  • 如果a!= …

java string ternary-operator conditional-statements

1
推荐指数
1
解决办法
74
查看次数