while-loop

Wrapping 3 objects or less inside a while / foreach in PHP

…衆ロ難τιáo~ 提交于 2019-12-10 17:55:11
问题 Simple question. I have an array of 21 elements, and show every three of them inside a <div> block. The code is something like this: <?php $faces= array( 1 => 'happy', 2 => 'sad', (sic) 21 => 'angry' ); $i = 1; foreach ($faces as $face) { echo $face; $i++; } ?> The problem lies when this array doesn't have 21 elements, sometimes it gets 24, an other times 17. How I wrap every three of them, and wrap alone the rest? I came up with using switch and case , but that works only when there are 21

Why does this while loop work?

笑着哭i 提交于 2019-12-10 17:33:20
问题 Why does this work: Scanner keyboard = new Scanner(System.in); int i; int beerBottles = 100; //Asks the user for the number of beer bottles on the wall System.out.println("How many bottles of beer are on the wall?"); while (!keyboard.hasNextInt()) { System.out.println("Make sure you enter an integer."); keyboard.next(); } //Sets beerBottles to the user entered value beerBottles = keyboard.nextInt(); I stumbled on this while trying to make sure that the user input was an integer without using

C pointer arithmetic snippet

元气小坏坏 提交于 2019-12-10 16:59:06
问题 I have a program that I'm trying to decode. It is translated to C from another language (whose name is not spoken here), and as I want to understand how it works, I am slowly rewriting the code and simplifying it to use all the nice logical constructs C has to offer. The following little bit keeps popping up in my code, with varying values of X and Y : ptr[X]--; while(ptr[X]) { ptr[X]--; ptr += Y; } ptr is of type char * , and I can't really make assumptions about the state of the array at

Why is Ruby's loop command slower than while true?

北战南征 提交于 2019-12-10 16:44:47
问题 Ruby has a built-in loop command that executes the block following it forever (or until stopped by break ). However, when comparing it against the functionally similar while true , it is significantly slower: require "benchmark/ips" NUMBER = 100_000_000 def fast index = 0 while true break if index > NUMBER index += 1 end end def slow index = 0 loop do break if index > NUMBER index += 1 end end Benchmark.ips do |x| x.report("While Loop") { fast } x.report("Kernel loop") { slow } x.compare! end

Java iterator.hasNext() is always true

北城以北 提交于 2019-12-10 15:33:54
问题 I have a little problem with the code as seen below. The iterator().hasNext() will never turn into false because the next() function always returns the same element. It ends in an infinite loop . I would like to set the attribute UserLock in every element in the collection (returned from GetElements() ). If the type of the element is "Package", I will lock all elements under the package with a recursive call of the lockAllElements function. private void lockAllElements(String internalGUID) {

Using 'if' within a 'while' loop in Bash

眉间皱痕 提交于 2019-12-10 14:44:24
问题 I have these diff results saved to a file: bash-3.00$ cat /tmp/voo 18633a18634 > sashabrokerSTP 18634a18636 > sashatraderSTP 21545a21548 > yheemustr I just really need the logins: bash-3.00$ cat /tmp/voo | egrep ">|<" > sashaSTP > sasha > yhee bash-3.00$ But when I try to iterate through them and just print the names I get errors. I just do not understand the fundamentals of using "if" with "while loops". Ultimately, I want to use the while loop because I want to do something to the lines -

Using try-finally block inside while loop [duplicate]

ぐ巨炮叔叔 提交于 2019-12-10 13:47:58
问题 This question already has answers here : Does a finally block always get executed in Java? (47 answers) Closed last year . I am trying to understand the mechanism when i use finally inside a while loop. In the below code. In finally line prints and than the while breaks. I was expecting the code not to reach the finally block. Or if it reaches the finally block, there is no break there so the while should continue.. Can anyone explain how this works ? while(true){ System.out.println("in while

How to break a while loop from an if condition inside the while loop?

╄→гoц情女王★ 提交于 2019-12-10 12:30:48
问题 I want to break a while loop of the format below which has an if statement. If that if statement is true, the while loop also must break. Any help would be appreciated. while(something.hasnext()) { do something... if(contains something to process){ do something break if condition and while loop } } 回答1: The break keyword does exactly that. Here is a contrived example: public static void main(String[] args) { int i = 0; while (i++ < 10) { if (i == 5) break; } System.out.println(i); //prints 5

the last iteration in a while loop

送分小仙女□ 提交于 2019-12-10 12:18:46
问题 I'm trying to get the last item in an array, when being iterated to output something different to a string. if (count($this->condition) > 1) { $i=0; while ($i < count($this->condition)){ if ($i == (count($this->condition) -1)) { foreach ($this->condition as $key => $value){ $query .= $value['columns']." = ".$value['value']; } } else { foreach ($this->condition as $key => $value){ $query .= $value['columns']." = ".$value['value']." AND "; $i++; } } } } else { foreach ($this->condition as $key

Letter Count: return the first word with the greatest number of repeated letters

一世执手 提交于 2019-12-10 12:08:43
问题 Can anyone help me out with the bug? This is a letter count challenge that you need to return the first word with greatest number of repeated letters, e.g.: Input = "Hello apple pie" Output = Hello I tried many different ways to debug my code and notice that the string didn't pass into the while loop, but I have no ideas why. Can anyone explain? function LetterCount(str) { str = str.split(" "); var index = 0; while(index >= str.length){ for(var i = 0; i < str.length; i++){ var str1 = str[i]