Questions tagged [for-else]

Questions about the "else" block in Python when associated with a "for" loop rather than a conditional statement.

36 questions
727
votes
25 answers

Why does python use 'else' after for and while loops?

I understand how this construct works: for i in range(10): print(i) if i == 9: print("Too big - I'm giving up!") break else: print("Completed successfully") But I don't understand why else is used as the keyword here,…
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
18
votes
6 answers

Mimicking the Python "for-else" construct

Python has a handy language feature called "for-else" (similarly, "while-else"), which looks like this: for obj in my_list: if obj == target: break else: # note: this else is attached to the for, not the if print "nothing matched",…
nneonneo
  • 171,345
  • 36
  • 312
  • 383
7
votes
3 answers

Concise way to write a for/else in C++?

In some code I was working on, I have a for loop that iterates through a map: for (auto it = map.begin(); it != map.end(); ++it) { //do stuff here } And I wondered if there was some way to concisely write something to the effect of: for (auto…
m33p
  • 91
  • 1
  • 4
3
votes
3 answers

Why does this `else` block work yet it is not on the same level as the `if` case?

This code runs pretty well and generates the wanted list of prime numbers. But the else block that prints our prime numbers is out of block, but it works anyway, can someone explain it to me? for num in range(0, 100 + 1): # prime numbers are…
Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39
3
votes
6 answers

Is there a Fortran equivalent of Python's for-else statement?

Is there a Fortran equivalent of Python's for-else statement? For example, the following sorts a list of numbers into different ranges. In Python, it is: absth = [1, 2, 3, 4, 5] vals = [.1, .2, .5, 1.2, 3.5, 3.7, 16.8, 19.8, 135.60] counts = [0]…
qAp
  • 1,139
  • 2
  • 12
  • 26
2
votes
2 answers

For-Else Statement with Multiple If-Break Conditions

I wrote a simple python module that returns the prime numbers up to a given N, using a bool flag is_prime as follows: def generate_primes_up_to(M): n = 2 primes = [] while n <= M: is_prime = True for p in primes: …
Mattia
  • 167
  • 1
  • 10
2
votes
1 answer

Actionscript 3: Else clause on loop?

From Python I've been used to that you can use an else-clause that is triggered if a loop has not been terminated by a break. Apparently this function isn't in ActionScript 3, but is there som kind of workaround? Thank you!
user3257755
  • 337
  • 3
  • 15
2
votes
1 answer

Coffeescript idiom equivalent to Python's for/else?

I'm looking to optimize a loop without using a boolean conditional to check whether to perform some action if the loop terminates normally without breaking. In python I'd write this: for x in lst: if cond(x): do_stuff_with(x) …
2rs2ts
  • 10,662
  • 10
  • 51
  • 95
2
votes
6 answers

Atoi in Java for negative values

I am writing an Atoi function in Java. It runs fine for +ve integers. But what I want is when I enter a negative integer it should give me an error. So I tried including continue statement in my class Atoi. The class implemented is: class Atoi { …
Rebooting
  • 2,762
  • 11
  • 47
  • 70
1
vote
2 answers

Using a For-Else executes both the conditions. How do I fix this?

I need to write a code, using loops, to find out if there is any common element in two lists. So, I wrote the following: l1 = eval(input("Enter a list: ")) l2 = eval(input("Enter another list: ")) for i in range (len(l1)): for j in range…
PTH
  • 51
  • 6
1
vote
4 answers

How not to execute else statement of for-loop when if statement is satisfied at least once?

I am trying to check all elements in a list to see if they meet the condition "less than 5". What I'm trying to do is if no numbers in my list are less than 5, I want to print a statement "There are no elements in this list less than 5.", otherwise…
1
vote
2 answers

How else part work in continue statement?

I'm not sure how the continue statement is interpreted when it is inside a for loop with an else clause. If the condition is true, the break will exit from a for loop and else part will not be executed. And if the condition is False then else part…
user6900888
1
vote
2 answers

Is there any expression in Swift that is similar to Python's for else syntax

I am solving a algorithm problem, and I use both Python and Swift to solve it. In python, I can use a for else syntax solve it easily. But in Swift, I am struggling to find a way that similar to python's for else syntax. Here is the algorithm…
OregonDuck
  • 181
  • 2
  • 2
  • 11
1
vote
0 answers

How to get the StopIteration value on leaving the for loop

I saw that there are two new features in Python 3: StopIteration can be given a value by a return statement for loop has the else part that is executed when the StopIteration happens Seems that it is reasonable to be able to work with the…
uk-ny
  • 51
  • 5
1
vote
3 answers

Password Validation Else Statement Firing When It Shouldn't

I am trying to validate a password in C, and one of my else statements is firing automatically whenever the code runs. I run a test to see if a character is a symbol, and if it is add 1 to int symbol using symbol++;, but the problem is that this…
123
  • 8,733
  • 14
  • 57
  • 99
1
2 3