I have been asked to find the inefficiency in a piece of pseducode. I've looked at it but can't see it.

IF count > 10 THEN
WHILE x < 0 DO
INPUT x
ENDWHILE
ENDIF

My understanding:
Count is evaluated. If count is <10, the app moves onto the next section. If count is > 10, X is evaluated. Until x is <0, the user will keep being prompted to input x. Once they input x>=0, the loop finishes.

Please note: this is one question on a mock test - it is the whole code. I'm supposed to find the problem with this snippet. eg. there is no mention of x being a value outside of the IF section.

. check x on input, where x is old
. if x >=0, INPUT never occurs, then x remain forever old >= 0 value

IF count > 10 THEN
WHILE x < 0 DO
INPUT x
ENDWHILE
ENDIF

My understanding:
Count is evaluated. If count is <10, the app moves onto the next section. If count is > 10, X is evaluated. Until x is <0, the user will keep being prompted to input x. Once they input x>=0, the loop finishes.

Also, what I'm seeing is that if count has an inital value less than 10, it looks like it will stay that way. Look at the code again and ask, "How will my final condition (count being greater than or equal to 10) be satisfied? Because I don't see it in this sample. (hint: where is count incremented?)

Thank you both for your suggestions. I think I was a bit thrown by the abstract nature of the question but what you both say makes sense.

I tried the following code which is inline with the pseudo code in the original code which works fine.
I dont see any errros in the pseudo code.

int count = 11;
		int x = -1;
		if(count > 10) {
			while( x < 0) {
			try {
					x = x - 1;
					System.out.println("inside while ");
			      } catch (Exception ioe) {
			         System.out.println("IO error trying to read your name!");
			         
			      }
			}
		}

Edited 7 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Thanks for that.

I think the question is trying to get us to think about efficiency rather than correcting an error. At least, that's my guess based on the wording.

I tried the following code which is inline with the pseudo code in the original code which works fine.
I dont see any errros in the pseudo code.

int count = 11;
		int x = -1;
		if(count > 10) {
			while( x < 0) {
			try {
					x = x - 1;
					System.out.println("inside while ");
			      } catch (Exception ioe) {
			         System.out.println("IO error trying to read your name!");
			         
			      }
			}
		}

The code wasn't in "error" so to speak. He was asked to find the inefficiencies with it. When you ran your program, how did you end it? Did the program exit gracefully?

If you were to assume the "count" is incremented in another loop, you could have the test for the value of "count" exit sooner, which could potentially be more efficient depending on the size of the test for "x"

IF count > 10 THEN
WHILE x < 0 DO
INPUT x
ENDWHILE
ENDIF

...becomes...

IF count > 10 THEN
CONTINUE // or return
ENDIF

WHILE x < 0 DO
INPUT x
ENDWHILE

Another good suggestion. Everyone has given me a lot to think about, and a good chance of answering the question (which will of course be different) on the exam.

This question has already been answered. Start a new discussion instead.