2

Buenas ando haciendo unas tablas que imprrimen numeros de parimpar y de 5 en 5 pero ya los hice en for y while pero cuando intento hacerlo en do while me arroja otro resultado no se a que se deba

este es con el for:

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>Este ejercicio es del for</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>            
  <table class="table">
    <thead>

        <th>1 a 11</th>
        <th>11 a 1</th>
        <th>  par </th>
    <th> impar </th>
    <th> 5 en 5 </th>
    </thead>
    <tbody>
      <?php

for($i=1; $i<12; $i++)
{
    echo "<tr>";
echo "<td>".($i). "</td>";
echo "<td>".(11-$i)."</td>";
echo "<td>".($i * 2 )."</td>";
echo "<td>".($i * 2 -1)."</td>";
echo "<td>".($i * 5 - 5)."</td>";
echo "</tr>";

}
?>
    </tbody>
  </table>
</div>

</body>
</html>

ESTE ES CON EL WHILE:

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>Este ejercicio es del while</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>            
  <table class="table">
    <thead>

        <th>1 a 11</th>
        <th>11 a 1</th>
        <th>  par </th>
    <th> impar </th>
    <th> 5 en 5 </th>
    </thead>
    <tbody>


<?php
$i=1;

while($i<12)
{
   echo "<tr>";
echo "<td>" .$i."</td>";
echo "<td>".(11-$i)."</td>";
echo "<td>".($i * 2 )."</td>";
echo "<td>".($i * 2 -1)."</td>";
echo "<td>".($i * 5 - 5)."</td>";
echo "</tr>";


   $i++;
}

?>

</tbody>
  </table>
</div>

</body>
</html>

Y ESTE CON EL DO WHILE PEOR YA NO ME ARROJA EL MISMO RESULTADO

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>Este ejercicio es del for</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>            
  <table class="table">
    <thead>

        <th>1 a 11</th>
        <th>11 a 1</th>
        <th>  par </th>
    <th> impar </th>
    <th> 5 en 5 </th>
    </thead>
    <tbody>

<?php
$i=1;

do{

echo "<tr>";
echo "<td>".($i). "</td>";
echo "<td>".(11-$i)."</td>";
echo "<td>".($i * 2 )."</td>";
echo "<td>".($i * 2 -1)."</td>";
echo "<td>".($i * 5 - 5)."</td>";
echo "</tr>";



} while($i<12);


?>
 </tbody>
  </table>
</div>

</body>
</html>

NO SE A QUE SE DEBA QUE ARROJA OTRO RESULTADO

1 respuesta 1

1

Te falta incrementar el valor de la variable $i en cada iteracción:

do{

    echo "<tr>";
    echo "<td>".($i). "</td>";
    echo "<td>".(11-$i)."</td>";
    echo "<td>".($i * 2 )."</td>";
    echo "<td>".($i * 2 -1)."</td>";
    echo "<td>".($i * 5 - 5)."</td>";
    echo "</tr>";

    /*Aquí hay que incrementar*/
    $i++;

} while($i<12);

Resultado:

table {
  border-collapse: collapse;
}

td, th {
  border: 1px solid #999;
  padding: 0.5rem;
  text-align: left;
}
<div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
  <table class="table">
    <thead>

      <th>1 a 11</th>
      <th>11 a 1</th>
      <th> par </th>
      <th> impar </th>
      <th> 5 en 5 </th>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>10</td>
        <td>2</td>
        <td>1</td>
        <td>0</td>
      </tr>
      <tr>
        <td>2</td>
        <td>9</td>
        <td>4</td>
        <td>3</td>
        <td>5</td>
      </tr>
      <tr>
        <td>3</td>
        <td>8</td>
        <td>6</td>
        <td>5</td>
        <td>10</td>
      </tr>
      <tr>
        <td>4</td>
        <td>7</td>
        <td>8</td>
        <td>7</td>
        <td>15</td>
      </tr>
      <tr>
        <td>5</td>
        <td>6</td>
        <td>10</td>
        <td>9</td>
        <td>20</td>
      </tr>
      <tr>
        <td>6</td>
        <td>5</td>
        <td>12</td>
        <td>11</td>
        <td>25</td>
      </tr>
      <tr>
        <td>7</td>
        <td>4</td>
        <td>14</td>
        <td>13</td>
        <td>30</td>
      </tr>
      <tr>
        <td>8</td>
        <td>3</td>
        <td>16</td>
        <td>15</td>
        <td>35</td>
      </tr>
      <tr>
        <td>9</td>
        <td>2</td>
        <td>18</td>
        <td>17</td>
        <td>40</td>
      </tr>
      <tr>
        <td>10</td>
        <td>1</td>
        <td>20</td>
        <td>19</td>
        <td>45</td>
      </tr>
      <tr>
        <td>11</td>
        <td>0</td>
        <td>22</td>
        <td>21</td>
        <td>50</td>
      </tr>
    </tbody>
  </table>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.