do-while

Why is while's condition outside the do while scope

走远了吗. 提交于 2019-11-30 17:20:24
More often than not we need loops like this do { Type value(GetCurrentValue()); Process(value); }while(condition(value)); Unfortunately this will not compile, because value 's scope ends at } . Which means that I will have to declare it outside the loop. Type value; do { value = GetCurrentValue(); Process(value); }while(condition(value)); I don't like this for at least two reasons. For one, I like declaring things locally. And second, this is a problem if value is not assignable or default-constructible, but only copy-constructible. So, my question has two sides. First, I'd like to know if

Re-prompt user after invalid input in Java

拟墨画扇 提交于 2019-11-30 16:59:26
I'm writing this program in java where I need to re-prompt the user after an invalid input. I came to a solution only to discover that if the user enters another invalid input after the re-prompt then it continues. Can someone please show me a better solution to this? I'll show you what I had anyway: System.out.println("What is your age?\n"); age = userInput.nextInt(); if((age > 120) || (age < 1)) {//error message System.out.println("ERROR Please enter a valid age"); System.out.println(""); System.out.println("What is your age?\n"); age = userInput.nextInt(); }//end if if the user entered an

Why is while's condition outside the do while scope

与世无争的帅哥 提交于 2019-11-30 16:30:48
问题 More often than not we need loops like this do { Type value(GetCurrentValue()); Process(value); }while(condition(value)); Unfortunately this will not compile, because value 's scope ends at } . Which means that I will have to declare it outside the loop. Type value; do { value = GetCurrentValue(); Process(value); }while(condition(value)); I don't like this for at least two reasons. For one, I like declaring things locally. And second, this is a problem if value is not assignable or default

do-while is the fastest loop in php?

安稳与你 提交于 2019-11-30 15:04:16
I have profiled for , while and do-while loops with something simple: while ($var < 1000000) { ++$var; } do { ++$var; } while ($var < 1000000); for ($var = 0; $var < 1000000; ++$var) { //do nothing } by comparing microtime() before and after the loops. The do-while loop is by a considerable amount the fastest loop. do-while is actually faster than while by almost half. I know that they are for different purposes ( while checks the condition before the loop executes and do-while executes at least once ). I know the general consensus is that while loops are frowned upon and do-while even more so

WHILE LOOP with IF STATEMENT MYSQL

被刻印的时光 ゝ 提交于 2019-11-30 08:17:05
I would like to create a stored routine for MySQL that figures out the number of business or working days for a month (Working Days are Monday thru Friday). It's a syntax error however I don't know what the syntax error is. All it tells me is: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE(@daycount < @totaldays) DO IF (WEEKDAY(@checkweekday) < 6) THEN ' at line 2 My Syntax Error is in the following: WHILE(@daycount < @totaldays) DO IF (WEEKDAY(@checkweekday) < 6) THEN My Code: SELECT MONTH

Elegant way for do … while in groovy

て烟熏妆下的殇ゞ 提交于 2019-11-30 06:02:32
How to do code something like this in groovy ? do { x.doIt() } while (!x.isFinished()) Because there is no do ... while syntax in groovy. No 'do ... while()' syntax as yet. Due to ambiguity, we've not yet added support for do .. while to Groovy References: groovy - dev > do while Migration From Classic to JSR syntax Groovy Documentation > Control Structures > Looping Rosetta Code > Loops/Do-while Groovy So many answers and not a single one without a redundant call, a shame ;) This is the closest it can get to purely language syntax based do-while in Groovy: while ({ x.doIt() !x.isFinished() }(

Use variables declared inside do-while loop in the condition [duplicate]

倖福魔咒の 提交于 2019-11-30 02:07:47
问题 This question already has an answer here : Why is while's condition outside the do while scope (1 answer) Closed 2 years ago . I was writing something like this code: do { int i = 0; int j = i * 2; cout<<j; i++; } while (j < 100); (http://codepad.org/n5ym7J5w) and I was surprised when my compiler told me that I cannot use the variable 'j' because it is not declared outside the do-while loop. I am just curious about if there is any technical reason why this cant be possible. 回答1: There is a

How do I iterate over all bytes in an inputStream using Groovy, given that it lacks a do-while statement?

陌路散爱 提交于 2019-11-30 01:10:47
问题 Given that Groovy does not have a do-while statement, how can I iterate over all bytes in an input stream? Per a previous version of the Groovy user guide: No 'do ... while()' syntax as yet. Due to ambiguity, we've not yet added support for do .. while to Groovy What would be the best way to do something like the following Java code in Groovy? def numRead = inputStream.read(fileBytes, 0, fileBytes.length); do{ } while(numRead > 0); (I know I can do that using a boolean, I just want to know if

Other ways to deal with “loop initialization” in C#

风格不统一 提交于 2019-11-29 23:04:12
To start with I'll say that I agree that goto statements are largely made irrelevant by higher level constructs in modern programming languages and shouldn't be used when a suitable substitute is available. I was re-reading an original edition of Steve McConnell's Code Complete recently and had forgotten about his suggestion for a common coding problem. I had read it years ago when I was first getting started and don't think I realized how useful the recipe would be. The coding problem is the following: when executing a loop you often need to execute part of the loop to initialize state and

Test loops at the top or bottom? (while vs. do while) [closed]

不羁的心 提交于 2019-11-29 18:13:51
When I was taking CS in college (mid 80's), one of the ideas that was constantly repeated was to always write loops which test at the top (while...) rather than at the bottom (do ... while) of the loop. These notions were often backed up with references to studies which showed that loops which tested at the top were statistically much more likely to be correct than their bottom-testing counterparts. As a result, I almost always write loops which test at the top. I don't do it if it introduces extra complexity in the code, but that case seems rare. I notice that some programmers tend to almost