Control structures are fundamental in any programming language as they allow you to control the flow of your program. In MUMPS, control structures include conditional statements and loops. This section will cover the basic control structures: IF, ELSE, FOR, and WHILE.

IF and ELSE Statements

The IF statement allows you to execute a block of code only if a specified condition is true. The ELSE statement can be used to execute a block of code if the condition is false.

Syntax

IF condition DO
. . . ; code to execute if condition is true
ELSE  DO
. . . ; code to execute if condition is false

Example

SET age=20
IF age<18 DO
. WRITE "You are a minor.",!
ELSE  DO
. WRITE "You are an adult.",!

Explanation

  • SET age=20: Initializes the variable age with the value 20.
  • IF age<18: Checks if age is less than 18.
  • WRITE "You are a minor.",!: Executes if the condition is true.
  • ELSE DO: Executes if the condition is false.
  • WRITE "You are an adult.",!: Executes if the condition is false.

FOR Loop

The FOR loop is used to execute a block of code a specific number of times.

Syntax

FOR variable=start:step:end DO
. . . ; code to execute in each iteration

Example

FOR i=1:1:5 DO
. WRITE "Iteration: ", i, !

Explanation

  • FOR i=1:1:5: Initializes i to 1, increments i by 1 in each iteration, and stops when i exceeds 5.
  • WRITE "Iteration: ", i, !: Prints the current value of i in each iteration.

WHILE Loop

The WHILE loop executes a block of code as long as a specified condition is true.

Syntax

SET variable=start
WHILE condition DO
. . . ; code to execute while condition is true

Example

SET count=1
WHILE count<=5 DO
. WRITE "Count: ", count, !
. SET count=count+1

Explanation

  • SET count=1: Initializes the variable count with the value 1.
  • WHILE count<=5: Checks if count is less than or equal to 5.
  • WRITE "Count: ", count, !: Prints the current value of count.
  • SET count=count+1: Increments count by 1 in each iteration.

Practical Exercises

Exercise 1: Using IF and ELSE

Write a MUMPS program that checks if a number is positive, negative, or zero and prints an appropriate message.

Solution

SET number=-5
IF number>0 DO
. WRITE "The number is positive.",!
ELSE  IF number<0 DO
. WRITE "The number is negative.",!
ELSE  DO
. WRITE "The number is zero.",!

Exercise 2: Using FOR Loop

Write a MUMPS program that prints the first 10 natural numbers.

Solution

FOR i=1:1:10 DO
. WRITE i, !

Exercise 3: Using WHILE Loop

Write a MUMPS program that prints the numbers from 1 to 5 using a WHILE loop.

Solution

SET count=1
WHILE count<=5 DO
. WRITE count, !
. SET count=count+1

Common Mistakes and Tips

  • Indentation: Properly indent your code within control structures to improve readability.
  • Infinite Loops: Ensure that the condition in a WHILE loop will eventually become false to avoid infinite loops.
  • Condition Syntax: Double-check the syntax of your conditions to avoid logical errors.

Conclusion

In this section, you learned about the basic control structures in MUMPS: IF, ELSE, FOR, and WHILE. These constructs allow you to control the flow of your program based on conditions and iterations. Practice these concepts with the provided exercises to reinforce your understanding. In the next module, we will delve into working with data, starting with an introduction to global variables.

© Copyright 2024. All rights reserved