Flow-control structures: Difference between revisions

From Rosetta Code
Content added Content deleted
(+ Forth exceptions)
(add C)
Line 22: Line 22:
end loop;
end loop;
end loop;
end loop;

=={{header|C}}==
===goto===
One common use of goto in C is to break out of nested loops.
int main()
{
int i,j;
for (j=1; j<1000; j++) {
for (i=0; i<j, i++) {
if (exit_early())
goto out;
/* etc. */
}
}
out:
return 0;
}


==[[C plus plus|C++]]==
==[[C plus plus|C++]]==
[[Category:C plus plus]]
[[Category:C plus plus]]
==== goto ====
=== goto ===
'''Compiler:''' [[GCC]] 3.3.4
'''Compiler:''' [[GCC]] 3.3.4
#include <iostream>
#include <iostream>
Line 38: Line 55:
Note that "goto" may also be used in conjunction with other forms of branching.
Note that "goto" may also be used in conjunction with other forms of branching.


==== Exceptions ====
=== Exceptions ===
'''Compiler:''' [[GCC]] 4.0.2
'''Compiler:''' [[GCC]] 4.0.2


Line 182: Line 199:
==[[Tcl]]==
==[[Tcl]]==
[[Category:Tcl]]
[[Category:Tcl]]
==== exception ====
=== exception ===
Tcl's catch command can be used to provide a basic exception-handling mechanism:
Tcl's catch command can be used to provide a basic exception-handling mechanism:
if {[catch { ''... code that might give error ...'' } result]} {
if {[catch { ''... code that might give error ...'' } result]} {
Line 190: Line 207:
}
}


==== custom control structures ====
=== custom control structures ===
A novel aspect of Tcl is that it's relatively easy to create new control structures (more detail at http://wiki.tcl.tk/685).
A novel aspect of Tcl is that it's relatively easy to create new control structures (more detail at http://wiki.tcl.tk/685).
Eg. defining a command to perform some operation for each line of an input file:
Eg. defining a command to perform some operation for each line of an input file:

Revision as of 17:23, 30 September 2007

Task
Flow-control structures
You are encouraged to solve this task according to the task description, using any language you may know.
Control Structures

These are examples of control structures. You may also be interested in:


In this task, we document common flow-control structures. One common example of a flow-control structure is the goto construct. Note that Conditional Structures and Loop Structures have their own articles.

Ada

goto

Top:
   Put_Line("Hello, World");
   goto Top;

exit

Exit is used to break out of loops. Exit can be used with a label to break out of an inner loop to an outer loop and its enclosing outer loop

Outer:
   loop
      -- do something
      loop
         -- do something else
         exit Outer; -- exits both the inner and outer loops
      end loop;
   end loop;

C

goto

One common use of goto in C is to break out of nested loops.

int main()
{
  int i,j;
  for (j=1; j<1000; j++) {
    for (i=0; i<j, i++) {
      if (exit_early())
        goto out;
      /* etc. */
    }
  }
out:
  return 0;
}

C++

goto

Compiler: GCC 3.3.4

#include <iostream> 

int main()
{
 LOOP:
  std::cout << "Hello, World!\n";
 goto LOOP;
}

Note that "goto" may also be used in conjunction with other forms of branching.

Exceptions

Compiler: GCC 4.0.2

Exceptions are a way to give control back to a direct or indirect caller in case of an error.

#include <iostream>
#include <ostream>

void foo()
{
  std::cout << "Going to throw an exception.\n";
  throw 7; // almost any object can be thrown, including ints
  std::throw << "This output will never execute.\n";
}

void bar()
{
  std::cout << "Going to call foo().\n";
  foo();
  std::cout << "This will be skipped by the exception coming from foo.\n";
}

void baz()
{
  try // everything thrown from inside the following code block
  {   // will be covered by the following catch clauses
    std::cout << "Going to call bar().\n";
    bar();
    std::cout << "This will be skipped by the exception coming from foo.\n";
  }
  catch(...) // a simple catch-all, but doesn't give access to the thrown exception
  {
    std::cout << "An exception occured. I'll just throw it on.\n";
    throw; // without an argument, the caught exception is re-thrown
  }
  std::cout << "This will not be executed due to the re-throw in the catch block\n";
}

void foobar()
{
  try
  {
    baz();
  }
  catch(char const* s)
  {
    std::cout << "If foo had thrown a char const*, this code would be executed.\n";
    std::cout << "In that case, the thrown char const* would read " << s << ".\n";
  }
  catch(int i)
  {
    std::cout << "Caught an int, with value " << i << " (should be 7).\n";
    std::cout << "Not rethrowing the int.\n";
  }
  catch(...)
  {
    std::cout << "This catch-all doesn't get invoked because the catch(int) above\n"
              << "already took care of the exception (even if it had rethrown the\n"
              << "exception, this catch-all would not be invoked, because it's\n"
              << "only invoked for exceptions coming from the try block.\n";
  }
  std::cout << "This will be executed, since the exception was handled above, and not rethrown.\n";
}

int main()
{
  try
  {
    foobar();
  }
  catch(...)
  {
    std::cout << "The main function never sees the exception, because it's completely handled\n"
              << "inside foobar(). Thus this catch-all block never gets invoked.\n";
  }
}

Forth

CATCH-THROW

Forth does not have goto, but the standard does have an exception mechanism.

: checked-array
  CREATE ( size -- ) DUP , CELLS ALLOT
  DOES> ( i -- a+i )
    2DUP @ 0 SWAP WITHIN IF
      SWAP 1+ CELLS +
    ELSE
      1 THROW
    THEN ;

8 checked-array myarray

: safe-access ( i -- a[i] )
  ['] myarray CATCH 1 = IF ." Out of bounds!" 0 THEN ;

IDL

goto

 test:
 ..some code here
 goto, test

(This is almost never used)

on_error

 on_error, test

(This resumes at the label test if an error is encountered)

on_ioerror

 on_ioerror, test

(Same as on_error, but for EOFs and read-errors and such)

break

break

immediately terminates the innermost current loop (or if or case etc)

continue

continue

immediately starts the next iteration of the current innermost loop

Perl

Interpreter: Perl 5.x

goto

Goto is typically looked down upon by most perl programmers

FORK:
# some code
goto FORK;

Tcl

exception

Tcl's catch command can be used to provide a basic exception-handling mechanism:

 if {[catch { ... code that might give error ... } result]} {
   puts "Error was $result"
 } else {
   ... process $result ...
 }

custom control structures

A novel aspect of Tcl is that it's relatively easy to create new control structures (more detail at http://wiki.tcl.tk/685). Eg. defining a command to perform some operation for each line of an input file:

 proc forfilelines {linevar filename code} {
   upvar $linevar line	; # connect local variable line to caller's variable
   set filechan [open $filename]
   while {[gets $filechan line] != -1} {
     uplevel 1 $code	; # Run supplied code in caller's scope
   }
   close $filechan
 }

Now use it to print the length of each line of file "mydata.txt":

 forfilelines myline mydata.txt {
   puts [string length $myline]
 }