quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif File Iterative Control

In e the way we open and read ASCII files is slightly different then C and C++. There are two types of looping constructs that are provided to help with handling ASCII files.

   

space.gif

  • for each line in file
  • for each file matching
   

space.gif

  ../images/main/bulllet_4dots_orange.gif for each line in file

Executes the action block for each line in the text file file-name. Inside the block, it (or optional name) refers to the current line (as string) without the final "\n" (the final new line character, CR).

   

space.gif

Syntax :

   

space.gif

for each [line] [(name)] in file file-name-exp [do] {action; ...}

   

space.gif

  ../images/main/bullet_star_pink.gif Example - for each line in file
   

space.gif

Lets consider a ASCII file file.txt containing below lines

   

space.gif

 This is first line
 This is second line
 This is third line
   

space.gif

Lets write a e program to read this file and print it

   

space.gif


 1 <'
 2 extend sys {
 3   run() is also {
 4     for each line in file "file.txt" {
 5       out (it);
 6     };
 7   };
 8 };
 9 '>
You could download file flow_control9.e here
   

space.gif

Simulator Output

   

space.gif

This is first line
This is second line
This is third line
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif for each file matching

For each file (in the file search path) whose name matches file-name-exp execute the action block. Inside the block, it (or optional name) refers to the matching file name.

   

space.gif

Syntax

   

space.gif

for each file [(name)] matching file-name-exp [do] {action; ...}

   

space.gif

   

space.gif

  ../images/main/bullet_star_pink.gif Example - for each file matching
   

space.gif


  1 <'
  2 extend sys {
  3   run() is also {
  4     for each file (file_name) matching "f*.txt" {
  5       outf("Found file %s\n",file_name);
  6       for each line in file file_name {
  7         out (it);
  8       };
  9     };
 10   };
 11 };
 12 '>
You could download file flow_control10.e here
   

space.gif

Simulator Output

   

space.gif

Found file file.txt
This is first line
This is second line
This is third line
   

space.gif

  ../images/main/bullet_green_ball.gif Controlling the Program Flow

The actions described in this section are used to alter the flow of the program in places where the flow would otherwise continue differently. The e language provides the following actions for controlling the program flow:

   

space.gif

  • break
  • continue
   

space.gif

  ../images/main/bulllet_4dots_orange.gif break

Breaks the execution of the nearest enclosing iterative action (for or while). When a break action is encountered within a loop, the execution of actions within the loop is terminated, and the next action to be executed is the first one following the loop.

   

space.gif

Syntax

   

space.gif

break

   

space.gif

  ../images/main/bulllet_4dots_orange.gif continue

Stops the execution of the nearest enclosing iteration of a for or a while loop, and continues with the next iteration of the same loop. When a continue action is encountered within a loop, the current iteration of the loop is aborted, and execution continues with the next iteration of the same loop.

   

space.gif

You cannot place continue actions outside the scope of a loop

   

space.gif

Syntax

   

space.gif

continue

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Example - break and continue
   

space.gif


  1 <'
  2 extend sys {
  3   run() is also {
  4     var i : int = 0;
  5     var j : int = 0;
  6     for {i=0; i < 15; i = i + 1} do { 
  7       outf("Current value of i : %d\n",i); 
  8       outf("Current value of j : %d\n",j); 
  9       if ( i < 3) {
 10         continue;
 11       } else {
 12          j = j + 1;
 13       };
 14       if (j > 4) {
 15         break;
 16       };
 17     }; 
 18   };
 19 };
 20 '>
You could download file flow_control8.e here
   

space.gif

Simulator Output

   

space.gif

Current value of i : 0
Current value of j : 0
Current value of i : 1
Current value of j : 0
Current value of i : 2
Current value of j : 0
Current value of i : 3
Current value of j : 0
Current value of i : 4
Current value of j : 1
Current value of i : 5
Current value of j : 2
Current value of i : 6
Current value of j : 3
Current value of i : 7
Current value of j : 4
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com