Java/JSP/Statements

Материал из Java эксперт
Перейти к: навигация, поиск

For loop: Using Two Loop Indexes

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using Two Loop Indexes</TITLE>
 </HEAD>
 <BODY>

Using Two Loop Indexes

   <%
       for (int i = 0, doubleIndex = 0; i <= 10;
           i++, doubleIndex = 2 * i) {
           out.println("i: " + i +
               " doubleIndex: " + doubleIndex + "
"); }  %> </BODY>

</HTML>

      </source>
   
  
 
  



Nested if Statements

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Nested if Statements</TITLE>
 </HEAD>
 <BODY>

Nested if Statements

   <%
       double value = 2;
       if (value != 0) {
           if (value > 0)
              out.println("The result = " + (1 / value));
           else
              out.println("Sorry, we need a positive number.");
       }
   %>
 </BODY>

</HTML>

      </source>
   
  
 
  



Testing for Multiple Conditions

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Testing for Multiple Conditions</TITLE>
 </HEAD>
 <BODY>

Testing for Multiple Conditions

   <%
       int temperature = 64;
       switch(temperature) {
           case 60:
           case 61:
           case 62:
               out.println("Sorry, too cold!");
               break;
           case 63:
           case 64:
           case 65:
               out.println("Pretty cool.");
               break;
           case 66:
           case 67:
               out.println("Nice!");
               break;
           case 70:
           case 74:
           case 75:
               out.println("Fairly warm.");
               break;
           default:
               out.println("Too hot!");
       }
   %>
 </BODY>

</HTML>

      </source>
   
  
 
  



Using an if-else Ladder

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using an if-else Ladder</TITLE>
 </HEAD>
 <BODY>

Using an if-else Ladder

   <%
       String day = "Friday";
       if(day == "Monday")
           out.println("It\"s Monday.");
       else if (day == "Tuesday")
           out.println("It\"s Tuesday.");
       else if (day == "Wednesday")
           out.println("It\"s Wednesday.");
       else if (day == "Thurssday")
           out.println("It\"s Thursday.");
       else if (day == "Friday")
           out.println("It\"s Friday.");
       else if (day == "Saturday")
           out.println("It\"s Saturday.");
       else if (day == "Sunday")
           out.println("It\"s Sunday.");
   %>
 </BODY>

</HTML>

      </source>
   
  
 
  



Using Compound Statements

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using Compound Statements</TITLE>
 </HEAD>
 <BODY>

Using Compound Statements

   <%
       int value = 10;
       if(value > 0) {
           out.println("The number was positive.");                
           out.println("Absolute value of " + value + " = " + value);
       }
   %>
 </BODY>

</HTML>

      </source>
   
  
 
  



Using the break Statement

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using the break Statement</TITLE>
 </HEAD>
 <BODY>

Using the break Statement

   <%
       double array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
       int sum = 0;
       for(int i = 0; i <
           array.length; i++) {
           sum += array[i];
           if (sum > 12) break;
           out.println("Looping...
"); } out.println("The sum exceeded the maximum allowed value.");  %> </BODY>

</HTML>

      </source>
   
  
 
  



Using the continue Statement

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using the continue Statement</TITLE>
 </HEAD>
 <BODY>

Using the continue Statement

   <%
       for(double i = 6; i >= -6; i--) {
           if (i == 0) continue;
           out.println("The reciprocal of " + i +
               " is " + (1 / i) + ".
"); }  %> </BODY>

</HTML>

      </source>
   
  
 
  



Using the if Statement

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using the if Statement</TITLE>
 </HEAD>
 <BODY>

Using the if Statement

   <%
       int i;
       for (i = 1; i <= 10; i++) {
           out.println("This is iteration number "
               + i + "
"); }  %> </BODY>

</HTML>

      </source>
   
  
 
  



Using the switch Statement

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using the switch Statement</TITLE>
 </HEAD>
 <BODY>

Using the switch Statement

   <%
       int day = 3;
       switch(day) {
           case 0:
               out.println("It\"s Sunday.");
               break;
           case 1:
               out.println("It\"s Monday.");
               break;
           case 2:
               out.println("It\"s Tuesday.");
               break;
           case 3:
               out.println("It\"s Wednesday.");
               break;
           case 4:
               out.println("It\"s Thursday.");
               break;
           case 5:
               out.println("It\"s Friday.");
               break;
           default:
               out.println("It must be Saturday.");
       }
   %>
 </BODY>

</HTML>

      </source>
   
  
 
  



Using the while Loop

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Using the while Loop</TITLE>
 </HEAD>
 <BODY>

Using the while Loop

   <%
       int value = 5;
       while (value > 0) {
           out.println("The value is now " + value-- + ".
"); }  %> </BODY>

</HTML>

      </source>
   
  
 
  



While: Finding a Factorial

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Finding a Factorial</TITLE>
 </HEAD>
 <BODY>

Finding a Factorial

   <%
       int value = 6, factorial = 1, temporaryValue = value;
       while (temporaryValue > 0) {
           factorial *= temporaryValue;
           temporaryValue--;
       }
       out.println("The factorial of " + value + " is " + factorial + ".");
   %>
 </BODY>

</HTML>

      </source>
   
  
 
  



While statement

   <source lang="java">

<HTML>

 <HEAD>
   <TITLE>Finding Reciprocals</TITLE>
 </HEAD>
 <BODY>

Finding Reciprocals

   <%
       double values[] = {4, 3, 2, 1, 0, 1, 2, 3, 4};
       int i = 0;
       while (values[i] != 0){
           out.println("The reciprocal = " + 1 / values[i++] + ".
"); }  %> </BODY>

</HTML>


      </source>