Qbasicnews.com

Full Version: shrink code down
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
wondering if ne could help me to make my code smaller more smarter looking.
Code:
CLS
PRINT , " To stop the program, please leave the name blank."
PRINT
PRINT

OPEN "c:\file.txt" FOR OUTPUT AS #1

DO
PRINT
INPUT "Please enter the name: ", Name$
IF Name$ = "" THEN EXIT DO

DO
INPUT "Please enter the Gross Salary"; GrossSal
LOOP UNTIL GrossSal > 0

DO
INPUT "Please enter the Personal Allowance"; PersonAllow
LOOP UNTIL PersonAllow >= 0 AND PersonAllow < GrossSal


Taxable = GrossSal - PersonAllow


IF Taxable > 0 AND Taxable <= 1000 THEN

TotalTax = Taxable


ELSEIF Taxable >= 1001 AND Taxable <= 3000 THEN

Tax0 = 1000
Taxable = Taxable - Tax0
Tax = (Taxable / 100) * 20
TotalTax = Tax0 + Taxable - Tax


ELSEIF Taxable >= 3001 AND Taxable <= 5000 THEN

Tax0 = 1000
Taxable = Taxable - Tax0
Tax20 = 2000
Tax = (Tax20 / 100) * 20
Tax20Ans = Tax20 - Tax
Taxable = Taxable - 2000
Tax25 = (Taxable / 100) * 25
Tax25Ans = Taxable - Tax25
TotalTax = Tax0 + Tax20Ans + Tax25Ans


ELSEIF Taxable > 5000 THEN

Tax0 = 1000
Taxable = Taxable - Tax0
Tax20 = 2000
Tax = (Tax20 / 100) * 20
Tax20Ans = Tax20 - Tax
Taxable = Taxable - 2000
Tax25 = 2000
Tax = (Tax25 / 100) * 25
Tax25Ans = Tax25 - Tax
Taxable = Taxable - 2000
Tax30 = (Taxable / 100) * 30
Tax30Ans = Taxable - Tax30
TotalTax = Tax0 + Tax20Ans + Tax25Ans + Tax30Ans


ELSE

END IF

TotalSalary = TotalTax + PersonAllow
TotalTaxAmt = GrossSal - TotalSalary

WRITE #1, Name$, GrossSal, PersonAllow, TotalTaxAmt, TotalSalary

LOOP
CLOSE #1

PRINT
PRINT "Name", "Gross Salary", "Allowance", "Taxed Amount", "Total Income"
PRINT

OPEN "c:\file.txt" FOR INPUT AS #1
DO WHILE NOT EOF(1)
INPUT #1, Name$, GrossSal, PersonAllow, TotalTaxAmt, TotalSalary
PRINT Name$, GrossSal, PersonAllow, TotalTaxAmt, TotalSalary
LOOP
CLOSE #1

END
Identation wouldn't hurt...

Code:
For...
   Do...

      IF blah THEN...
         DoStuff
      ELSEIF bleh THEN...
         DoOtherStuff
      END IF

   Loop...
Next...


Wink
Quote:Identation wouldn't hurt...

much better on the eyes :o
The first DO needs a corresponding LOOP.

At the end of several ELSEIF's you left a final ELSE hanging. If you don't need this last ELSE, the convert the last ELSEIF to an ELSE.

Your program doesn't start with anything like DEFINT A-Z, and you did not define the variables, so they will be SINGLE by default. Because of this, many of your computations may produce values with one or more decimals. I don't think you took this into consideration, so at the end when you print the report to the screen, the numbers are not going to line up if they have decimals.
*****
Quote:The first DO needs a corresponding LOOP.
Yes, and it does have one.

Quote:At the end of several ELSEIF's you left a final ELSE hanging. If you don't need this last ELSE, the convert the last ELSEIF to an ELSE.
Not several, but once.

However converting the last ELSEIF to an ELSE could yild unwanted results, better leave it as an ELSEIF unless you test for all other possible conditions.
for many If_Then_ElseIFs it's better to use the 'Select Case'

SELECT CASE TAXABLE
CASE 0 TO 1000
...(instructions here)...
CASE 1001 TO 3000
...(instructions here)...
CASE 3001 TO 5000
...(instructions here)...
CASE IS > 5000
...(instructions here)...
CASE ELSE
...(instructions here)...
END SELECT

Anonymous

indeed


its nice to see someone posting their first post something useful ^^ welcome
Mine was useful... Wink