2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GoToEatで学ぶgoto文

Posted at

コード

GoToEat使った場合

goto_eat.php
<?php
echo("よーし、GoToEatで安くご飯を食べるぞー!\n");
$price = 3000;
goto Eat;

Eat:
$price = 1000;

Finish:
echo("こちらのご飯は{$price}円です\n");

↓結果

よーし、GoToEatで安くご飯を食べるぞー!
こちらのご飯は1000円です

GoToEat使わなかった場合

normal_eat.php
<?php
echo("よーし、GoToEatで安くご飯を食べるぞー!\n");
$price = 3000;
goto Finish;

Eat:
$price = 1000;

Finish:
echo("こちらのご飯は{$price}円です\n");

↓結果

よーし、GoToEatで安くご飯を食べるぞー!
こちらのご飯は3000円です

GoToEatを使った方がお安くなった。

練習問題

次の結果、ご飯はいくらになるでしょう?

goto_eat_problem.php
<?php
echo("よーし、GoToEatをいっぱい使って安くご飯を食べるぞー!\n");
$price = 10000;

Use1:
goto Eat1;
Use2:
goto Eat2;
Use3:
goto Eat3;
Use4:
goto Finish;
goto Eat4;
Use5:
goto Eat5;

Eat1:
$price -= 100;
goto Use5;
Eat2:
$price -= 200;
goto Use4;
Eat3:
$price -= 300;
goto Use2;
Eat4:
$price -= 400;
goto Use1;
Eat5:
$price -= 500;
goto Use3;

Finish:
echo("こちらのご飯は{$price}円です\n");
答え 8900円

おまけ

if_eat.php
<?php
echo("よーし、GoToEatで安くご飯を食べるぞー!\n");
$price = 3000;
$GoToEat = true;
if($GoToEat){
    $price = 1000;
}
echo("こちらのご飯は{$price}円です\n");

↓結果

よーし、GoToEatで安くご飯を食べるぞー!
こちらのご飯は1000円です
switch_eat.php
<?php
echo("よーし、GoToEatで安くご飯を食べるぞー!\n");
$price = 3000;
$GoToEat = true;
switch($GoToEat){
    case true:
        $price = 1000;
        break;
}
echo("こちらのご飯は{$price}円です\n");

↓結果

よーし、GoToEatで安くご飯を食べるぞー!
こちらのご飯は1000円です
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?