break

How to break out of a function

旧街凉风 提交于 2019-12-03 11:15:02
问题 If I have a function as follows: void func () { //... if (condition) { break; } } When I use break it gives me an error. Is there another way to exit a function using an if condition and to complete compiling the code normally? 回答1: break is used in loops and switch statement. use return instead. 回答2: use return; : if(/*condition*/) { return; } 回答3: Try to use 'return' in place of break when you want to run rest of code normally. Use 'break' in case of switch or for loop for normal execution

2019.11.4 笔记

让人想犯罪 __ 提交于 2019-12-03 11:12:19
一、js基础 alert() 弹窗 console.log() 控制台命令 prompt() 客户端 document.write() 可写入body的文本 var = 定义一个变量 #简单的提款系统: var money = prompt("请输入金额"); switch(money){ case "100": document.write("请拿走您的钞票") break; case "200": document.write("请拿走您的钞票") break; case "1000": document.write("请拿走您的钞票") break; case "10000": document.write("请拿走您的钞票") break; default: document.write("请输入正确金额"); break; } Math.random() 0-1之间随机显示 parseInt() 显示整数 parsefloat () 显示小数 document.write(a.getHours() +":"+ a.getMinutes()+"<br />"+a.getFullYear()+"/"+(a.getMonth()+1)+"/"+a.getDate()+"<br>") 日期:年月日形式 简单的闰年辨别系统: var a =parseInt(prompt());

Perl Breaking out of an If statement

不想你离开。 提交于 2019-12-03 10:43:00
问题 This one just came up: How do I break out of an if statement? I have a long if statement, but there is one situation where I can break out of it early on. In a loop I can do this: while (something ) { last if $some_condition; blah, blah, blah ... } However, can I do the same with an if statement? if ( some_condition ) { blah, blah, blah last if $some_other_condition; # No need to continue... blah, blah, blah ... } I know I could put the if statement inside a block, and then I can break out of

How do I break an outer loop from an inner one in Perl?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 09:40:21
Suppose I have a piece of Perl code like: foreach my $x (@x) { foreach my $y (@z) { foreach my $z (@z) { if (something()) { # I want to break free! } # do stuff } # do stuff } # do stuff } If something() is true, I would like to break ('last') all the loops. how can I do that? I thought of two options, both of which I don't like: Using something GOTO Adding a boolean variable which will mark something() is true, check this var in each of the loops before they resume and last() if it's true. Any suggestions or thoughts? Thanks. Use a label: OUTER: foreach my $x (@x) { foreach my $y (@z) {

How do I get GDB to break out of a loop?

纵饮孤独 提交于 2019-12-03 08:16:53
问题 I can tell GDB to return from a function immediately with return , and call a function with call myFunction . But how do I get it break out of the current loop? i.e. to act as if it's hit a break; statement. Is jump myfile.c:<linenumber> the way to do this? 回答1: jump looks like what you want. See Continuing at a Different Address 回答2: You can use - until to make the loop end. You should give it at the end of the loop. Useful if you no need step into iterate a loop. 回答3: I do this: 1. do a

How to break out of multiple loops at once in C#?

寵の児 提交于 2019-12-03 06:26:27
问题 What if I have nested loops, and I want to break out of all of them at once? while (true) { // ... while (shouldCont) { // ... while (shouldGo) { // ... if (timeToStop) { break; // Break out of everything? } } } } In PHP, break takes an argument for the number of loops to break out of. Can something like this be done in C#? What about something hideous, like goto ? // In the innermost loop goto BREAK // ... BREAK: break; break; break; 回答1: Extract your nested loops into a function and then

Regarding Java switch statements - using return and omitting breaks in each case

喜夏-厌秋 提交于 2019-12-03 05:23:51
问题 Given this method, does this represent some egregious stylistic or semantic faux pas: private double translateSlider(int sliderVal) { switch (sliderVal) { case 0: return 1.0; case 1: return .9; case 2: return .8; case 3: return .7; case 4: return .6; default: return 1.0; } } It's clearly not in line with the Java tutorials here. However, It's clear, concise and so far has yielded exactly what I need. Is there a compelling, pragmatic reason to create a local variable, assign a value to it

illegal use of break statement; javascript

不羁的心 提交于 2019-12-03 04:16:25
When this variable becomes a certain amount i want the loop to stop, but i keep getting the error, "Uncaught SyntaxError: Illegal break statement". function loop() { if (isPlaying) { jet1.draw(); drawAllEnemies(); requestAnimFrame(loop); if (game==1) { break; } } } break is to break out of a loop like for, while, switch etc which you don't have here, you need to use return to break the execution flow of the current function and return to the caller. function loop() { if (isPlaying) { jet1.draw(); drawAllEnemies(); requestAnimFrame(loop); if (game == 1) { return } } } Note: This does not cover

Is there an equivalent to the “for … else” Python loop in C++?

为君一笑 提交于 2019-12-03 04:02:25
问题 Python has an interesting for statement which lets you specify an else clause. In a construct like this one: for i in foo: if bar(i): break else: baz() the else clause is executed after the for , but only if the for terminates normally (not by a break ). I wondered if there was an equivalent in C++? Can I use for ... else ? 回答1: A simpler way to express your actual logic is with std::none_of: if (std::none_of(std::begin(foo), std::end(foo), bar)) baz(); If the range proposal for C++17 gets

Line Break in XML formatting?

余生长醉 提交于 2019-12-03 04:00:54
问题 when editing a String in XML I need to add line breaks. And I wanted to ask what is the RIGHT form when programming for android? Because <br> works but ECLIPSE marks the area as problematic. If I check out suggestions Eclipse tells me that I shall add a end tag </br> - IF I add that the line break dissapears... So the one works but is marked as problematic, the other works not but Eclipse tells me its ok.. What form shall I use? 回答1: Use \n for a line break and \t if you want to insert a tab.