プログラム言語 制御文

三項演算子

変数 = 条件式 ? 条件==true時の処理 : 条件==false時の処理

例(1)
int count = 25;
int ans = count > 20 ? 20 : count;
⇒ans : 20

↓ と同じ意味
if (count > 20) {
  ans = 20;
}else{
  ans = count;
}

例(2)
int count = 5;
int ans = count > 20 ? 20 : count;
⇒ans : 5

変数 = If( 条件式 , 条件=true時の処理 , 条件==false時の処理 )

例(1)
Dim count As Integer = 25
Dim ans As Integer = If(count > 20, 20, count)
⇒ans : 20

↓ と同じ意味
If count > 20 Then
  ans = 20
Else
  ans = count
End If

例(2)
Dim count As Integer = 5
Dim ans As Integer = If(count > 20, 20, count)
⇒ans : 5

ret = True
x = 'BIG' if ret else 'SMALL'
BIG

変数 = 条件式 ? 条件==true時の処理 : 条件==false時の処理

例(1)
var count = 25;
var ans = count > 20 ? 20 : count;
⇒ans : 20

↓ と同じ意味
if (count > 20) {
  ans = 20;
}else{
  ans = count;
}

例(2)
var count = 5;
var ans = count > 20 ? 20 : count;
⇒ans : 5

If文

string myStr = "aa";

if (myStr == null) {
  ~
} else if ( myStr == "aa" ) {
  ~
} else {
  ~
}

※処理が1行のみ場合の省略形
if (a == 5) a = 10;
if (a == 5) {
  a = 10;
}

Dim myStr As String = "aa"

If myStr = vbNull Then
  ~
ElseIf myStr = "bb" Then
  ~
Else
  ~
End If

※処理が1行のみ場合の省略形
if a = 5 Then a = 10
if a = 5 Then
  a = 10
End If

if (true){
  System.out.println(~);
}else if (true){
  System.out.println(~);
}else{
  if (true){
    System.out.println(~);
  }
}

{ }は省略可能
if (true)
  System.out.println(~);
else if (false)
  System.out.println(~);
else
  if (true)
    System.out.println(~);

C#と同じ
C#と同じ

htmlを出力する場合の書き方
<?php if(条件): ?>
 html文
<?php elseif(条件): ?>
 html文
<?php endif; ?>

if num > 0:
   print(f'{num} > 0')
elif num == 0:
   print(f'{num} = 0')
else:
   print(f'{num} < 0')
if ($x > 5) {
 ~
} elseif ($x == 5) {
 ~
} else {
 ~
}
if [ $val = 0 ]
then
 ~
elif [ $val = 1 ]
 ~
else
 ~
fi

Switch(Case)文

string myStr = "aa";

switch(myStr)
{
  case null:
    ~
    break;
  case "aaa":
    ~
    break;
  default:
    ~
    break;
}

int i = 1;

switch (i)
{
 case 1:
  goto caseA; // 独自のCaseへ移動可能
 case 2:
  goto caseB;
 case 3:
  return; // returnでプロシージャの処理を終了可能
 caseA:  // 独自のCaseを作成可能
  goto case 2; // 既存のCaseへ移動可能
 caseB:
  break;
}

Dim myStr As String = "aa"

Select Case myStr
  Case vbNull
    ~
  Case "bb"
    ~
  Case "C", "D&quot, "E&quot
    ~
  Case 1 To 5
    ~
  Case myStr > 5
    ~
  Case Else
    ~
End Select

‘VB.NET限定
Select Case True
  Case myStr = vbNull
    ~
  Case 5 = 5
    ~
  Case Else
    ~
End Select

int num = 2;
switch (num)
{
case 1:
  num++;
  break;
case 2:
  num++;
  break;
default:
  num++;
  break;
}
// num : 3

int num = 2;
switch (num)
{
case 1:
  num++;
case 2:
  num++;
default:
  num++;
}
// num : 4
breakが無いと以降の選択肢全てを通る(defaultも)。

C#と同じ
string myStr = "aa";

switch(myStr)
{
  case null:
    ~
    break;
  case "aaa":
    ~
    break;
  case "bbb":
  case "ccc":
    bbb or ccc の場合
    ~
    break;
  default:
    ~
    break;
}

For文

for (int i = 0; i < 5; i++)
{
  ~
  if (i == null) { break; };
  if (i == null) { continue; };
}
For cnt As Integer = 0 To 10 Step 2
 ~
