A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83292so82j4j6968gdd3lknhotcckald1fjs

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83292t4da0o99f5472mpssi3q6lu330f5qg0

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83292u9lonc3tr88r1ahg0vsuria4ic8gds6

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd8329327p3bsmogf1g917tn38nvl5m5pqvogo

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd8329331tlgormnkbvqg5384ohf2o33r6dfof

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd832937legcc0cb3l67pud88d45lghl0dbqqi

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd832937ve910g9km8d9gql60ic5fpi1muid0c

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd832938augf3aglffgq77qj71bbnvbv08feui

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd832938vdnbictu92iqt2ad0fiqqclp49hgp3

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83293eu995nhnl017dt513u9ri85s9kkaven

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83293i9q6nr075tr3sies5onae0r69frqe34

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83293js6eshsj7csmcfoktivub5e8g39p0gr

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83293l3sq9nd2okcjnr9mu8nahq9l8kal07d

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83293lavdjvlbujhr0lo2ik9tlgegoq6k3je

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: filemtime(): stat failed for D:\xampp_old\htdocs\ebookreading.net\application\writable1/ci_session21f21f1fe57975998845f4ef5bbd83293t6097uhsp5hskgmiqled9kmag6kuer8

Filename: controllers/Base.php

Line Number: 44

Backtrace:

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\Base.php
Line: 44
Function: filemtime

File: D:\xampp_old\htdocs\ebookreading.net\application\controllers\View.php
Line: 10
Function: __construct

File: D:\xampp_old\htdocs\ebookreading.net\index.php
Line: 380
Function: require_once

The foreach Statement

9.6. The foreach Statement

The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through a collection to get the desired information, but, to avoid unpredictable side effects, you should not use it to change the contents of the collection. The statement takes the following form:

foreach (type identifier in expression) statement

where

  • type is the type of identifier.

  • identifier is the iteration variable that represents the collection element.

  • expression is the object collection or array expression. The type of the collection element must be convertible to the identifier type.

  • statement is the embedded statement(s) to be executed.

The embedded statements continue to execute for each element in the array or collection. After the iteration has been completed for all the elements in the collection, control is transferred to the next statement following the foreach block. The foreach statement can be used to iterate through an array and any of the collections of the System.Collections namespace.

To iterate through a collection, the collection must meet specific requirements. Let's use the following foreach statement as an example:

foreach (ItemType item in myCollection)

myCollection must meet the following requirements:

  • The collection type must be one of the types interface, class, or struct.

  • The collection type must include an instance method named GetEnumerator that returns a type—for example, Enumerator (explained next).

  • The type Enumerator (a class or struct) must contain a property named Current that returns ItemType or a type that can be converted to it. The property accessor returns the current element of the collection.

  • The type Enumerator must contain a bool method named MoveNext, which increments the item counter and returns true if there are more items in the collection.

Listing 9.8 is an example of the foreach statement in action.

Listing 9.8. The foreach Statement
using System;
using System.Collections;

public class Test {

  public static void Main(string[] args) {
    //foreach on an array
    int[] arr = new int[] {1,2,2,3};
    foreach (int i in arr) {
      Console.WriteLine(i);
    }
    //foreach on an array
    foreach (int i in arr) {
      Console.WriteLine(i);
    }
  }
}

Unlike the for loop, the iterating primitive or object of the foreach statement is read-only. That is, for (int i =0; i <10; ++i) lets you change the value of i inside the loop, but foreach (int i in arr) doesn't let you change the value of i inside its loop. Performance-conscious Java programmers will be quick to ask whether the foreach construct is faster than the for construct for the situation shown in Listing 9.8 (iterating over an array of integers). As always, the proof is in the pudding, so Listing 9.9 shows the pudding.

Listing 9.9. Which Is Faster: foreach or for?
using System;

public class Test {

  public static void Main(string[] args) {
    int NUM = 10000000;
    int[] arr = new int[NUM];
    for (int i =0; i < NUM; ++i) {
      arr[i] = i+1;
    }
    long t1 = System.Environment.TickCount;
    for (int i =0; i < NUM; ++i);
    long t2 = System.Environment.TickCount;
    foreach (int i in arr);
    long t3 = System.Environment.TickCount;
    Console.WriteLine("for loop "+(t2-t1)+" ms");
    Console.WriteLine("foreach loop "+(t3-t2)+" ms");
  }
}

Here is a sample output of Listing 9.9:

for loop 80 ms
foreach loop 391 ms

Note that the actual output of this listing may vary from one run to another. A single test should not dictate the choice of the appropriate construct for solving your problem. Choosing between for and foreach is also a matter of design, code consistency, and readability. For iterating over collections, using the foreach construct is more intuitive than using the for loop.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.222.47