3.11 The null Statement

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

The reserved word null serves two purposes. It provides the null statement, illustrated below, and it provides the null value for variables of access types, covered later. The null or "do nothing" statement is similar to the "empty" statement of C or Pascal and the "continue" statement of COBOL or Fortran.

Sometimes developers temporarily implement a subprogram with a one-line "do nothing" executable part (to be later replaced with a more meaningful sequence). This allows them to compile, link and run a program that has partial functionality.

Im3-12.gif (2367 bytes)

The procedure shown here depends on two packages from the predefined environment.

A single example of a null statement appears in one branch (the others branch) of a case statement.

 

The null statement is often used in one or more branches of a case statement, because all possible values of the case expression must be included -- including those calling for no action. It could also appear in one or more branches of an if statement.

Source Code Listing

-----------------------------------------------------------
------------------ Show_Clock_Set_Advice ------------------
--  This procedure displays one or two lines of text, 
--  depending on the month. In April and October it advises
--  to set the clock ahead or back, respectively. 
-----------------------------------------------------------
with Ada.Text_IO, Ada.Calendar;
procedure Show_Clock_Set_Advice is
  package Tio renames Ada.Text_IO;
  package Cal renames Ada.Calendar;
  type Months is (Jan,Feb,Mar,Apr,May,Jun,
                  Jul,Aug,Sep,Oct,Nov,Dec);  
  Month_Num    : Cal.Month_Number;
  Month_Name   : Months;  
begin
     
  Month_Num  := Cal.Month(Cal.Clock);  
  Month_Name := Months'Val(Month_Num-1);
    
  case Month_Name is
    when Apr => 
      Tio.Put_Line("Set clock ahead one hour this month.");
    when Oct =>
      Tio.Put_Line("Set clock back one hour this month.");
    when others =>
      null;                               -- null statement
  end case; 
  
  Tio.Put_Line("End of clock advice");   
end Show_Clock_Set_Advice;
-----------------------------------------------------------

If the above program were run during April, the output would be:

     Set clock ahead one hour this month.
     End of clock advice

Related Topics

3.4 if Statements 3..5 case Statements
A.2 Simple and Compound Statements

[ Back to top of pagePrev ] Next ]