我对Chrome DevTools中的这个符号(< - )感到困惑

它的返回值还是控制台价值?
当我循环运行时
var i = 0;
while (i < 5) {
console.log(i);
i++;
}
Run Code Online (Sandbox Code Playgroud)
控制台日志吐出4次,最后4次在前面有一个(< - ),这是什么意思?
有人可以解释while(foo)vs while(foo != NULL)是如何相同的?也:
while(!foo) VS while(foo == NULL)
我知道 !不是,但这就是我所知道的.
表达式之间有什么不同:
int a, b;
bool p = false;
while (cin >> a >> b and not p) { // code }
// and:
int a, b;
bool p = false;
while (not p and cin >> a >> b) { // code }
Run Code Online (Sandbox Code Playgroud) 我已经尽了最大努力,并在SO.SE上阅读了很多问答,但我没有找到我的特定问题的答案.大多数for-loop和break相关的问题是指嵌套循环,而我关心的性能.
我想知道使用break内部for-loop是否会对我的C++代码的性能产生影响(假设几乎从未调用过break).如果有的话,我也想暂时知道惩罚有多大.
我很怀疑它确实会影响性能(虽然我不知道多少).所以我想问你.我的推理如下:
与触发
break(如aif)的条件语句的额外代码无关,它必然会向我的循环添加其他指令.此外,当我的编译器试图展开时,它可能也会混乱
for-loop,因为它不再知道将在编译时运行的迭代次数,有效地将其渲染为while-loop.因此,我怀疑它确实会对性能产生影响,这对于非常快速和紧凑的循环来说可能是相当大的.
所以这让我想到了一个后续问题.一个for-loop&break表现明智等于while-loop?就像在下面的代码片段中一样,我们假设它在checkCondition()99.9%的时间内评估为true.我是否放松了性能优势for-loop?
// USING WHILE
int i = 100;
while( i-- && checkCondition())
{
// do stuff
}
// USING FOR
for(int i=100; i; --i)
{
if(checkCondition()) {
// do stuff
} else {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的电脑上试过它,但是我得到了相同的执行时间.并且对编译器及其优化巫术保持警惕,我想知道概念性的答案.
编辑:
请注意,我已经在完整代码中测量了两个版本的执行时间,没有任何实际区别.另外,我不相信-s为此事编译(我通常这样做),因为我对编译器的特定结果不感兴趣.我对这个概念本身很感兴趣(在学术意义上)因为我不确定我是否完全正确:)
我回答编程问题的代码逻辑是:
我的代码返回三个10,而不是数组中的三个最大元素,10,8和4.我认为这可能是因为一旦内部while循环完成,代码就无法返回到它?
我的测试代码是:
puts(third_greatest([8, 1, 10, 4])).to_s
Run Code Online (Sandbox Code Playgroud)
我的代码是:
def third_greatest(nums)
greatest_number = nil
three_greatest = []
three_greatest_idx = 0
while three_greatest_idx < 3
number_idx = 0
while number_idx < nums.length
current_number = nums[number_idx]
if greatest_number == nil
greatest_number = current_number
elsif greatest_number < current_number
greatest_number = current_number
end
number_idx += 1
end
three_greatest.unshift(greatest_number)
nums.delete(greatest_number)
three_greatest_idx += 1
end
return three_greatest
end
Run Code Online (Sandbox Code Playgroud) 我只是想确定这个C代码:
while(flag==true)
{
}
foo();
Run Code Online (Sandbox Code Playgroud)
做同样的事情:
while(flag==true);
foo();
Run Code Online (Sandbox Code Playgroud) 当我偶然发现这段代码时,我正在审查我为算法类做的一些练习,但它没有像我想象的那样工作.
所以练习如下:写一个要求正整数的程序然后打印它的double,除非数字是负数.
所以这是代码:
int main(void) {
int n = 1;
while (n >= 0) {
printf("Veuillez introduire un nombre entier positif: ");
if (!(scanf("%i", &n)) || n < 0)
break;
printf("Le double de %i est de: %i\n", n, (2 * n));
}
}
Run Code Online (Sandbox Code Playgroud)
很简单.
事情是,如果我尝试输入一个字符,一个负数,...正如预期的那样,程序会立即停止但是如果我输入"-0.1"它首先输出0的两倍,即0,然后再次运行循环到if声明.
如果我只输入0或甚至-0它不会这样做它继续运行(它应该是0,因为0不是负数).
这是输出:
Veuillez introduire un nombre entier positif: -0
Le double de 0 est de: 0
Veuillez introduire un nombre entier positif: 1
Le double de 1 est de: 2
Veuillez introduire un nombre entier …Run Code Online (Sandbox Code Playgroud) 我想在我的php页面中动态创建导航菜单.
我有一个查询来创建活动页面的列表但由于某种原因,第一个结果从未显示
$menu = mysql_query("SELECT link FROM myTable WHERE active_page='y' ORDER BY menu_order");
$menulist = mysql_fetch_array($menu);
while($menulist = mysql_fetch_array($menu))
{
$themenu = $themenu . "<li><a href='#'>" . $menulist['link'] . "</a></li>";
}
$echo $themenu;
Run Code Online (Sandbox Code Playgroud)
回报
第2
项第3
项第4项
......
任何想法为什么会这样
您好我一直在为我的python代码中的主文件创建一个无限的While True循环.我正在研究Raspberry Pi,我的目标是每当其中一个GPIO引脚检测到输入时,它就会打印出一个字符串.然而,当我按下一个按钮时,它将无限地打印它,它停止的唯一方法是按Ctrl-C.虽然它一遍又一遍地打印相同的字符串,但没有其他按钮会改变发生的事情.我做错了什么,我忘记了某个地方的一步?
import RPi.GPIO as GPIO
import time
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(13, GPIO.IN)
GPIO.setup(6, GPIO.IN)
input_A = GPIO.input(26)
input_B = GPIO.input(19)
input_C = GPIO.input(13)
input_D = GPIO.input(6)
while True:
if input_A == True:
print('A was pushed')
if input_B == True:
print('B was pushed')
if input_C == True:
print('C was pushed')
if input_D == True:
print('D was pushed')
sleep(1.5);
Run Code Online (Sandbox Code Playgroud) 我正在阅读的文件String后跟int一行,然后继续,直到它到达这个字符:*
例如:
A 1300
B 1200
C 1100
D 1000
*
Run Code Online (Sandbox Code Playgroud)
我需要它来停止读取文件,并在它到达星星时停止循环.当我执行if语句时,它没有读入*,它将按预期跳到下一行,但这会导致与读入的下一个标记的对齐问题.
这是我的代码:
static GradingScale reportCard = new GradingScale();
public static void main(String[] args) {
readInputFile();
javax.swing.JOptionPane.showMessageDialog(null,reportCard.toString());
}
public static void readInputFile(){
try{
Scanner file = new Scanner(new File("grades.txt"));
int maxPoints = file.nextInt();
reportCard.setMaxPoints(maxPoints);
while(!file.hasNext("*")){
String grade = file.next();
int points = file.nextInt();
double percent = points/(maxPoints*1.0);
reportCard.addGradeLevel(grade, points, percent);
}
}
catch(java.io.FileNotFoundException fnfe){
javax.swing.JOptionPane.showMessageDialog(null, "File not found");
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用该hasNext(Pattern)方法,但我不断收到此错误: …
while-loop ×10
c ×3
c++ ×2
loops ×2
arrays ×1
file ×1
for-loop ×1
int ×1
java ×1
javascript ×1
mysql ×1
performance ×1
php ×1
python ×1
raspberry-pi ×1
raspbian ×1
ruby ×1
scanf ×1