1 / 13

Re-writing the for -statement using a while -statement and vice versa

Re-writing the for -statement using a while -statement and vice versa. Relationship between the while -statement and the for -statement. The for -statement and the while -statement have many aspects in common They can often replace each other.

armands
Download Presentation

Re-writing the for -statement using a while -statement and vice versa

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Re-writing the for-statement using a while-statement and vice versa

  2. Relationship between the while-statement and the for-statement • The for-statement and the while-statement have many aspects in common They can often replace each other.

  3. Relationship between the while-statement and the for-statement (cont.) • Facts: • A while-statement can always be re-written using a for-statement • A for-statement that does not contain any continue-statement in its body, can be re-written using a while-statement.

  4. Re-writing a while-statement using a for-statement • While-statement:

  5. Re-writing a while-statement using a for-statement (cont.) can be written as follows using a for-statement: (We simply use an empty statement as the INIT-STATEMENT and as the INCR-STATEMENT)

  6. Re-writing a while-statement using a for-statement (cont.) • Example: While-statement Equivalent for-statement a = 1; while ( a <= 10 ) { System.out.println(a); a++; } a = 1; for ( ; a <= 10 ; ) { System.out.println(a); a++; }

  7. Re-writing a for-statement using a while-statement • For-statement:

  8. Re-writing a for-statement using a while-statement (cont.) that does not contain a continue-statement, can be written as follows using a while-statement: (We move the INIT-STATEMENTbefore the while-statement and put the INCR-STATEMENT as the last statement in the body)

  9. Re-writing a for-statement using a while-statement (cont.) • Example: For-statement Equivalent while-statement a = 1; while ( a <= 10 ) { System.out.println(a); a++; } for ( a = 1 ; a <= 10 ; a++ ) { System.out.println(a); }

  10. Re-writing a for-statement using a while-statement (cont.) • Note: • A for-statement containing a continue-statement may jump to the INCR-STATEMENT • Because a while-statement does not have an INCR-STATEMENT, it can never jump to it.... Thus: a for-statement containing a continue-statementcannot be re-written as a while-statement

  11. Abusing the Java for-statement • If a for-statement does not contain any continue statements, you can re-write the for-statement as follows:

  12. Abusing the Java for-statement (cont.) • Example: Original for-statement Abused for-statement a = 1; for ( ; a <= 10 ; ) { System.out.println(a); a++; } for ( a = 1 ; a <= 10 ; a++ ) { System.out.println(a); }

  13. Abusing the Java for-statement (cont.) • Example Program: (Shows the abused for-loop) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/For02.java • How to run the program:             • Right click on link and save in a scratch directory • To compile:   javac For02.java • To run:          java For02

More Related