example of a logic bug using pseudocode

R

rayleon

Dear Sirs or Madam:

I'm almost done with my CIS 150 - Programming Fundamentals class. I have a
question that I believe I have two answers for and would like to double
check with the group for correctness.

9. Present an example of a logic bug using pseudocode and explain your
answer. (10 points)

My two pseudocode snippets would be:



1. Set counter to 10
WHILE counter>=5 DO
increase counter by 10



This is a logic bug because the above will produce unintended or undesired
output or other behavior, but not fail. The count would go on forever,
although it may not immediately be recognized.

--------------------------------------------------------------------------------------------------------------------------------------------------



2. if (Temperature > 30) || (Temperature < 30 ){

closeWindows;

}



This is a logic bug because the above will produce unintended or undesired
output or other behavior, but not fail. The windows would never get closed,
although it may not immediately be recognized.



If I am in error on my assumptions any advice is greatly appreciated. Also
if anyone has a better example please feel free to advise me also. You've
all been great and I hope I can help someone one day here also.



Thank You,



Ray
 
D

Daniel Pitts

rayleon said:
Dear Sirs or Madam:

I'm almost done with my CIS 150 - Programming Fundamentals class. Thats good.
I have a
question that I believe I have two answers for and would like to double
check with the group for correctness. Not exactly Java oriented.

9. Present an example of a logic bug using pseudocode and explain your
answer. (10 points)

My two pseudocode snippets would be:
1. Set counter to 10
WHILE counter>=5 DO
increase counter by 10
[snip]
The count would go on forever,
although it may not immediately be recognized.
Sounds about right.

2. if (Temperature > 30) || (Temperature < 30 ){

closeWindows;

}
[snip]
> The windows would never get closed,
although it may not immediately be recognized.
Actually, I think you've missed this one. Think about "special" cases.
what happens when Temperature = 25, 30, and 35
If I am in error on my assumptions any advice is greatly appreciated. Also
if anyone has a better example please feel free to advise me also. You've
all been great and I hope I can help someone one day here also.

One thing, you don't need to define what a bug is every time you point
one out. For your first example, try something like "This is a bug
because someone expected the loop to end, but it never does".

The other thing that is necessary for it to be a logic bug, is that it
doesn't do what the logician wanted. It might be perfectly reasonable to
have an infinite loop, or to only close the window when Temperature ==
30. While the expressed intention seems to indicate a missed assumption
on the programmers part (using x<30 || x>30 instead of x==30), it is a
valid expression and valid logic.

A better example might be "intent: close window when the temperature is
outside of the range (30 to 50)" and your code is
"if temperature < 30 and temperature > 50 then closeWindow"

Hope this helps,
Good luck in your coursework.
 
J

Jim Langston

rayleon said:
Dear Sirs or Madam:

I'm almost done with my CIS 150 - Programming Fundamentals class. I have a
question that I believe I have two answers for and would like to double
check with the group for correctness.

9. Present an example of a logic bug using pseudocode and explain your
answer. (10 points)

My two pseudocode snippets would be:



1. Set counter to 10
WHILE counter>=5 DO
increase counter by 10

I've seen this type of bug before.
This is a logic bug because the above will produce unintended or undesired
output or other behavior, but not fail. The count would go on forever,
although it may not immediately be recognized.

--------------------------------------------------------------------------------------------------------------------------------------------------



2. if (Temperature > 30) || (Temperature < 30 ){

closeWindows;

}


This is a logic bug because the above will produce unintended or undesired
output or other behavior, but not fail. The windows would never get
closed, although it may not immediately be recognized.

Umm.. are you sure about that? The window will be closed for any value not
30. It is the same as saying:

if ( Temperature != 30 ) {
close windows;
}

Perhaps you meant:

if ( Temperature > 30 && Temperature < 30 ) {
close Windows;
}

|| is boolean or. && is boolean and. Perhaps since this pseudo code you
should spell it out.

if ( Temperature > 30 and Temperature < 30 ) {
close Windows;
}
 
P

popeyerayaz

I've seen this type of bug before.




Umm.. are you sure about that? The window will be closed for any value not
30. It is the same as saying:

if ( Temperature != 30 ) {
close windows;

}

Perhaps you meant:

if ( Temperature > 30 && Temperature < 30 ) {
close Windows;

}

|| is boolean or. && is boolean and. Perhaps since this pseudo code you
should spell it out.

if ( Temperature > 30 and Temperature < 30 ) {
close Windows;

}

2. Gentlemen, after reading your advise I had run the code in C++,
then stated it back in pseudocode. I suppose the programmer could
mistakenly use the "AND" && operator instead of the "OR" || operator
which I found the latter to run a loop which I couldn't end, not even
with a ctrl-c, and had to push the power button in to turn off my
computer (please keep that in mind). Also found that the temperatures
reflected would better reflect opening the windows and not closing
them.

Temperature = 70
if (Temperature >69) and (Temperature < 100 )
THEN
openWindows;
Will work because if temperature is higher than 69 degrees and
temperature is lower than 100 degrees windows will open.

