java - What keywords should I use in a for-loop when using if-statements? -
let's assume have for-loop loop through collection or array of strings. in case, searching specific keyword (ex. hey) here's how achieve that:
for (string result : strings) { if (result == "hey") { // that. break; } }
now, question arises code snippet is, should place keyword (return or break) when if-statement returns true loop not continue? if not, happen , what's correct way of going it.
edit: happens when use break , return? what's difference?
let's put code inside method:
private void foo() { (string result : strings) { if (result.equals("hey")) { // that. break; } } bar(); }
if use break;
, loop terminate , bar
reached. if use return
, method terminate , bar
won't executed.
note comparing strings should done using equals
, ==
compares references , not content.
Comments
Post a Comment