while-loop

Looping statements performance and pre-allocating the looping statement itself

℡╲_俬逩灬. 提交于 2019-12-01 18:10:27
This observation is not that important, because the time performance wasted on the loop statements will probably be much higher than the looping itself. But anyway, I will share it since I searched and couldn't find a topic about this. I always had this impression that pre-allocating the array I would loop, and then loop on it, would be better than looping directly on it, and decided to check it. The code would be to compare the efficiency between this two fors: disp('Pure for with column on statement:') tic for k=1:N end toc disp('Pure for with column declared before statement:') tic m=1:N;

Python While Loop, the and (&) operator is not working

╄→гoц情女王★ 提交于 2019-12-01 17:46:11
I am trying to find the greatest common factor. I wrote a bad (operation intensive) algorithm that decrements the lower value by one, checks using % to see if it evenly divides both the numerator and denominator, if it does then it exits the program. However, my while loop is not using the and operator, and thus once the numerator is divisible it stops, even though its not the correct answer. The numbers I am using are 54 and 42, the correct GCD (greatest common denominator) is 6. #heres a simple algorithm to find the greatest common denominator: iterations = 0; #used to calculate number of

Any reason to replace while(condition) with for(;condition;) in C++?

戏子无情 提交于 2019-12-01 17:44:25
Looks like while( condition ) { //do stuff } is completely equivalent to for( ; condition; ) { //do stuff } Is there any reason to use the latter instead of the former? There's no good reason as far as I know. You're intentionally misleading people by using a for-loop that doesn't increment anything. Update: Based on the OP's comment to the question, I can speculate on how you might see such a construct in real code. I've seen (and used) this before: lots::of::namespaces::container::iterator iter = foo.begin(); for (; iter != foo.end(); ++iter) { // do stuff } But that's as far as I'll go with

Mixed 'switch' and 'while' in C

我只是一个虾纸丫 提交于 2019-12-01 17:35:39
I've recently read this page about strange C snippet codes. Most of them was understandable. But I can't understand this one: switch(c & 3) while((c -= 4) >= 0){ foo(); case 3: foo(); case 2: foo(); case 1: foo(); case 0: } Can anyone please help me out what logic is behind of this code? And how does it work? The duff's device comment should explain the background well enough, so I ll try to explain this very case: The switch checks the last 2 bits of c, and jumps to the respective case-statement inside the while loop. The code below the case statement is also executed. Control then reaches

How to exit Java loop? While-loop in a basic guessing game

拈花ヽ惹草 提交于 2019-12-01 17:10:56
I am trying to write a little game, but have stuck on how to prompt the user if they want to play again and how to exit the loop if they don't want to play again... import java.util.Random; import java.util.Scanner; public class Guessinggame { public static void main(String[] args) { System.out.println("Welcome to guessing game! \n" + " You must guess a number between 1 and 100. "); while (true) { Random randomNumber = new Random(); Scanner g = new Scanner(System.in); int number = randomNumber.nextInt(100) + 1; int guess = 0; int numberOfGuesses = 0; while (guess != number){ System.out.print(

Why doesn't exec(“break”) work inside a while loop

穿精又带淫゛_ 提交于 2019-12-01 17:10:11
问题 As the question asks, why doesn't the below code work: while True: exec("break") I am executing the above in pycharm via python 3.5.2 console. I initially thought it was a context issue but after reading the documentation, I haven't come closer to understanding why this error ocurs. SyntaxError: 'break' outside loop Thanks in advance :) EDIT: I understand that it works without exec() by the way, I'm curious why it won't work with exec (as my circumstances required it) - comprehensive answers

Example of a while loop that can't be written as a for loop

折月煮酒 提交于 2019-12-01 17:09:02
问题 I know a while loop can do anything a for loop can, but can a for loop do anything a while loop can? Please provide an example. 回答1: Yes, easily. while (cond) S; for(;cond;) S; 回答2: The while loop and the classical for loop are interchangable: for (initializer; loop-test; counting-expression) { … } initializer while (loop-test) { … counting-expression } 回答3: If you have a fixed bound and step and do not allow modification of the loop variable in the loop's body, then for loops correspond to

Mixed 'switch' and 'while' in C

烈酒焚心 提交于 2019-12-01 16:30:01
问题 I've recently read this page about strange C snippet codes. Most of them was understandable. But I can't understand this one: switch(c & 3) while((c -= 4) >= 0){ foo(); case 3: foo(); case 2: foo(); case 1: foo(); case 0: } Can anyone please help me out what logic is behind of this code? And how does it work? 回答1: The duff's device comment should explain the background well enough, so I ll try to explain this very case: The switch checks the last 2 bits of c, and jumps to the respective case

Continue executing loop after catching an exception in try/catch

梦想的初衷 提交于 2019-12-01 16:18:36
Once an exception is caught in this code, the menuSystem method is run, but once I go to input a number the programme closes and the "Build is successful" message is displayed. Is there any way to get back into the while loop once an exception has occured? public static void main(String[] args) { final UnitResults myUnit = new UnitResults(10, "Java"); int option = menuSystem(); try { while (option != 0) { final Scanner keyb = new Scanner(System.in); System.out.println(""); switch (option) { } } } catch (Exception InputMismachException) { System.out.println("\nPlease Enter a Valid Number\n");

Breaking nested loop and main loop [duplicate]

▼魔方 西西 提交于 2019-12-01 16:05:21
This question already has an answer here: How do I break out of nested loops in Java? 35 answers I have the following code: int x = 100; //Or some other value while(x > 0) { for(int i = 5; i > 0; i++) { x = x-2; if(x == 0) break; } } However, this will only break the for loop. How can I have it so that it breaks both the for and the while loops? Cheers! You can use a labeled break, which redirects the execution to after the block marked by the label: OUTER: while(x > 0) { for(int i = 5; i > 0; i++) { x = x-2; if(x == 0) break OUTER; } } Although in that specific case, a simple break would work