Back to manual index

:: goto and label

:: About: The goto statement in Pascal provides an unconditional jump from the goto to a label statement. goto forces a jump to a label, regardless of nesting. goto is mostly used to force a termination of heavily nested code. Never jump into or out of try statements, or into a loop or conditional block.

A label is a name for a location in the source code to which can be jumped to from another location with a goto statement

:: Use: goto label

:: Example (color coordinated to help follow code):
program testgoto;    
                     
label republican;    
label democrat;      
label finish;        
                     
var choice : string; 
                     
begin                
  writeln('"R" for republican, "D" for democrat');
  read(choice);       
                      
  if choice = 'R' then                      
    writeln('you choose republican');       
  if choice = 'R' then                      
    goto republican;                        
                                            
  if choice = 'D' then                      
    writeln('you choose democrat');         
  if choice = 'D' then                      
    goto democrat;                          
                                            
  goto finish;                              
                                            
republican:                                 
  writeln('Reagan');                        
  writeln('Bush');                          
  goto finish;                              
                                           
democrat:                                   
  writeln('Carter');                        
  writeln('Clinton');                       
                             
finish:                                    
  writeln('done');                          
                                            
end.