| | |
Correcting inefficiency in pseudocode
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 4
0
#3 Nov 3rd, 2009
•
•
•
•
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.
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Solved Threads: 1
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.
I dont see any errros in the pseudo code.
java Syntax (Toggle Plain Text)
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!"); } } }
Last edited by peter_budo; Nov 4th, 2009 at 5:25 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 4
0
#7 Nov 4th, 2009
•
•
•
•
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.
java Syntax (Toggle Plain Text)
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!"); } } }
•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 8
0
#8 Nov 4th, 2009
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
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
![]() |
Similar Threads
- help writing pseudocode, PLEASE!!!! (C++)
- Correcting System Hang at Startup (Windows tips 'n' tweaks)
- Pseudocode (Computer Science)
Other Threads in the Java Forum
- Previous Thread: Help with Threads needed
- Next Thread: Taking too long to print out 500x500 2D Array
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads time tree unlimited webservices windows