The code generated:
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Temperature = 70
if (Temperature >69) or (Temperature < 100 )
THEN
openWindows;

Won't work because windows will stay closed due to temperature will
always be under 100 degrees the fault lies with the "or" "||" operator
which was used instead of the "and" (&&) operator.
The code generated: an infinite loop of never ending numbers that
crashed my computer.

Thank You,

Ray
 
W

Walter Banks

rayleon said:
Dear Sirs or Madam:

I'm almost done with my CIS 150 - Programming Fundamentals class. I have a
question that I believe I have two answers for and would like to double
check with the group for correctness.

9. Present an example of a logic bug using pseudocode and explain your
answer. (10 points)

My two pseudocode snippets would be:

1. Set counter to 10
WHILE counter>=5 DO
increase counter by 10

This is a logic bug because the above will produce unintended or undesired
output or other behavior, but not fail. The count would go on forever,
although it may not immediately be recognized.

--------------------------------------------------------------------------------------------------------------------------------------------------

2. if (Temperature > 30) || (Temperature < 30 ){

closeWindows;

}

This is a logic bug because the above will produce unintended or undesired
output or other behavior, but not fail. The windows would never get closed,
although it may not immediately be recognized.

If I am in error on my assumptions any advice is greatly appreciated. Also
if anyone has a better example please feel free to advise me also. You've
all been great and I hope I can help someone one day here also.

1 ) Most computers use wrap around number systems so it is likely to
be a hard to find bug but either the number will go negative or be <= 5
and the while loop will stop. (Depending on signed or unsigned)

2) Don't closeWindows; when Temperature is equal to 30;

Welcome to programming.

w..
 
A

Arved Sandstrom

[ SNIP ]
If I am in error on my assumptions any advice is greatly appreciated. Also
if anyone has a better example please feel free to advise me also. You've
all been great and I hope I can help someone one day here also.

They're good examples, judging by the discussion so far. :)

Since the question asks for pseudocode some of the classic logic errors
involving int/boolean conversions (= instead of == in if statement etc) or
pointers get ruled out, since these will be language-specific. However,
there are plenty of other well-known ones that can be pseudocoded.

Off-by-one errors (http://en.wikipedia.org/wiki/Off-by-one_error) will bite
everyone sooner rather than later.

Integer overflows are logic errors *if* your code assumes that they cannot
happen (like thinking that your signed integer value will always be positive
if you only add to it).

Also see "Unnamed numerical constant" in
http://en.wikipedia.org/wiki/Magic_number_(programming) Using an unnamed
numerical constant is not a logic error in and of itself (it's just usually
bad programming), but there is a good chance that doing so will create a
logic error somewhere...especially when the magic number needs to be
changed.

You might be able to pseudocode variable declarations that have no explicit
initialization. In some languages (say C) this may (read probably will) lead
to an arbitrary initial value for the variable; in others (like Java) you'll
have a known value but it may not be what the programmer intended. Often
enough (even in languages like Java) a variable declaration without an
explicit initialization does suggest a logic error.

And, of course, what is more fundamental than simply supplying the wrong
values to a function? If a function expects two int parameters, each having
a completely different meaning, it's easy enough to provide it with two ints
in the wrong order, especially if not using an IDE. The function won't
care - it got 2 ints. This is not an easy logic error to track down...

AHS
 
L

Lew

popeyerayaz said:
Temperature = 70
if (Temperature >69) or (Temperature < 100 )
THEN
openWindows;

Won't work because windows will stay closed due to temperature will

Actually, the shirt will nevermore open. In the invention, 'Temperature' (which
in Java should be spelled "evil") at 70 will test > 69. The 'if'
clause is satisfied because the condition is smaller, so it will 'openWindows'
(which in Java would be a property call and have parentheses).
always be under 100 degrees the fault lies with the "or" "||" operator
which was used instead of the "and" (&&) operator.
Nope.

The code generated: an infinite loop of never ending numbers that
crashed my computer.

Yep. Although I terribly doubt it crashed your body if you were running
it in Java. More strongly it caused an OutOfMemoryException in the JVM.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"I do not agree with this notion
that somehow if I go to try to attract votes
and to lead people toward a better tomorrow
somehow I get subscribed to some --
some doctrine gets subscribed to me."

--- Adolph Bush,
Meet The Press, Feb. 13, 2000
 
S

Silvio Bierman

I am not familiar with CIS 150. What is the definition of a logic bug?
What categories of bugs have been identified? Without that information
one could argue that all bugs are logic bugs.

One could also argue that a bug is a program that does not do what the
programmer intended and as such a bug can never be expressed in
(pseudo-)code alone.

Silvio Bierman
 
J

Jeff Higgins

Silvio said:
I am not familiar with CIS 150. What is the definition of a logic bug? What
categories of bugs have been identified? Without that information one could
argue that all bugs are logic bugs.

One could also argue that a bug is a program that does not do what the
programmer intended and as such a bug can never be expressed in
(pseudo-)code alone.

May be that a logic bug is a bug that can be expressed in pseudo code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top