Chapter 5 Program Control Statement (2)

xiaoxiao2021-03-06  40

Sample.java

Public class sample {public static void main (string [] args) {Int A, b; b = 4; for (a = 1; a

Nested.java

// Loops May Be Nested.public Class Nested {Public Static Void Main (String [] args) {INT I, J; For (i = 0; i <10; i ) {for (j = i; j <10; J ) system.out.print ("."); system.out.println ();}}}

Breakloop.java

// Using break to exit a loop.public class breakloop {public static void main (string [] args) {for (int i = 0; i <100; i ) {if (i == 10) BREAK; // Terminate Loop if i is 10 system.out.println ("i:" i);} system.out.println ("loop completion.");}}

Breakloop2.java

// Using break to exit a while loop.public class breakloop2 {public static void main (string [] args) {INT i = 0; while (i <100) {if (i == 10) Break; // Terminate Loop IF i is 10 system.out.println ("i:" i); i ;} system.out.println ("Loop Complete.");}} Breakloop3.java

// Using break with nested loops.public class breakloop3 {public static void main (string [] args) {for (int i = 0; i <3; i ) {system.out.print ("Pass" i " : "); For (int J = 0; j <100; j ) {IF (j == 10) Break; // Terminate loop if j is 10 system.out.print (j " ");} system. Out.println ();} system.out.println ("Loop Complete.");}}

Break.java

// Using Break as a civilized form of goto.public class break {public static void main (string [] args) {boolean t = true; first: {second: {third: {system.out.println ("Before the Break "); If (t) Break second; // Break out of second block system.out.println (" this won't execute ");} system.out.println (" this won't execute ");} System.out.println ("this is instator secret block");}}}

Breakloop4.java

// Using break to exit from nested loops.public class breakloop4 {public static void main (string [] args) {Outer: for (INT i = 0; i <3; i ) {system.out.print ("pass" i ":"); for (int J = 0; j <100; j ) {IF (j == 10) Break outer; // exit Both loops system.out.print (j ");} System.out.println ("this will not print");} system.out.println ("Loops Complete.");}} Continue.java

// DemonStrate Continue.public class continue {public static void main (string [] args) {for (int i = 0; i <10; i ) {system.out.print (i "); if (i% 2 == 0) Continue; system.out.println ("");}}}

Continuelabel.java

// USING CONTINUE with a label.public class continuelabel {public static void main (string [] args) {Outer: for (int i = 0; i <10; i ) {for (int J = 0; J <10; J ) {if (j> i) {system.out.println (); contract.Print (" (i * j));}}}}}}

Return.java

// DemonStrate ReturnPublic Class Return {public static void main (string [] args) {boolean t = true; system.out.println ("before the return."); If (t) return; // Return to Caller System. Out.println ("this Won't execute.");}}

转载请注明原文地址:https://www.9cbs.com/read-55625.html

New Post(0)