Next cnt
【ラベル】
z:
for (int i=1; i<=3; i++){
  for (int j=1; j<=3; i++){
    if (j==1){
      continue;
      ※loopを続ける(処理Aを通らない)
    }else if (j==2){
      break;
      ※内側のforを抜ける
    }else if (j==3){
      break z;
      ※外側のforを抜ける
    }
    処理A
  }
}

【複数処理】
for (int i = 0, j = 0; i < 4; i++, j++){
  System.out.println(i+j);
}

【省略】
for (int i = 0; i < 4; ; ){
  System.out.println(i);
  ↑ ではなく↓ に書く
  i++;
}

【{}の省略】
for (int i = 0, j = 0; i < 4; i++, j++)
  System.out.println(i+j);
  // ⇒ 0 2 4 6

↓ と同じ
for (int i = 0, j = 0; i < 4; i++, j++){
  System.out.println(i+j);
}

for (int i = 0, j = 0; i < 4; i++, j++)
  System.out.println(i);
  System.out.println("aaa");

⇒ 0 1 2 3 aaa
⇒ {} を省略したfor文は1行目しかloopしない。
(aaa)の行はloop終了後

↓ と同じ
for (int i = 0, j = 0; i < 4; i++, j++){
  System.out.println(i);
}
System.out.println("aaa");

コンパイルエラー
初期値は異なる型を宣言できない
for (int i = 0, long j = 0; i < 4; i++, j++){
  System.out.println(i+j);
}

コンパイルエラー
loop毎の処理は複数指定できない
for (int i = 0, j = 0; i < 4, j < 4; i++, j++){
  System.out.println(i+j);
}

【Loop毎の処理】
public static void main(String[] args) {
  for (int i = 0, j = 0; i < 3; i++, j++, myFunction1(i), myFunction2(j)){
    System.out.println(i);
  }
}
public static void myFunction1(int i){
  System.out.println("myFunction1:" + i);
}
public static void myFunction2(int j){
  System.out.println("myFunction2:" + j);
}

0
myFunction1:1
myFunction2:1
1
myFunction1:2
myFunction2:2
2
myFunction1:3
myFunction2:3

for ($i=1; $i<=3; $i++)
{
  echo $i;
}

# range(最終値)
for i in range(5):
  print(i)
  → 0 1 2 3 4
 
# range(初期値, 最終値)
for i in range(1, 5):
  print(i)
  → 1 2 3 4
 
# range(初期値, 最終値, 増加値)
for i in range(1, 5, 2):
  print(i)
  → 1 2
 
for ($x=1; $x<10; $x++) {
 ~
}

@x = (1,2,3,4,5);
for my $i (@x) {
  print “$i\n”;
}
⇒1 2 3 4 5

for my $i (0 .. 4) {
  print “$i\n”;
}
⇒0 1 2 3 4

@x = (“a”,”b”,”c”,”d”,”e”);
for my $i (0 .. scalar @x -1) {
  print “@x[$i]\n”;
}
⇒a b c d e

C#と同じ
for i in 1 2 3
do
 ~
done

↓ も可
for i in `seq 1 3`
do
 
done

オプション 機能
無し ディレクトリ内を対象
/d ディレクトリ名を対象
/r ディレクトリ名及びそのサブディレクトリを対象
/l 指定した値を代入

