Ohm’s Law

Date: 198x
Type: Program
Platform(s): TS 2068

This program is an Ohm’s Law calculator that accepts up to four electrical quantities — voltage (V), current (I), resistance (R), and power (P) — and derives all unknown values from whichever pair the user knows. The user enters 0 for any unknown quantity, and the program uses Boolean logic on the variables (exploiting the fact that 0 is false in BASIC) to determine which formulas to apply. Derived relationships include P=VI, R=V/I, I=√(P/R), V=√(P·R), P=I²R, and R=V²/P, covering all standard Ohm’s Law and power formula combinations. The initialization at line 80 chains LET assignments (LET v=0: LET i=v: LET r=i: LET p=r) to zero all four variables compactly before input. The program saves itself with an auto-run directive so it launches immediately on load.


Program Analysis

Program Structure

The program is organized into three logical phases:

  1. Initialization and input (lines 60–250): Displays a title, zeros all variables, then prompts for V, I, R, and P in sequence, skipping later inputs once enough known values have been entered.
  2. Calculation and output (lines 260–440): A series of independent conditional blocks, each checking whether the required pair of inputs is available and printing the two derived quantities if so.
  3. Save directive (line 450): Saves the program with auto-run enabled.

Variable Initialization Idiom

Line 80 uses a chain of dependent assignments: LET v=0: LET i=v: LET r=i: LET p=r. This initializes all four variables to zero while saving a small amount of memory and typing compared to four separate LET x=0 statements. It relies on each newly assigned variable being immediately available for use in the next statement on the same line.

Boolean Logic for Unknown Detection

The program exploits the fact that in Sinclair BASIC, numeric zero evaluates as false and any non-zero value evaluates as true. By having users enter 0 for unknown quantities, the program can use expressions like IF V AND I THEN (line 180) to test whether both values are known, or IF NOT V OR NOT I THEN GO TO ... (line 260) to skip a block when either value is missing. This is a clean and idiomatic technique that avoids string-based flags or extra boolean variables.

Early-Exit Input Skipping

The input section uses conditional jumps to skip requesting R and P once sufficient data is available. Line 180 jumps to 260 if both V and I are known (since V and I alone determine both P and R). Line 220 jumps to 260 if either V+R or I+R are known. This prevents the user from having to press Enter through unnecessary prompts, but it does mean that some valid two-variable combinations (e.g., V and P) require the user to first enter 0 for I and R before reaching the P prompt.

Formula Coverage

The calculation blocks cover all standard combinations of Ohm’s Law and the power formula:

LinesKnownDerived
270–280V, IP = V·I; R = V/I
300–310P, RI = √(P/R); V = √(P·R)
330–340P, IV = P/I; R = P/I²
370–380R, VP = V²·R; I = V/R
400–410I, RP = I²·R; V = I·R
430–440V, PR = V²/P; I = P/V

Notable Bugs and Anomalies

  • Line 370 formula error: The intended formula for power given V and R is P = V²/R, but the code reads (V^2)*R, which would give an incorrect result (it computes V²·R instead of V²/R). This is a genuine bug.
  • Zero input ambiguity: Since 0 is used as a sentinel for “unknown,” it is impossible to calculate results when any legitimate input value is actually zero (e.g., a short circuit with R=0). The program would treat it as an unknown.
  • Line 350 comment: REM ^ = raised to the power of is a helpful in-code annotation for users unfamiliar with the exponentiation operator.
  • Duplicate variable names: The program uses both lowercase v, i, r, p (initialized at line 80) and uppercase V, I, R, P (used in INPUT and calculations). In Sinclair BASIC, variable names are case-sensitive for single-letter numeric variables, so these are treated as distinct variables — the lowercase ones initialized at line 80 are never used again after initialization, making that initialization redundant.

Content

Appears On

Library tape of the Indiana Sinclair Timex User’s Group.

Related Products

Related Articles

Related Content

Image Gallery

Source Code

   10 REM //////////////////////
   20 REM /                    /
   30 REM /      OHM'S LAW     /
   40 REM /                    /
   50 REM //////////////////////
   60 PRINT "  OHM'S LAW  "
   70 CLS 
   80 LET v=0: LET i=v: LET r=i: LET p=r
   90 PRINT "Input known values"
  100 PRINT "  0 If unknown"
  120 PRINT '''"voltage     (V)=";
  130 INPUT V
  140 PRINT V;" V"
  150 PRINT ;"Current     (I)=";
  160 INPUT I
  170 PRINT I;" A"
  180 IF V AND I THEN GO TO 260
  190 PRINT ;"Resistance  (R)=";
  200 INPUT R
  210 PRINT R;" Ohms"
  220 IF V AND R OR I AND R THEN GO TO 260
  230 PRINT ;"Power       (P)=";
  240 INPUT P
  250 PRINT P;" W"
  260 IF NOT V OR NOT I THEN GO TO 290
  270 PRINT "Power        (P)=";V*I;" W"
  280 PRINT "Resistance   (R)=";V/I;" Ohms"
  290 IF NOT P OR NOT R THEN GO TO 320
  300 PRINT "Current      (I)=";SQR (P/R);" A"
  310 PRINT "Voltage      (V)=";SQR (P*R);" V"
  320 IF NOT P OR NOT I THEN GO TO 360
  330 PRINT "Voltage      (V)=";P/I;" V"
  340 PRINT "Resistance   (R)=";P/(I^2);" Ohms"
  350 REM ^ = raised to the power of
  360 IF NOT R OR NOT V THEN GO TO 390
  370 PRINT "Power        (P)=";(V^2)*R;" W"
  380 PRINT "Current      (I)=";V/R;" A"
  390 IF NOT I OR NOT R THEN GO TO 420
  400 PRINT "Power        (P)=";(I^2)*R;" W"
  410 PRINT "Voltage      (V)=";I*R;" V"
  420 IF NOT V OR NOT P THEN STOP 
  430 PRINT "Resistance   (R)=";(V^2)/P;" Ohms"
  440 PRINT "Current      (I)=";P/V;" A"
  445 STOP 
  450 SAVE "OHM'S LAW" LINE 1

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top