1
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.

【PHP基礎⑪】ループ処理-1.for文とwhile文

Last updated at Posted at 2022-06-20

[問題]  (参照:http://www.cc.kyoto-su.ac.jp/~mmina/bp1/hundredKnocksBasic.html)
Hello Ruki!を10回繰り返して表示するプログラムを作成しなさい。

1.for文の場合

コード

for($a = 1; $a <= 10; $a++){
    echo "Hello Ruki! \n";
}

結果

Hello Ruki! 
Hello Ruki! 
Hello Ruki! 
Hello Ruki! 
Hello Ruki! 
Hello Ruki! 
Hello Ruki! 
Hello Ruki! 
Hello Ruki! 
Hello Ruki! 

2.while文の場合

コード

$a = 0;
while($a < 10){
    echo 'Hello Ruki!',PHP_EOL;
    $a++;
}

結果

Hello Ruki!
Hello Ruki!
Hello Ruki!
Hello Ruki!
Hello Ruki!
Hello Ruki!
Hello Ruki!
Hello Ruki!
Hello Ruki!
Hello Ruki!

☆ループ処理~for文とwhile文~

<for文>
for(①カウンタの初期値; ②ループ処理の条件式; ③増減式){
実行する処理;
}

①:はじめの値
②:どのような条件のところまで処理を行うか
③:処理1回毎に、値をいくつ増やしたり減らしたりするか

<while文>
初期値;
while(条件式){
実行する処理;
  増減式;

☆改行
~,PHP_EOL;

" \n";  ← ' 'はダメ。" "を用いる!

1
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
1
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?