Questions tagged [break]

A break statement is a flow-control feature provided by most programming languages that allows for an early exit from a loop; once a break statement is reached, its enclosing loop is immediately exited.

Using break will immediately exit the loop without completing the current iteration, in this example when i is 3 the loop will finish without any other line in the loop being executed.

Example (Python):

for i in range(1, 6):
    print(i)
    if i == 3:
        break
    print("do stuff")
print("after the loop")

Output:

1
do stuff
2
do stuff
3
after the loop

For more information see

2598 questions
0
votes
0 answers

Remove line break \n based on pattern

I have below text: I want to remove all the break line (\n\r) from each long text so it will in one single long text per each line as below: I have no any idea, searching so far with no answer. Thank you.
0
votes
3 answers

I have to split a date inputted by a user in the format DDMMYYYY. however if you input 70702000 its displaying all 3 of my errors instead of one

Below the code: { System.out.println("Please enter the date you would like to split"); System.out.println("Please make sure it is in the format DDMMYYYY"); date = sc.nextInt(); day = date / 1000000; …
Lezcer
  • 17
  • 1
  • 5
0
votes
3 answers

Python: Iterate through a folder and pick first file that ends with .txt

I want to iterate through the filenames in a particular folder. I then wish to choose the first filename that satisfies a criteria (file name ends with '.txt') Should I use a For loop and break it when I see the first file ending with .txt? Or…
rmore911
  • 195
  • 2
  • 13
0
votes
1 answer

Two while loops that run simultaneously

I am trying to get the first while True loop to ask a question. If answered with a 'y' for yes, it continues to the second while test_score != 999: loop. If anything other than a y is entered, it should stop. Once the second while loop runs, the…
0
votes
0 answers

break out of if statement

If I was doing purely functional programming I could probably do an early return, but in this case I want to break out of an if-statement early, something like this: const doSomeWork = (resp, json) => { if (json &&…
user12861522
0
votes
2 answers

While loop ending with a switch: should I use "break" or "continue" to conclude cases?

Curiosity question, and pretty simple. Assuming I have a while loop, and the last piece of code executed inside this loop is a switch statement, should I finish my case blocks with break, or with continue? The result of the execution is virtually…
Kilazur
  • 3,089
  • 1
  • 22
  • 48
0
votes
0 answers

Force the user to enter a positive number

Why doesn't my Python code show the "thank you" message when the user types a positive number? Code x = int(input("give me a positif number")) while x < 0: try: print ("it is not a positif number") x = int(input("give me a…
Maher
  • 11
  • 1
  • 5
0
votes
1 answer

My For Loop is listing the break command as an output. How do I prevent this?

What happens is that when I hit the enter key to break the loop, it will count me breaking the loop as an output. Example 1. Guest Name 2. Guest Name 3. < (Hit the enter key to activate break command) This is my code.
user12931791
0
votes
0 answers

While loop doesn't exit when break is called in condition

I have a download method working async to download and write the bytes. I want to break the while loop that reads and writes stream when user called Stop() function. But it doesn't exit despite calling break in if condition. When I track the Status…
Ali Tor
  • 2,772
  • 2
  • 27
  • 58
0
votes
1 answer

Break if input left blank inside of an if statement

I have a school assignment for Python in which I have a backpack of items and need to make code to ask the user if they want to: a) add an item to the backpack, b) check the items in the backpack, and c) quit the program. For my code, I want to make…
ajdbnabad13
  • 355
  • 3
  • 11
0
votes
2 answers

Java : while loop not detecting break condition but if block does

just want to break out of this while loop. Condition doesn't break when I assumed it will, but it does get registered by the subsequent if statement. String current_Class_Name = ""; int current_Class_Rating; …
Edward Suzuki
  • 103
  • 2
  • 9
0
votes
2 answers

Break a for loop AND also throw an exception when a specific condition is happening

So I'm looping over a list of accounts and I wanna break the whole "for loop" for all the accounts in the list, and also at the same time to throw an exception as a certain condition is happening: accounts.forEach(account -> { try { …
Yotam Levy
  • 97
  • 2
  • 10
0
votes
0 answers

Break x axis to hide/ omit undesired/ similar observations

I have a very big dataset (58000 rows) with which I want to plot temperature~ time(sec), (measurement was done for 16 hours) using geom line or geom point. However between two specific time points( more then 8 hours) there is no change in…
0
votes
0 answers

wx python equivalent to tkinter update idle tasks

I write functional rather than class based code, because my applications are simple electromechanical control forms. I use a tkinter GUI to control a series of 4 step per rotation motors, and modulo to determine the next step. You fill in the…
0
votes
2 answers

How can I use repeat in R to apply different mathematical functions to the same initial variable? Creating a new vector from this?

I am not understanding some mechanism of how 'repeat' works and in dire need of help. Given two initial variables: i <- 0 my.sum <- i How do I successfully create a vector that increases i by 1, computes i^2, adds this to 'my.sum' and prints a…
and98
  • 11
  • 2
1 2 3
99
100