Skip to content

Commit 34e555f

Browse files
authored
Merge pull request certik#38 from pdebuyl/master
move control flow to a dedicated section
2 parents 4996d43 + 7da9de7 commit 34e555f

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

src/rosetta.rst

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,55 @@ And use it like:
730730
| -7.69468277489e-16 | -7.69468277488715811E-016 |
731731
+-----------------------------------------------------------+------------------------------------------------------------------------+
732732

733+
Control flow in loops
734+
---------------------
735+
736+
The common loop types in Python and Fortran are the ``for`` and ``do`` loops
737+
respectively. It is possible to skip a single loop or to stop the execution of a loop in
738+
both languages but the statements to do so differ.
739+
740+
break and exit statements
741+
~~~~~~~~~~~~~~~~~~~~~~~~~
742+
743+
In Python, ``break`` is used to stop the execution of the innermost loop. In Fortran, this
744+
is accomplished by the ``exit`` statement. For named loops, it is possible to speficy which
745+
loop is affected by appending its name to the ``exit`` statement. Else, the innermost loop
746+
is interrupted.
747+
748+
Python's ``exit()`` interrupts the execution of program or of an interactive session.
749+
750+
+------------------------------------------------------+--------------------------------------------------------+
751+
| NumPy | Fortran |
752+
+------------------------------------------------------+--------------------------------------------------------+
753+
|:: |.. code-block:: fortran |
754+
| | |
755+
| for i in range(1, 9): | loop_name: do i = 1, 8 |
756+
| if i>2: | if (i>2) exit loop_name |
757+
| break | print *, i |
758+
| print i | end do loop_name |
759+
+------------------------------------------------------+--------------------------------------------------------+
760+
761+
continue and cycle statements
762+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
763+
764+
Python's ``continue`` statement is used to skip the rest of a loop body. The
765+
loop then continues at its next iteration cycle. Fortran's ``continue``
766+
statement does not do anything and one should use ``cycle`` instead. For named
767+
loops, it is possible to speficy which loop is affected by appending its name
768+
to the ``cycle`` statement.
769+
770+
+------------------------------------------------------+--------------------------------------------------------+
771+
| NumPy | Fortran |
772+
+------------------------------------------------------+--------------------------------------------------------+
773+
|:: |.. code-block:: fortran |
774+
| | |
775+
| for i in range(1, 9): | loop_name: do i = 1, 8 |
776+
| if i%2 == 0: | if (modulo(i, 2) == 0) cycle loop_name |
777+
| continue | print *, i |
778+
| print i | end do loop_name |
779+
+------------------------------------------------------+--------------------------------------------------------+
780+
781+
733782
Examples
734783
--------
735784

@@ -917,28 +966,5 @@ This prints::
917966

918967
1.4207732655565537 1.6556111085593115 0.53462502018670921
919968

920-
Caveats
921-
-------
922-
923-
continue statement
924-
~~~~~~~~~~~~~~~~~~
925-
926-
Python's ``continue`` statement is used to skip the rest of a loop body. The
927-
loop then continues at its next iteration cycle. Fortran's ``continue``
928-
statement does not do anything and one should use ``cycle`` instead. For named
929-
loops, it is possible to speficy which loop is affected by appending its name
930-
to the ``cycle`` statement.
931-
932-
+------------------------------------------------------+--------------------------------------------------------+
933-
| NumPy | Fortran |
934-
+------------------------------------------------------+--------------------------------------------------------+
935-
|:: |.. code-block:: fortran |
936-
| | |
937-
| for i in range(1, 9): | loop_name: do i = 1, 8 |
938-
| if i%2 == 0: | if (modulo(i, 2) == 0) cycle loop_name |
939-
| continue | print *, i |
940-
| print i | end do loop_name |
941-
+------------------------------------------------------+--------------------------------------------------------+
942-
943969

944970
.. :: vim: set nowrap textwidth=0 syn=off: ~

0 commit comments

Comments
 (0)