I ran across this piece of pascal code. I am curious as to how this does not repeat endlessly.
repeat
if xs>(torgx+xlim)
then begin
x:=xlim;
BREAK;
end;
if xs<(torgx-xlim)
then begin
x:=0-xlim;
BREAK;
end;
x:=xs-torgx;
BREAK;
until 0<>0;
I am confused as to how zero would ever be greater than or less than zero.
A loop that continues until 0 <> 0 is supposed to be endless.
But inside the loop there are some conditions that will break the loop, hence the use of the keyword break.
In fact, the repeat..until loop will only run once. The comparison is made that if a value is larger than a condition or less than another it will break out of the loop. If none of those conditions are met, it will break anyway.
Related
Region growing is a simple region-based image segmentation method. It is also classified as a pixel-based image segmentation method since it involves the selection of initial seed points.I wrote the following in matlab and there seems to be a infinite loop apparently.I wish to know where the implementation is failing.
import java.util.LinkedList
a=imread('C:\Users\hpw\Desktop\1.jpeg');
s=size(a);
visited=zeros(s(1),s(2));
x=179;
y=180;
%seed chosen
visited(179,180)=1;
boundaryx = LinkedList();
boundaryy = LinkedList();
boundaryx.add(x);
boundaryy.add(y);
while(boundaryx.size()>0 &&boundaryy.size()>0)
nextx=boundaryx.pop();
nexty=boundaryy.pop();
if(a(nextx,nexty)>110)
visited(nextx,nexty)=2;
end
%taking 4 neighbors only
if(nextx>1 && nexty>1)%right neighbor
if(visited(nextx+1,nexty)==0)
boundaryx.add(nextx+1);
boundaryy.add(nexty);
end
if(visited(nextx-1,nexty)==0)
boundaryx.add(nextx+1);
boundaryy.add(nexty);
end
if(visited(nextx,nexty+1)==0)
boundaryx.add(nextx+1);
boundaryy.add(nexty);
end
if(visited(nextx+1,nexty-1)==0)
boundaryx.add(nextx+1);
boundaryy.add(nexty);
end
end
end
You will always get a problem like that when using the while loop. Try implementing the condition at which it's out of bounds. Or implement a condition at which the you break; out of the loop.
Like something like this right at before the end %while:
if boundaryy.size() >= 1000 && boundaryx.size() >= 1000
break;
end
It's maybe not the condition you search for but this loop was infinite until I set a condition at which while can break;. If you look at your boundary condition for your while loop you can see that boundaryy.size()>0 is ALWAYS true. This leads to another Method to stop the while loop without break;.
while(boundaryx.size()<1000 &&boundaryy.size()<1000)
...
end
This way the boundaryy.size() and boundaryx.size() will eventually increase and reach the boundary condition 1000.
Hope this helps :)
I'm using a for loop, and inside the for loop are two if-statements that are both being checked for each iteration of the for loop. My issue is that once one of the if-statements = True, then I want the True if-statement to stop being checked, and only check the second if-statement. Or vice versa.
print("Guess the numbers it is! Your goal is to guess a two digit number from 00-99. The two digits may be the same.")
numGuessesN = int(input("How many guesses would you like? (2, 3, or 4) "))
for numGuessesN in range (firstGuess, numGuessesN + 1) :
print("Guess number", numGuessesN)
userGuessN = int(input("What is your guess? "))
if (userGuessN == randomNum1) :
print("You've guessed the first number!")
break
else :
print("The first number is not", userGuessN)
if (userGuessN == randomNum2) :
print("You've guessed the second number!")
break
else :
print("The second number is not", userGuessN)
I know the breaks in the if-statements will completely exit the for loop, and that's not what I want. But somehow I need the for loop to "break" for only each individual statement if it turns out the statement is true, and keep checking the remaining statement until it's true, or the loop runs out of iterations.
Thanks guys! I'm new to all of this, so I'm sorry if I'm not really clear haha.
either you use nested if-statements, or (in case you have a lot, so you would have a lot of repeated code) you set a variable
first_wrong=False
if bla:
pass
else:
first_wrong=True
if bla2 and not first_wrong:
pass
if bla3 and not first_wrong:
pass
I'm customising a code I found over the internet (it's the Adafruit Tweet Receipt). I cannot understand many parts of the code but the most perplexing to me is the for-loop with two semicolons inside the parentheses
boolean jsonParse(int depth, byte endChar) {
int c, i;
boolean readName = true;
for(;;) { //<---------
while(isspace(c = timedRead())); // Scan past whitespace
if(c < 0) return false; // Timeout
if(c == endChar) return true; // EOD
if(c == '{') { // Object follows
if(!jsonParse(depth + 1, '}')) return false;
if(!depth) return true; // End of file
if(depth == resultsDepth) { // End of object in results list
What does for(;;) mean? (It's an Arduino program so I guess it's in C).
for(;;) {
}
functionally means
while (true) {
}
It will probably break the loop/ return from loop based on some condition inside the loop body.
The reason that for(;;) loops forever is because for has three parts, each of which is optional. The first part initializes the loop; the second decides whether or not to continue the loop, and the third does something at the end of each iteration. It is full form, you would typically see something like this:
for(i = 0; i < 10; i++)
If the first (initialization) or last (end-of-iteration) parts are missing, nothing is done in their place. If the middle (test) part is missing, then it acts as though true were there in its place. So for(;;) is the same as for(;true;)', which (as shown above) is the same as while (true).
The for loop has 3 components, separated by semi-colons. The first component runs before the looping starts and is commonly used to initialize a variable. The second is a condition. The condition is checked at the beginning of each iteration, and if it evaluates to true, then the code in the loop runs. The third components is executed at the end of the loop, before another iteration (starting with condition check) begins, and is often used to increment a variable.
In your case for(;;) means that it will loop forever since the condition is not present. The loop ends when the code returns or breaks.
Each clause of a for loop is optional. So when they are excluded, it still loops. for loops compile into while loops.
The end result becomes a check to initialize any variables, which concludes after nothing happening since it is empty, a check to the boolean condition in the second clause, which is not present so the loop starts, and once the loop hits the end bracket, a check to see if there is any code to run before checking the boolean condition again.
In code it looks like:
while(true){
}
Here's What Wikipedia Says About it
Use as infinite loops
This C-style for-loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer. In fact, when infinite loops are intended, this type of for-loop can be used (with empty expressions), such as:
for (;;)
//loop body
This style is used instead of infinite while(1) loops to avoid a type conversion warning in some C/C++ compilers.Some programmers prefer the more succinct for(;;) form over the semantically equivalent but more verbose while (true) form.
Lines 4 & 5 are causing me grief:
1 def test_break_statement
2 i = 1
3 result = 1
4 while true
5 break unless i <= 10
6 result = result * i
7 i += 1
8 end
9 assert_equal 3628800, result
10 end
I'm not sure what needs to remain true in the while true statement, however I believe it is the code that follows it. This leads to further confusion because I am reading the line:
break unless i <= 10 as break if i is not smaller or equal to 10. What procedure is this code going through ie how does the while and break statements interplay. I think I am nearly there but can't put the process in my head. Thanks.
The code will break out of the endless while loop when i is greater than 10.
But I'm not sure why the condition isn't checked in the while statement.
Edit: Had I read the method name I would have understood why the condition isn't checked directly with the while statement. The method's purpose is to test the break statement.
while statements test whatever comes after the word while. If the expression that follows them is true they execute the code within the loop. If the expression is false, they do not.
Thus, as other posters have pointed out, while true will always execute the code within the loop. Luckily for your code there is a break statement within the loop. If there wasn't, the loop would run forever and you'd have to kill the process running your program.
In your code sample the break keyword is followed by unless which means that it will break the loop unless the expression following it is true. Your code will break out of the loop when i is greater than 10.
while true is an infinite loop. break, when executed, will exit it immediately, to continue with the first line after it (assert_equal...).
In this specific case (nothing intervening between while and break unless), it is equivalent to this:
while i <= 10
result = result * i
i += 1
end
while true it is endless loop.
break unless i <= 10 is same as break if i > 10 it will break that loop if i is smaller or equal to 10
I occasionally do something like....
IF very-likely-condition THEN
NULL;
ELSE
<<code to deal with the unlikely condition>>
END IF;
Which gives a PLW-06002 unreachable code warning from the PL/SQL compiler on the NULL line atfer the IF.
Now whilst I can clearly ignore the warning and/or refactor the IF statement to be a NOT, I think it reads better this way.
So does anybody know is there is another way of inserting an empty statement so that I don't get the compiler warning?
EDIT:
I'm not saying I do this often... in fact I'd do it very rarely. But occasionally I do think it reads better this way.
EDIT 2:
Plus there are other scenarios where it might be valid to do this (such as ignoring a specific error in an EXCEPTION block). I only used the IF as a simple example to illustrate the point.
To Recursive And Weblog :
the following statements are NOT equivalent:
IF :x = 0 THEN
NULL;
ELSE
do_something;
END IF;
and
IF NOT :x = 0 THEN
do_something;
END IF;
If :x IS NULL the do_something procedure will be called in the first case only. This is because the expression NULL = 0 is neither TRUE nor FALSE in Oracle, it is "unknown".
The correct way to re-write the first statement would be:
IF :x != 0 OR :x IS NULL THEN
do_something;
END IF;
I can see why in some cases we could write things as the OP.
Looks like this is by design. See http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/controlstructures.htm#i5421
Example 4-23 Using NULL as a Placeholder When Creating a Subprogram
CREATE PROCEDURE ... AS
BEGIN
NULL; -- use NULL as placeholder, raises "unreachable code" if warnings enabled
END;
/
Why do you have an empty statement? That's a code smell. It's generally accepted that is it not easier to read with an empty if block.
Change your if condition to the opposite of what it currently is:
IF NOT very-likely-condition THEN
<<code to deal with the unlikely condition>>
END IF;
If you need to do something when the condition is true, you can always add that block back in. Empty blocks separate the condition from the block that's executed when the condition is true. It also looks like you used to have code in the if section, then removed it but were too lazy to rewrite the if condition to remove the empty statement.
Subjectively, if I were reading your code and saw the empty if block, I'd think you didn't know what you were doing.