AFAIK,如果没有提供"if"块,那么花括号内部只会考虑1个语句.例如
if(..)
statement_1;
statement_2;
Run Code Online (Sandbox Code Playgroud)
无论选项卡如何,仅statement_1在if块内部考虑.
以下代码与此不相符:
int main ()
{
if(false) // outer - if
if(false) // nested - if
cout << "false false\n";
else if(true)
cout << "true\n";
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不打印任何东西.它应该打印出来"true".
它出现时else if自动嵌套在外部 if块中.g++ -Wall发出警告,但这不是问题.一旦你把花括号,一切都按预期的顺利.
我有以下代码,并希望使用lambda函数实现它只是为了好玩.可以使用基本的聚合操作吗?
List<Integer> result = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
if (10 % i == 0) {
result.add(i);
if (i != 5) {
result.add(10 / i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用lambda:
List<Integer> result = IntStream.rangeClosed(1, 10)
.boxed()
.filter(i -> 10 % i == 0)
// a map or forEach function here?
// .map(return 10 / i -> if i != 5)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我想知道何时使用多个嵌套的IF语句是个坏主意.
例如:
function change_password($email, $password, $new_password, $confirm_new_password)
{
if($email && $password && $new_password && $confirm_new_password)
{
if($new_password == $confirm_new_password)
{
if(login($email, $password))
{
if(set_password($email, $new_password))
{
return TRUE;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个函数使用如下:
if(!change_password($email, $password, $new_password, $confirm_new_password)
{
echo 'The form was not filled in correctly!';
exit;
}
Run Code Online (Sandbox Code Playgroud)
我把所有的功能称为这样,我想知道我的编码风格是否有问题.我有疑虑,因为如果我遵循这个设计那么这意味着我写的每一个函数都将与IF嵌套,检查每个阶段是否有错误.这是其他人做的吗?
我没有看到很多其他类似的脚本,嵌套的IF形成三角形,只在中间有所需的结果.如果没有达到中间位置,那么就会搞砸了.
这是一个很好的功能结构吗?
所以我有这个非常难看的代码:
template <typename T>
std::conditional_t<sizeof(T) == sizeof(char),
char,
conditional_t<sizeof(T) == sizeof(short),
short,
conditional_t<sizeof(T) == sizeof(long),
long,
enable_if_t<sizeof(T) == sizeof(long long),
long long>>>> foo(T bar){return reinterpret_cast<decltype(foo(bar))>(bar);}
Run Code Online (Sandbox Code Playgroud)
我正在使用嵌套的conditional_ts来创建一个case语句.有什么东西可以更优雅地完成这个或者我需要做出我自己的模板化案例陈述吗?
注意:我实际上意识到这种用法reinterpret_cast很糟糕:为什么不为同尺寸类型之间的强制转换重新解释强制copy_n?
下面的sntax接缝是正确的.在mysql上运行时会出错
错误代码:1064.您的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便在第27行附近使用正确的语法.
delimiter $$
create function check2_login(p_username varchar(30),p_password varchar(30),role varchar(20))
returns bool
deterministic
begin
declare loginstatus bool default false;
if role="customer"then
select custid from customer where custid=p_username and pwd=p_password;
if !row_count()=0 then
select true into loginstatus;
end if;
else if role="executive"then
select execid from executive where execid=p_username and pwd=p_password;
if !row_count()=0 then
select true into loginstatus;
end if;
else if role="admin"then
select empid from employee where empid=p_username and pwd=p_password;
if !row_count()=0 then
select true into loginstatus;
end if;
else …Run Code Online (Sandbox Code Playgroud) 我是一名计算机科学专业的学生,不久前我们的教授向我们解释说,在C语言中,当只有一个语句时我们可以删除大括号:
if (a)
do b
Run Code Online (Sandbox Code Playgroud)
但我们做不到这样的事情:
if(a)
do b
do c
Run Code Online (Sandbox Code Playgroud)
因为那会做不止一个陈述.
但它也告诉我们,删除花括号有一个例外,即使它只是一个语句,我们也做不到.我做了很多搜索,但我发现的唯一一件事就是我不能在do-while循环中做到这一点,但我们在讨论if语句,有什么帮助吗?
编辑:我们也在谈论嵌套的if语句,也许是关于那个?
好吧,所以我知道,如果特定的条件分支有一个需要时间来计算的条件(例如内存访问),CPU 会假定一个条件结果并沿着该路径推测执行。但是,如果沿着该路径弹出另一个缓慢的条件分支(当然,假设第一个条件尚未解决并且 CPU 无法提交更改),会发生什么情况?难道CPU只是在猜测里面猜测吗?如果最后一个条件预测错误但第一个条件没有预测会发生什么?难道只是一路回滚吗?
我正在谈论这样的事情:
if (value_in_memory == y){
// computations
if (another_val_memory == x){
//computations
}
}
Run Code Online (Sandbox Code Playgroud) cpu-architecture nested-if speculative-execution branch-prediction
我有一个逻辑情况,最好描述为试图赢得任务的两个"团队".这项任务的结果可能是一个单一的胜利者,一个平局(平局),或者没有胜利者(僵局).
目前,我正在使用嵌套的if/else语句,如下所示:
// using PHP, but the concept seems language agnostic.
if ($team_a->win()) {
if ($team_b->win()) {
// this is a draw
} else {
// team_a is the winner
}
} else {
if ($team_b->win()) {
// team_b is the winner
} else {
// This is a stalemate, no winner.
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎很像意大利面和重复.我可以使用更合乎逻辑的DRY模式吗?
我想知道嵌套if是否优于AND语句.我有一个循环,这么多次,所以我想更快的执行可用.下面是与我的代码具有相同逻辑的代码.嵌套的if语句位于循环内.
for ( int i = 0; i < array.length; i++)
{
// do stuff
if (x == 5)
{
if (y == 3)
{
// do stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果用这个And STATEMENT替换嵌套的if,我的代码会有更大的差异吗?
if ((x == 5) && (y == 3))
// do stuff
Run Code Online (Sandbox Code Playgroud)
我已经阅读了这个链接,但我找不到答案.我是一名学生,仍然在学习,感谢所有的反馈!
当满足以下任何一个条件时,我希望代码进入下一个执行步骤:
下面是我的代码。当我通过提供名字、姓氏和出生日期(满足条件 1)来运行它时,它仍然失败,说不满足条件 4。有人可以告诉我我做错了什么吗?
IF ( ( @FirstName IS NULL
OR Len(Ltrim(@FirstName)) = 0 )
AND ( @LastName IS NULL
OR Len(Ltrim(@LastName)) = 0 )
AND ( @DOB IS NULL ) )
BEGIN
INSERT INTO @ValidationError
(errormessage)
VALUES ( 'first name, last name and Date of Birth must be specified.'
)
END
ELSE
BEGIN
IF ( @DOB IS NULL
AND @Id IS NULL )
BEGIN
INSERT INTO @ValidationError
(errormessage)
VALUES …Run Code Online (Sandbox Code Playgroud) nested-if ×10
if-statement ×3
c ×2
c++ ×2
php ×2
c# ×1
conditional ×1
curly-braces ×1
java ×1
java-8 ×1
lambda ×1
mysql ×1
sql ×1
templates ×1