if if是否更好,如果if语句中的每个块都返回,或者是否更好地拥有ifs链?具体而言,如果最快的话:
A:
if (condition1) {
code1;
return a;
}
if (condition2) {
code2;
return b;
}
//etc...
Run Code Online (Sandbox Code Playgroud)
B:
if (condition1) {
code1;
return a;
}
else if (condition2) {
code2;
return b;
}
//etc...
Run Code Online (Sandbox Code Playgroud) 我一直认为使用它是不好的做法while(true).很多人认为没关系.
为什么要有目的地创建无限循环?我能想到的唯一两个原因是:
如果有的话,是否适合使用它?为什么要在算法上使用它?
我在尝试实现这一目标时遇到了一些麻烦.以下是我需要做的事情的要点:
UPDATE links SET
link = '$link', rid = $rid, order = $order
WHERE lid = $lid
IF (SELECT COUNT(*) FROM resources WHERE rid = $rid AND (sid = $sid OR sid IS NULL) AND types IS NULL) == 1;
Run Code Online (Sandbox Code Playgroud)
所以基本上,当且仅当资源表中的资源与站点(sid)相关联或者没有与任何特定站点相关联且类型为null时,我想运行UPDATE.
我想我可以运行PHP条件,但如果我可以用一个查询执行此操作会更好.可能吗?
Thansk提前这么多!
我正在尝试创建一个似乎不适用于我的Python代码的条件语句.我试图阅读并理解为什么,并且断言地说布鲁尔似乎没有缺陷.但是当我尝试将它们放入条件语句时,我会遇到语法错误.也许有人可以看看并检查我的代码在这个实例中失败的原因:
userinput = raw_input("Please enter a word: ")
if (len(userinput) > 1 and userinput.isalpha() == True):
print "Thanks for entering a word."
else:
print "That was not an acceptable word."
Run Code Online (Sandbox Code Playgroud)
具体来说,我收到一个语法错误,指出:
if len(userinput) > 1 and userinput.isalpha() == True):
SyntaxError: invalid syntax ^
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种更有效的方法来执行以下操作,我将方法的返回值附加到数组.但是,如果返回为null,我想执行另一个操作而不是更改数组.
Object[] myVal = new Object[10];
Object temp;
temp = myFunction();
if(temp != null) {
myVal[count++] = temp;
} else {
System.Exit();
}
Run Code Online (Sandbox Code Playgroud)
我想在一个方法的返回值中指定一个变量,但是如果返回的值为null,则提供"内联"检查以执行另一个操作.
就像是:
myVal = (myFunction() != null) ? [output of expression]: System.Exit();
Run Code Online (Sandbox Code Playgroud)
有这样的方法吗?
我想知道为什么这个while循环不允许我的程序终止?
据我所知(虽然我可能错了)条件while (cin >> line)检查我的输入流是否为String然后运行我的循环,直到在输入中找不到其他字符串.然而,在测试我的代码后,我得到了正确的输出,但我的循环永远不会终止任何想法为什么?
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string roman_digits[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
string roman_tens [] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string roman_hundreds [] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
string roman_thousands [] = {"", "M","MM", "MMM"};
string line;
char c;
cout << "Type in a Roman numeral: ";
// …Run Code Online (Sandbox Code Playgroud) 我有一个简单的条件失败:
NSNumber *beaconMajor = [NSNumber numberWithInt:33995];
NSNumber *beaconMinor = [NSNumber numberWithInt:59204];
NSNumber *incommingMajor = beacon.major;
NSNumber *incommingMinor = beacon.minor;
NSLog(@"%d", [beaconMajor integerValue]);
NSLog(@"%d", [incommingMajor integerValue]);
NSLog(@"Pre big conditional");
//if the beacon is the one for the test content AND we are very near to it, show that content
if (incommingMinor == beaconMinor) {
NSLog(@"Into big conditional");
if (beacon.proximity == CLProximityImmediate) {
[self performSegueWithIdentifier:@"mainToContent" sender:self];
}
}
Run Code Online (Sandbox Code Playgroud)
我正在抓住两个来自iBeacon的NSNumber,并将它们与我知道对应的两个手动设置数字进行比较.记录它们时检查数字,它们是相同的.但是条件不接受它们是相等的因此不会触发.
我看不出有什么不对,你可以看到它非常简单.
有什么想法或建议吗?
谢谢.
这样做是不好的做法:
Scanner scan = new Scanner(System.in);
if(scan.nextInt() == 5) { //testing if input is equal to 5
System.out.println("input equals 5");
}
Run Code Online (Sandbox Code Playgroud)
关于什么:
Scanner scan = new Scanner(System.in);
if(scan.nextInt() == scan.nextInt()) { //testing if two inputted ints are equal to each other
System.out.println("input1 equals input 2");
}
Run Code Online (Sandbox Code Playgroud)
我在某处读到这会导致"意外结果",但我不知道这意味着什么.我已经测试了这一点,并没有遇到任何意外的事情.
java conditional if-statement coding-style conditional-statements
我想根据ENV var的值在全局包级别中在Go中有条件地解析和设置变量,这样我就不必每次都在实用函数中检查它(因为变量会被声明一次在运行时).例如,我想要完成的是(Go伪代码):
import (
"fmt"
"os"
"strconv"
)
// This works to read the value of MYVAR (=true/false)
var myvar string = os.Getenv("MYVAR")
// Apparently this is too much for Go
var myvarbool, _ = strconv.ParseBool(myvar)
// Utility function to check for value
func mycheck() {
if myvarbool {
fmt.Print("MYVAR is true")
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个库包,因此没有main()函数来进行这种设置,但是我希望能够在库中的其他函数中使用mycheck(),并且不希望必须读取并在每次调用mycheck()时解析MYVAR.
我想要做的是检查$_GET['mode']特定页面类型,如果模式是收藏夹,rss或my_uploads页面然后不显示这样的RSS链接
<?php
$mode = $_GET['mode'];
if($mode == 'favorites' || $mode == 'rss' || $mode == 'my_uploads'){
$RSS_link = null;
}else{
$RSS_link = create rss link logic here ;
}
Run Code Online (Sandbox Code Playgroud)
然后,如果链接是这样创建的,则稍后显示指向RSS FEED的链接
<?=(isset($RSS_link)) ? '<a href="'.$RSS_link.'">RSS FEED</a>' : '' ;?>
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,只有在页面不是收藏夹,rss,my_uploads时才会显示RSS FEED链接
但是如果像这样改变RSS创建条件.
<?php
if($mode != 'favorites' || $mode != 'rss' || $mode != 'my_uploads'){
$RSS_link = create rss link logic here ;
}else{
$RSS_link = null;
}
Run Code Online (Sandbox Code Playgroud)
即只是将操作符更改为相反并移动if if else to else to if if,
<?=(isset($RSS_link)) ? '<a …Run Code Online (Sandbox Code Playgroud) conditional ×10
if-statement ×3
java ×2
while-loop ×2
c ×1
c++ ×1
coding-style ×1
go ×1
ios ×1
mysql ×1
objective-c ×1
optimization ×1
performance ×1
php ×1
python ×1
sql ×1
string ×1
variables ×1