for /l %%i in (1,1,10) do (
for /l(代入オプション) %%変数 in (初期値,増分,終了値) do (
 echo %%i
)

cd ~
for %%i in (*.txt) do (
 echo %%i
)

LOOP内で変数を扱う場合

setlocal ENABLEDELAYEDEXPANSION

初期値、増分、終了値
FOR /L %%i IN (1,1,3) DO (
 SET LOOP+=1
 SETは従来通り
 
 ECHO !LOOP!
 !で囲む
 
 IF !LOOP!=2 (
  CALL test.bat !LOOP!
 )
)

While文

int cnt = 0;
while(cnt < 5){
  ~
  if (cnt == null) { break; };
  if (cnt == null) { continue; };
  cnt += 1;
}

do{
  ~
  if (cnt == null) { break; };
  if (cnt == null) { continue; };
  cnt += 1;
} while(cnt<5);

Dim i As Integer = 0

Do While i < 10
  ~
  Continue Do
  Exit Do
  i += 1
Loop

Do
  ~
  Continue Do
  Exit Do
  i += 1
Loop While i < 10 ‘VB.NET限定
Do Until i > 5
  ~
  Continue Do
  Exit Do
  i += 1
Loop

Do
  ~
  Continue Do
  Exit Do
  i += 1
Loop Until i > 5

後条件
int count = 0;
do {
  System.out.println(count);
  count ++;
} while (count < 5);
// ⇒ 0 1 2 3 4 5

前条件
count = 0;
while (count < 5) {
  System.out.println(count);
  count ++;
}
// ⇒ 0 1 2 3 4

{}省略
count = 0;
do
  count ++;
while (count == 0);
// count : 1

// ↓ と同じ
do {
  count ++;
} while (count == 0);

{}省略時の注意点
int i = 0;
int j = 0;
while (i < 5)
  i ++;
  j ++;

// i : 5
// j : 1
// {}省略時のwhile文は1行しかloopしない。
// ↓ と同じ
while (i < 5) {
  i ++;
}
j ++;

int cnt = 0;
while(cnt < 5){
  ~
  if (cnt == null) { break; };
  if (cnt == null) { continue; };
  cnt += 1;
}
$i = 1;
while (true){
  print $i++;
  if ($i > 100) break;
}

<?php while(条件): ?>
 html文
<?php endwhile; ?>

  i=0
  while i<10:
    i += 1
    print(i)
while ($x > 5) {
 ~
 $x++;
}

until ($x > 5) {
 ~
 $x++;
}

while [条件]
例 while [true]
do
 ~
 インクリメント
 count=$((++count))
 count=$((count++))
done

条件

演算子 機能 意味
-eq = equal
-ne != not equal
-lt < less than
-le <= less than or equal
-gt > greater than
-ge >= greater than or equal

拡張For文(ForEach文)

int[] myInt = {1,2,3,4,5};
foreach (int x in myInt)
{
  ~
  if (x == null) { break; };
  if (x == null) { continue; };
}

//foreach (string x in myInt){ ~ } だとエラー。xとmyIntは同じ型でなくてはならない

Enumerable.Range()を使ったfor文の代わり
foreach (int i in Enumerable.Range(start:0 , count:10) )
{
  Console.WriteLine(i);
  // ⇒ 0 1 2 3 4 5 6 7 8 9
}

Dim myInt() As Integer = {1, 2, 3, 4, 5}
For Each x As Integer In myInt
  ~
  Continue For
  Exit For
Next x

Enumerable.Range()を使ったfor文の代わり
For Each i As Integer In Enumerable.Range(start:=0, count:=10)
  Console.WriteLine(i)
  ’ ⇒ 0 1 2 3 4 5 6 7 8 9
Next i

Dim MyArray(3) As Integer
MyArray(0) = 1
MyArray(1) = 2
MyArray(2) = 3
For Each x In MyArray
  MsgBox(x)
  Exit For
Next x

int[] myInt = new int[]{1,2,3,4,5};

for (int elm : myInt) {
  System.out.println(elm);
}

int[][] myArray = new int[][]{{1},{2,2},{3,3,3}};

for (int[] outer : myArray){
  for (int inner : outer){
    System.out.println(inner);
  }
}

var 連想配列変数 = {キー:値 , キー:値 , キー:値 , ・・・}
for (var key in 連想配列変数) {
  alert(key);
  alert(連想配列変数[key]);
}

var array = { "name" : "安部礼司", "age":39 , "nickname":"平均", "bloodtype":"A" };
for (var key in array) {
  if ( key == "bloodtype" ) break;
  alert("キー:" + key + " 値:" + arytest[key]);
}

$('.class').each(function(){
 loopを1周回す=continue
 return true;

 loopを抜ける=break
 return false;
});

foreach ($array as $elm) {
  // 配列・オブジェクトの要素を取得
  $e = $elm;
  
  // Loopを抜ける。
  break;
}

foreach ($array as $key => $elm) {
  // 配列・オブジェクトのキーを取得
  $k = $key;
  $e = $elm;
}

ary = ['php', 'python', 'Java']

for item in ary:
  print(item)
  → php python Java

# 連想配列初期化
dic = {
  'php':'CakePHP3',
  'python':'Django',
  'Java':'Spring-boot',
}

for key in dic.keys():
  print(key)
  → php python Java

for value in dic.values():
  print(value)
  → CakePHP3 Djanog Spring-boot

for key, value in dic.items():
  print(key, value)
  → php CakePHP3 python Djanog Java Spring-boot

@x = (1,2,3,4,5);
foreach my $i (@x) {
  print "$i\n";
}
⇒1 2 3 4 5