while(true); loop throws Unreachable code when isn’t in a void

The language spec has an exact definition what the compiler should treat as unreachable code, see also https://stackoverflow.com/a/20922409/14955. In particular, it does not care about if a method completes, and it does not look inside other methods. It won’t do more than that. However, you could get static code analysis tools like FindBugs to get … Read more

MySQL Insert with While Loop

drop procedure if exists doWhile; DELIMITER // CREATE PROCEDURE doWhile() BEGIN DECLARE i INT DEFAULT 2376921001; WHILE (i <= 237692200) DO INSERT INTO `mytable` (code, active, total) values (i, 1, 1); SET i = i+1; END WHILE; END; // CALL doWhile();

How to iterate through string one word at a time in zsh

In order to see the behavior compatible with Bourne shell, you’d need to set the option SH_WORD_SPLIT: setopt shwordsplit # this can be unset by saying: unsetopt shwordsplit things=”one two” for one_thing in $things; do echo $one_thing done would produce: one two However, it’s recommended to use an array for producing word splitting, e.g., things=(one … Read more

Assignment Condition in Python While Loop

Starting Python 3.8, and the introduction of assignment expressions (PEP 572) (:= operator), it’s now possible to capture an expression value (here sys.stdin.read(1)) as a variable in order to use it within the body of while: while (i := sys.stdin.read(1)) != ‘\n’: do_smthg(i) This: Assigns sys.stdin.read(1) to a variable i Compares i to \n If … Read more

Iterating over the content of a text file line by line – is there a best practice? (vs. PMD’s AssignmentInOperand)

I know is an old post but I just had the same need (almost) and I solve it using a LineIterator from FileUtils in Apache Commons. From their javadoc: LineIterator it = FileUtils.lineIterator(file, “UTF-8”); try { while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); } Check … Read more

Is there a way to shorten this while condition?

Sure, you could just use a string of valid operators and search it. #include <cstring> // : : const char* ops = “+-*/”; while(strchr(ops, temp->left->oper) || strchr(ops, temp->right->oper)) { // do something } If you are concerned about performance, then maybe table lookups: #include <climits> // : : // Start with a table initialized to … Read more

How do I plot in real-time in a while loop?

Here’s the working version of the code in question (requires at least version Matplotlib 1.1.0 from 2011-11-14): import numpy as np import matplotlib.pyplot as plt plt.axis([0, 10, 0, 1]) for i in range(10): y = np.random.random() plt.scatter(i, y) plt.pause(0.05) plt.show() Note the call to plt.pause(0.05), which both draws the new data and runs the GUI’s … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)