| 情報学部 | 菅沼ホーム | 目次 | 索引 |
| 記号 | 意味 | 例 | 例の意味 |
|---|---|---|---|
| > | より大きい | a > b | 式 a の値が式 b の値より大きいとき真 |
| < | より小さい | a < b | 式 a の値が式 b の値より小さいとき真 |
| >= | 以上 | a >= b | 式 a の値が式 b の値以上のとき真 |
| <= | 以下 | a <= b | 式 a の値が式 b の値以下のとき真 |
| 記号 | 意味 | 例 | 例の意味 |
|---|---|---|---|
| ==(VB:=) | 等しい | a == b(VB:a = b) | 式 a の値と式 b の値が等しいとき真 |
| !=(VB:<>) | 等しくない | a != b(VB:a <> b) | 式 a の値と式 b の値が等しくないとき真 |
| 記号 | 意味 | 例 | 例の意味 | 言語による違い |
|---|---|---|---|---|
| || | 論理和 | x || y | 式 x が真か,または,式 y が真のとき真 | C++,PHP,Ruby では or でも可.Python,VB では or 表現だけ |
| && | 論理積 | x && y | 式 x が真で,かつ,式 y が真のとき真 | C++,PHP,Ruby では and でも可.Python,VB では and 表現だけ |
| ! | 否定 | ! x | 式 x が偽のとき真 | C++,Ruby では not でも可.Python,VB では not 表現だけ |
1 < x < 5
1 < 0 < 5
intval(1 < 0) < 5
(1 < 0) && (0 < 5) // Python,VB の場合は,(1 < 0) and (0 < 5)
01 Dim x() As Integer = {1, 2, 3}
02 Dim min As Integer = -1
03 For i1 As Integer = 0 To 2
04 if min < 0 or x(i1) < x(min)
05 ' if min >= 0 and x(i1) < x(min)
06 min = i1
07 End If
08 Next
(5 > 0) == true (5 > 0) === true (5 > 0) == "1" (5 > 0) === "1"
// $x = 5; の場合 $x == 5 $x === 5 $x == 5.0 $x === 5.0
String === "abc" "abc" === String (1..10) === 3 (1..10) === 0
a = 10 b = 10 print(a == b, a is b) x = complex(2.5, 3.14) y = complex(2.5, 3.14) print(x == y, x is y)
if (論理式1) { 文1(複数の文も可) } else if (論理式2) { 文2(複数の文も可) } ・ ・ else { 文n(複数の文も可) }
if (論理式1) {
・・・・・・
if (論理式2) {
・・・・・・
}
else {
・・・・・・
}
・・・・・・
}
else {
・・・・・・
}
01 if (a == b) {
02 max = y;
03 min = z;
04 if (min < 0.0) {
05 min = 0.0;
06 a = b;
07 }
08 }
09 else {
10 max = s;
11 min = g;
12 }
13 x = 9;
14 y = 10;
if 論理式1 then 文1(複数の文も可) elsif 論理式2 then 文2(複数の文も可) ・ ・ else 文n(複数の文も可) end
if 論理式1 then ・・・・・・ if 論理式2 then ・・・・・・ else ・・・・・・ end ・・・・・・ else ・・・・・・ end
01 if a == b then 02 max = y; 03 min = z; 04 if min < 0.0 then 05 min = 0.0; 06 a = b; 07 end 08 else 09 max = s; 10 min = g; 11 end 12 x = 9; 13 y = 10;
if 論理式1 : 文1(複数の文も可) elif 論理式2 : 文2(複数の文も可) ・ ・ else : 文n(複数の文も可)
if 論理式1 : ・・・・・・ if 論理式2 : ・・・・・・ else : ・・・・・・ ・・・・・・ else : ・・・・・・
01 if a == b : 02 max = y 03 min = z 04 if min < 0.0 : 05 min = 0.0 06 a = b 07 else : 08 max = s 09 min = g 10 x = 9 11 y = 10
If 論理式1 Then 文1(複数の文も可) ElseIf 論理式2 文2(複数の文も可) ・ ・ Else 文n(複数の文も可) End If
If 論理式1 Then ・・・・・・ If 論理式2 Then ・・・・・・ Else ・・・・・・ End If ・・・・・・ Else ・・・・・・ End If
01 If a == b Then 02 max = y 03 min = z 04 If min < 0.0 Then 05 min = 0.0 06 a = b 07 End If 08 Else 09 max = s 10 min = g 11 End If 12 x = 9 13 y = 10
switch (式) { case 定数式1 : 文1 case 定数式2 : 文2 ・・・・・ default : 文n }
01 int x;
02 printf("整数データを入力して下さい ");
03 scanf("%d", &x);
04 switch (x) {
05 case -1 :
06 printf("-1 です\n");
07 case 0 :
08 printf("0 です\n");
09 break;
10 case 1 :
11 printf("1 です\n");
12 break;
13 default :
14 printf("-1,0,1 以外です\n");
15 }
0 です
-1 です 0 です
case 式 when 式11, 式12, ・・・ , *式1m then 文1 when 式21, 式22, ・・・ , *式2n then 文2 ・・・ else 文n end
Select Case 変数 Case 条件1 文1 Case 条件2 文2 ・・・ Case Else 文n End Select
01 /******************************/
02 /* 分岐を使用したプログラム例 */
03 /* coded by Y.Suganuma */
04 /******************************/
05 #include <stdio.h>
06
07 int main()
08 {
09 // データの入力
10 int x;
11 printf("整数値を一つ入力してください ");
12 scanf("%d", &x);
13 // if,else を使用した場合
14 if (x >= -3 && x < 0)
15 printf("負です\n");
16 else {
17 if (x == 0)
18 printf("0 です\n");
19 else {
20 if (x > 0 && x <= 3)
21 printf("正です\n");
22 else
23 printf("範囲外です\n");
24 }
25 }
26 // if,else if,else を使用した場合
27 if (x >= -3 && x < 0)
28 printf("負です\n");
29 else if (x == 0)
30 printf("0 です\n");
31 else if (x > 0 && x <= 3)
32 printf("正です\n");
33 else
34 printf("範囲外です\n");
35 // switch を使用した場合
36 switch (x) {
37 case -3 :
38 case -2 :
39 case -1 :
40 printf("負です\n");
41 break;
42 case 0 :
43 printf("0 です\n");
44 break;
45 case 1 :
46 case 2 :
47 case 3 :
48 printf("正です\n");
49 break;
50 default :
51 printf("範囲外です\n");
52 }
53
54 return 0;
55 }
01 /******************************/
02 /* 分岐を使用したプログラム例 */
03 /* coded by Y.Suganuma */
04 /******************************/
05 import java.io.*;
06
07 public class Test {
08 public static void main(String args[]) throws IOException
09 {
10 // データの入力
11 System.out.print("整数値を一つ入力してください ");
12 BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
13 int x = Integer.parseInt(inp.readLine());
14 // if,else を使用した場合
15 if (x >= -3 && x < 0)
16 System.out.println("負です");
17 else {
18 if (x == 0)
19 System.out.println("0 です");
20 else {
21 if (x > 0 && x <= 3)
22 System.out.println("正です");
23 else
24 System.out.println("範囲外です");
25 }
26 }
27 // if,else if,else を使用した場合
28 if (x >= -3 && x < 0)
29 System.out.println("負です");
30 else if (x == 0)
31 System.out.println("0 です");
32 else if (x > 0 && x <= 3)
33 System.out.println("正です");
34 else
35 System.out.println("範囲外です");
36 // switch を使用した場合
37 switch (x) {
38 case -3 :
39 case -2 :
40 case -1 :
41 System.out.println("負です");
42 break;
43 case 0 :
44 System.out.println("0 です");
45 break;
46 case 1 :
47 case 2 :
48 case 3 :
49 System.out.println("正です");
50 break;
51 default :
52 System.out.println("範囲外です");
53 }
54 }
55 }
01 <!DOCTYPE HTML>
02 <HTML>
03 <HEAD>
04 <TITLE>分岐</TITLE>
05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
06 <SCRIPT TYPE="text/javascript">
07 /******************************/
08 /* 分岐を使用したプログラム例 */
09 /* coded by Y.Suganuma */
10 /******************************/
11 function run() {
12 // データの取得
13 let x = parseInt(document.getElementById("x").value);
14 // if,else を使用した場合
15 let str = "";
16 if (x >= -3 && x < 0)
17 str += "負です\n";
18 else {
19 if (x == 0)
20 str += "0 です\n";
21 else {
22 if (x > 0 && x <= 3)
23 str += "正です\n";
24 else
25 str += "範囲外です\n";
26 }
27 }
28 // if,else if,else を使用した場合
29 if (x >= -3 && x < 0)
30 str += "負です\n";
31 else if (x == 0)
32 str += "0 です\n";
33 else if (x > 0 && x <= 3)
34 str += "正です\n";
35 else
36 str += "範囲外です\n";
37 // switch を使用した場合
38 switch (x) {
39 case -3 :
40 case -2 :
41 case -1 :
42 str += "負です\n";
43 break;
44 case 0 :
45 str += "0 です\n";
46 break;
47 case 1 :
48 case 2 :
49 case 3 :
50 str += "正です\n";
51 break;
52 default :
53 str += "範囲外です\n";
54 }
55 // 結果の設定
56 document.getElementById("tx").value = str;
57 }
58 </SCRIPT>
59 </HEAD>
60 <BODY STYLE="font-size:130%">
61 <P STYLE="text-align:center">
62 整数データ : <INPUT TYPE="text" ID="x" SIZE="5" STYLE="font-size:120%">
63 <INPUT TYPE="button" VALUE="OK" onClick="run()" STYLE="font-size:90%"><BR><BR>
64 <TEXTAREA TYPE="text" ID="tx" COLS="15" ROWS="3" STYLE="font-size: 120%"></TEXTAREA>
65 </P>
66 </BODY>
67 </HTML>
01 <?php
02 /******************************/
03 /* 分岐を使用したプログラム例 */
04 /* coded by Y.Suganuma */
05 /******************************/
06 // データの入力
07 print "整数値を一つ入力してください ";
08 $x = intval(fgets(STDIN));
09 // if,else を使用した場合
10 if ($x >= -3 && $x < 0)
11 print "負です\n";
12 else {
13 if ($x == 0)
14 print "0 です\n";
15 else {
16 if ($x > 0 && $x <= 3)
17 print "正です\n";
18 else
19 print "範囲外です\n";
20 }
21 }
22 // if,else if,else を使用した場合
23 if ($x >= -3 && $x < 0)
24 print "負です\n";
25 else if ($x == 0)
26 print "0 です\n";
27 else if ($x > 0 && $x <= 3)
28 print "正です\n";
29 else
30 print "範囲外です\n";
31 // switch を使用した場合
32 switch ($x) {
33 case -3 :
34 case -2 :
35 case -1 :
36 print "負です\n";
37 break;
38 case 0 :
39 print "0 です\n";
40 break;
41 case 1 :
42 case 2 :
43 case 3 :
44 print "正です\n";
45 break;
46 default :
47 print "範囲外です\n";
48 }
49 ?>
01 ############################## 02 # 分岐を使用したプログラム例 # 03 # coded by Y.Suganuma # 04 ############################## 05 print "整数値を一つ入力してください "; 06 x = Integer(gets()); 07 # if,else を使用した場合 08 if (x >= -3 && x < 0) 09 print "負です\n"; 10 else 11 if (x == 0) 12 print "0 です\n"; 13 else 14 if (x > 0 && x <= 3) 15 print "正です\n"; 16 else 17 print "範囲外です\n"; 18 end 19 end 20 end 21 # if,elsif,else を使用した場合 22 if (x >= -3 && x < 0) 23 print "負です\n"; 24 elsif (x == 0) 25 print "0 です\n"; 26 elsif (x > 0 && x <= 3) 27 print "正です\n"; 28 else 29 print "範囲外です\n"; 30 end 31 # case を使用した場合 32 a = (1 .. 3); 33 b = [-2, -1]; 34 case x 35 when -3, *b # when -3, -2, -1 と同じ 36 print "負です\n"; 37 when 0 38 print "0 です\n"; 39 when *a # when 1, 2, 3 と同じ 40 print "正です\n"; 41 else 42 print "範囲外です\n"; 43 end
b = [-3, -2, -1];
when *b
01 # -*- coding: UTF-8 -*-
02 ############################
03 # 2つのデータの乗算と除算 #
04 # coded by Y.Suganuma #
05 ############################
06 # データの入力
07 x = int(input("整数値を一つ入力してください "))
08 # if,else を使用した場合
09 if -3 <= x < 0 : # if x >= -3 and x < 0 : でも可
10 print("負です")
11 else :
12 if x == 0 :
13 print("0 です")
14 else :
15 if 0 < x <= 3 :
16 print("正です")
17 else :
18 print("範囲外です")
19 # if,elif,else を使用した場合
20 if -3 <= x < 0 :
21 print("負です")
22 elif x == 0 :
23 print("0 です")
24 elif 0 < x <= 3 :
25 print("正です")
26 else :
27 print("範囲外です")
-3 <= x < 0
01 /******************************/
02 /* 分岐を使用したプログラム例 */
03 /* coded by Y.Suganuma */
04 /******************************/
05 using System;
06
07 class Program
08 {
09 static void Main()
10 {
11 Console.Write("整数値を一つ入力してください ");
12 int x = int.Parse(Console.ReadLine());
13 // if,else を使用した場合
14 if (x >= -3 && x < 0)
15 Console.WriteLine("負です");
16 else {
17 if (x == 0)
18 Console.WriteLine("0 です");
19 else {
20 if (x > 0 && x <= 3)
21 Console.WriteLine("正です");
22 else
23 Console.WriteLine("範囲外です");
24 }
25 }
26 // if,else if,else を使用した場合
27 if (x >= -3 && x < 0)
28 Console.WriteLine("負です");
29 else if (x == 0)
30 Console.WriteLine("0 です");
31 else if (x > 0 && x <= 3)
32 Console.WriteLine("正です");
33 else
34 Console.WriteLine("範囲外です");
35 // switch を使用した場合
36 switch (x) {
37 case -3 :
38 case -2 :
39 case -1 :
40 Console.WriteLine("負です");
41 break;
42 case 0 :
43 Console.WriteLine("0 です");
44 break;
45 case 1 :
46 case 2 :
47 case 3 :
48 Console.WriteLine("正です");
49 break;
50 default :
51 Console.WriteLine("範囲外です");
52 break; // C# では,必ず必要
53 }
54 }
55 }
01 ''''''''''''''''''''''''''''''
02 ' 分岐を使用したプログラム例 '
03 ' coded by Y.Suganuma '
04 ''''''''''''''''''''''''''''''
05 Module Test
06 Sub Main()
07 Dim x As Integer
08 Console.Write("整数値を一つ入力してください ")
09 x = Integer.Parse(Console.ReadLine())
10 ' If,Else を使用した場合
11 If x >= -3 And x < 0 Then
12 Console.WriteLine("負です")
13 Else
14 If x = 0 Then
15 Console.WriteLine("0 です")
16 Else
17 If x > 0 And x <= 3 Then
18 Console.WriteLine("正です")
19 Else
20 Console.WriteLine("範囲外です")
21 End If
22 End If
23 End If
24 ' If,ElseIf,Else を使用した場合
25 if x >= -3 And x < 0 Then
26 Console.WriteLine("負です")
27 ElseIf x = 0
28 Console.WriteLine("0 です")
29 ElseIf x > 0 And x <= 3
30 Console.WriteLine("正です")
31 Else
32 Console.WriteLine("範囲外です")
33 End If
34 ' Select Case を使用した場合
35 Select Case x
36 Case -3, -2, -1 ' Is >= -3 And Is <= -1 は不可
37 Console.WriteLine("負です")
38 Case Is = 0 ' 0
39 Console.WriteLine("0 です")
40 Case 1 To 3 ' Is = 1, Is = 2, Is = 3
41 Console.WriteLine("正です")
42 Case Else
43 Console.WriteLine("範囲外です")
44 End Select
45 End Sub
46 End Module
1 番目のデータを入力 入力したデータを sum に加える 2 番目のデータを入力 入力したデータを sum に加える ・・・・・
for (式1; 式2; 式3) {
文(複数の文も可)
}
// while 文
while (論理式) {
文(複数の文も可)
} // do while 文 do { 文(複数の文も可) } while (論理式) ;
式1;
while (式2) {
文(複数の文も可)
式3;
}
for (式1; 式2; 式3) {
・・・・・
for (式4; 式5; 式6) {
・・・・・
}
・・・・・
}
for (i1 = 0; i1 < 10; i1 = i1+1) {
a = b + c;
for (i2 = 0; i2 < 5; i2 = i2+1) {
bcd = a / y;
aa = b;
・・・・・・・・
}
sum = c + d;
}
for 変数 in 式 do 文(複数の文も可) end
while (論理式) do 文(複数の文も可) end
for 変数1 in 式1 ・・・・・ for 変数2 in 式2 ・・・・・ end ・・・・・ end
for i1 in (0..10) a = b + c for i2 in [0, 1, 2, 3, 4] bcd = a / y aa = b ・・・・・・・・ end sum = c + d end
for 変数 in イテラブルオブジェクト : 文1(複数の文も可) else : 文2(複数の文も可)
while 論理式 : 文1(複数の文も可) else : 文2(複数の文も可)
for 変数1 in イテラブルオブジェクト1 : ・・・・・ for 変数2 in イテラブルオブジェクト2 : ・・・・・ ・・・・・
For 変数 As Integer = 初期値 To 終了値 Step 刻み幅 文(複数の文も可) Next
For Each 変数1 As Integer in 変数2 文(複数の文も可) Next
' Do While Loop 文 Do While 論理式 文(複数の文も可) Loop
' Do Loop While 文 Do 文(複数の文も可) Loop While 論理式
変数の初期設定 Do While 変数 <= 終了値 文(複数の文も可) 変数 += 刻み幅 Loop
For 変数1 As Integer = 初期値1 To 終了値1 Step 刻み幅1 ・・・・・ For 変数2 As Integer = 初期値2 To 終了値2 Step 刻み幅2 ・・・・・ Next ・・・・・ Next
For i1 As Integer = 1 To 10 Step 1 a = b + c For i2 As Integer = 1 To 5 Step 1 bcd = a / y aa = b ・・・・・・・・ Next sum = c + d Next
' Do Until Loop 文 Do Until 論理式 文(複数の文も可) Loop
' Do Loop Until 文 Do 文(複数の文も可) Loop Until 論理式
01 /****************************/
02 /* 繰り返しのプログラム例 */
03 /* coded by Y.Suganuma */
04 /****************************/
05 #include <stdio.h>
06 #include <vector>
07 #include <algorithm>
08
09 using namespace std;
10
11 int main()
12 {
13 // 1 から 5 までの和( for )
14 int sum1 = 0;
15 for (int i1 = 1; i1 <= 5; i1++)
16 sum1 += i1;
17 printf("for による和 %d\n", sum1);
18 // 1 から 5 までの和( while )
19 int sum2 = 0;
20 int i1 = 1;
21 while (i1 <= 5) {
22 sum2 += i1;
23 i1++;
24 }
25 printf("while による和 %d\n", sum2);
26 // 1 から 5 までの和(範囲 for 文 )
27 int sum4 = 0;
28 int a[] {1, 2, 3, 4, 5};
29 for (auto x : a)
30 sum4 += x;
31 printf("範囲 for 文による和(配列) %d\n", sum4);
32 int sum5 = 0;
33 vector<int> v {1, 2, 3, 4, 5};
34 for (auto x : v)
35 sum5 += x;
36 printf("範囲 for 文による和(vector) %d\n", sum5);
37 // 1 から 5 までの和( for_each )
38 sum4 = 0;
39 for_each(a, a+5, [&sum4](int x){ return sum4 += x; });
40 printf("for_each による和(配列) %d\n", sum4);
41 sum5 = 0;
42 for_each(v.begin(), v.end(), [&sum5](int x){ return sum5 += x; });
43 printf("for_each による和(vector) %d\n", sum5);
44 // 最大値の計算1( while )
45 int x;
46 printf("整数データ?( 0 によって終了) ");
47 scanf("%d", &x);
48 int max1 = x;
49 while (x != 0) {
50 if (x > max1)
51 max1 = x;
52 printf("整数データ?( 0 によって終了) ");
53 scanf("%d", &x);
54 }
55 printf(" 最大値1(while) %d\n", max1);
56 // 最大値の計算2( while )
57 int max2 = 0, y = 1;
58 while (y != 0) {
59 printf("整数データ?( 0 によって終了) ");
60 scanf("%d", &y);
61 if (y == 0)
62 break;
63 else {
64 if (max2 == 0 || y > max2)
65 max2 = y;
66 }
67 }
68 printf(" 最大値2(while) %d\n", max2);
69 // 最大値の計算( for による多重ループ )
70 int n, max3;
71 printf("クラスの数? ");
72 scanf("%d", &n);
73 for (int i1 = 0; i1 < n; i1++) {
74 int m, sum3 = 0;
75 printf(" クラス %d の人数? ", i1+1);
76 scanf("%d", &m);
77 for (int i2 = 0; i2 < m; i2++) {
78 int z;
79 printf(" %d 番目の人の点数? ", i2+1);
80 scanf("%d", &z);
81 sum3 += z;
82 }
83 sum3 /= m;
84 if (i1 == 0 || sum3 > max3)
85 max3 = sum3;
86 }
87 printf(" 最大値3(for による多重ループ) %d\n", max3);
88
89 return 0;
90 }
if (y != 0) {
if (max2 == 0 || y > max2)
max2 = y;
}
if (sum3 > max3)
01 /****************************/
02 /* 繰り返しのプログラム例 */
03 /* coded by Y.Suganuma */
04 /****************************/
05 import java.io.*;
06
07 public class Test {
08 public static void main(String args[]) throws IOException
09 {
10 // 1 から 5 までの和( for )
11 int sum1 = 0;
12 for (int i1 = 1; i1 <= 5; i1++)
13 sum1 += i1;
14 System.out.printf("for による和 %d\n", sum1);
15 // 1 から 5 までの和( while )
16 int sum2 = 0;
17 int i1 = 1;
18 while (i1 <= 5) {
19 sum2 += i1;
20 i1++;
21 }
22 System.out.printf("while による和 %d\n", sum2);
23 // 最大値の計算1( while )
24 BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
25 System.out.printf("整数データ?( 0 によって終了) ");
26 int x = Integer.parseInt(inp.readLine());
27 int max1 = x;
28 while (x != 0) {
29 if (x > max1)
30 max1 = x;
31 System.out.printf("整数データ?( 0 によって終了) ");
32 x = Integer.parseInt(inp.readLine());
33 }
34 System.out.printf(" 最大値1(while) %d\n", max1);
35 // 最大値の計算2( while )
36 int max2 = 0, y = 1;
37 while (y != 0) {
38 System.out.printf("整数データ?( 0 によって終了) ");
39 y = Integer.parseInt(inp.readLine());
40 if (y == 0)
41 break;
42 else {
43 if (max2 == 0 || y > max2)
44 max2 = y;
45 }
46 }
47 System.out.printf(" 最大値2(while) %d\n", max2);
48 // 最大値の計算( for による多重ループ )
49 System.out.printf("クラスの数? ");
50 int n = Integer.parseInt(inp.readLine());
51 int max3 = 0; // 初期設定が必要
52 for (i1 = 0; i1 < n; i1++) { // i1 を定義できない
53 System.out.printf(" クラス %d の人数? ", i1+1);
54 int m = Integer.parseInt(inp.readLine());
55 int sum3 = 0;
56 for (int i2 = 0; i2 < m; i2++) {
57 System.out.printf(" %d 番目の人の点数? ", i2+1);
58 int z = Integer.parseInt(inp.readLine());
59 sum3 += z;
60 }
61 sum3 /= m;
62 if (i1 == 0 || sum3 > max3)
63 max3 = sum3;
64 }
65 System.out.printf(" 最大値3(for による多重ループ) %d\n", max3);
66 }
67 }
if (y != 0) {
if (max2 == 0 || y > max2)
max2 = y;
}
if (sum3 > max3)
-1 3 -5 0 // 最大値1(while) -3 -2 -4 -5 0 // 最大値2(while) 2 // クラスの数 3 // クラス1の人数 70 80 50 // クラス1の成績 2 // クラス2の人数 100 90 // クラス2の成績
for による和 15 while による和 15 最大値1(while) 3 最大値2(while) -2 最大値3(for による多重ループ) 95
01 <!DOCTYPE HTML>
02 <HTML>
03 <HEAD>
04 <TITLE>繰り返し</TITLE>
05 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
06 <SCRIPT TYPE="text/javascript">
07 /****************************/
08 /* 繰り返しのプログラム例 */
09 /* coded by Y.Suganuma */
10 /****************************/
11 function run() {
12 // データの取得
13 let str = document.getElementById("tx").value;
14 let data = str.split("\n");
15 // 1 から 5 までの和( for )
16 let sum1 = 0;
17 for (let i1 = 1; i1 <= 5; i1++)
18 sum1 += i1;
19 str += ("\nfor による和 " + sum1 + "\n");
20 // 1 から 5 までの和( while )
21 let sum2 = 0;
22 let i1 = 1;
23 while (i1 <= 5) {
24 sum2 += i1;
25 i1++;
26 }
27 str += ("while による和 " + sum2 + "\n");
28 // 最大値の計算1( while )
29 i1 = 0;
30 let x_st = data[0].split(" "); // 1 行目のデータ
31 let x = parseInt(x_st[i1]);
32 let max1 = x;
33 while (x != 0) {
34 if (x > max1)
35 max1 = x;
36 i1++;
37 x = parseInt(x_st[i1]);
38 }
39 str += ("最大値1(while) " + max1 + "\n");
40 // 最大値の計算2( while )
41 i1 = 0;
42 let y_st = data[1].split(" "); // 2 行目のデータ
43 let max2 = 0, y = 1;
44 while (y != 0) {
45 let y = parseInt(y_st[i1]);
46 if (y == 0)
47 break;
48 else {
49 if (max2 == 0 || y > max2)
50 max2 = y;
51 i1++;
52 }
53 }
54 str += ("最大値2(while) " + max2 + "\n");
55 // 最大値の計算( for による多重ループ )
56 let st = data[2].split(" "); // 3 行目のデータ
57 let n = parseInt(st[0]);
58 let max3;
59 for (let i1 = 0; i1 < n; i1++) {
60 st = data[3+2*i1].split(" "); // 4,6 行目のデータ
61 let m = parseInt(st[0]);
62 let sum3 = 0;
63 st = data[3+2*i1+1].split(" "); // 5,7 行目のデータ
64 for (let i2 = 0; i2 < m; i2++) {
65 let z = parseInt(st[i2]);
66 sum3 += z;
67 }
68 sum3 /= m;
69 if (i1 == 0 || sum3 > max3)
70 max3 = sum3;
71 }
72 str += "最大値3(for による多重ループ) " + max3 + "\n";
73 // 結果の設定
74 document.getElementById("tx").value = str;
75 }
76 </SCRIPT>
77 </HEAD>
78 <BODY STYLE="font-size:130%">
79 <P STYLE="text-align:center">
80 <INPUT TYPE="button" VALUE="OK" onClick="run()" STYLE="font-size:90%"><BR><BR>
81 <TEXTAREA TYPE="text" ID="tx" COLS="50" ROWS="10" STYLE="font-size: 100%">
82 -1 3 -5 0 // 最大値1(while)
83 -3 -2 -4 -5 0 // 最大値2(while)
84 2 // クラスの数
85 3 // クラス1の人数
86 70 80 50 // クラス1の成績
87 2 // クラス2の人数
88 100 90 // クラス2の成績
89 </TEXTAREA>
90 </P>
91 </BODY>
92 </HTML>
if (y != 0) {
if (max2 == 0 || y > max2)
max2 = y;
i1++;
}
if (sum3 > max3)
01 <?php
02 /****************************/
03 /* 繰り返しのプログラム例 */
04 /* coded by Y.Suganuma */
05 /****************************/
06 // 1 から 5 までの和( for )
07 $sum1 = 0;
08 for ($i1 = 1; $i1 <= 5; $i1++)
09 $sum1 += $i1;
10 printf("for による和 %d\n", $sum1);
11 // 1 から 5 までの和( while )
12 $sum21 = 0;
13 $i1 = 1;
14 while ($i1 <= 5) {
15 $sum21 += $i1;
16 $i1++;
17 }
18 printf("while による和 %d\n", $sum21);
19 // 1 から 5 までの和( foreach )
20 $a = array(1, 2, 3, 4, 5);
21 $sum22 = 0;
22 foreach ($a as $w)
23 $sum22 += $w;
24 printf("foreach による和 %d\n", $sum22);
25 // 最大値の計算1( while )
26 printf("整数データ?( 0 によって終了) ");
27 $x = intval(trim(fgets(STDIN)));
28 $max1 = $x;
29 while ($x != 0) {
30 if ($x > $max1)
31 $max1 = $x;
32 printf("整数データ?( 0 によって終了) ");
33 $x = intval(trim(fgets(STDIN)));
34 }
35 printf(" 最大値1(while) %d\n", $max1);
36 // 最大値の計算2( while )
37 $max2 = 0;
38 $y = 1;
39 while ($y != 0) {
40 printf("整数データ?( 0 によって終了) ");
41 $y = intval(trim(fgets(STDIN)));
42 if ($y == 0)
43 break;
44 else {
45 if ($max2 == 0 || $y > $max2)
46 $max2 = $y;
47 }
48 }
49 printf(" 最大値2(while) %d\n", $max2);
50 // 最大値の計算( for による多重ループ )
51 printf("クラスの数? ");
52 $n = intval(trim(fgets(STDIN)));
53 for ($i1 = 0; $i1 < $n; $i1++) {
54 printf(" クラス %d の人数? ", $i1+1);
55 $m = intval(trim(fgets(STDIN)));
56 $sum3 = 0;
57 for ($i2 = 0; $i2 < $m; $i2++) {
58 printf(" %d 番目の人の点数? ", $i2+1);
59 $z = intval(trim(fgets(STDIN)));
60 $sum3 += $z;
61 }
62 $sum3 /= $m;
63 if ($i1 == 0 || $sum3 > $max3)
64 $max3 = $sum3;
65 }
66 printf(" 最大値3(for による多重ループ) %d\n", $max3);
67 ?>
if ($y != 0) {
if ($max2 == 0 || $y > $max2)
$max2 = $y;
}
if ($sum3 > $max3)
01 ############################
02 # 繰り返しのプログラム例 #
03 # coded by Y.Suganuma #
04 ############################
05 # 1 から 5 までの和( for )
06 sum1 = 0;
07 for i1 in (1..5) do # for i1 in [1, 2, 3, 4, 5] do でも可
08 sum1 += i1;
09 end
10 printf("for による和 %d\n", sum1);
11 # 1 から 5 までの和( while )
12 sum2 = 0;
13 i1 = 1;
14 while (i1 <= 5) do
15 sum2 += i1;
16 i1 += 1;
17 end
18 printf("while による和 %d\n", sum2);
19 # 最大値の計算1( while )
20 printf("整数データ?( 0 によって終了) ");
21 x = Integer(gets());
22 max1 = x;
23 while (x != 0) do
24 if (x > max1)
25 max1 = x;
26 end
27 printf("整数データ?( 0 によって終了) ");
28 x = Integer(gets());
29 end
30 printf(" 最大値1(while) %d\n", max1);
31 # 最大値の計算2( while )
32 max2 = 0;
33 y = 1;
34 while (y != 0) do
35 printf("整数データ?( 0 によって終了) ");
36 y = Integer(gets());
37 if (y == 0)
38 break;
39 else
40 if (max2 == 0 || y > max2)
41 max2 = y;
42 end
43 end
44 end
45 printf(" 最大値2(while) %d\n", max2);
46 # 最大値の計算( for による多重ループ )
47 printf("クラスの数? ");
48 n = Integer(gets());
49 max3 = 0; # ここで定義しておく必要あり
50 for i1 in (1..n)
51 printf(" クラス %d の人数? ", i1);
52 m = Integer(gets());
53 sum3 = 0;
54 for i2 in (1..m)
55 printf(" %d 番目の人の点数? ", i2);
56 z = Integer(gets());
57 sum3 += z;
58 end
59 sum3 /= m;
60 if (i1 == 1 || sum3 > max3)
61 max3 = sum3;
62 end
63 end
64 printf(" 最大値3(for による多重ループ) %d\n", max3);
if (y != 0) if (max2 == 0 || y > max2) max2 = y; end end
if (sum3 > max3)
01 # -*- coding: UTF-8 -*-
02 ############################
03 # 繰り返しのプログラム例 #
04 # coded by Y.Suganuma #
05 ############################
06 # 1 から 5 までの和( for )
07 sum1 = 0
08 for i1 in range(1, 6) :
09 sum1 += i1
10 print("for による和", sum1)
11 # 1 から 5 までの和( while )
12 sum2 = 0
13 i1 = 1
14 while (i1 <= 5) :
15 sum2 += i1
16 i1 += 1
17 print("while による和", sum2)
18 # 最大値の計算1( while )
19 x = int(input("整数データ?( 0 によって終了) "))
20 max1 = x
21 while (x != 0) :
22 if (x > max1) :
23 max1 = x
24 x = int(input("整数データ?( 0 によって終了) "))
25 print(" 最大値1(while)", max1)
26 # 最大値の計算2( while )
27 max2 = 0
28 y = 1
29 while (y != 0) :
30 y = int(input("整数データ?( 0 によって終了) "))
31 if (y == 0) :
32 break
33 else :
34 if (max2 == 0 or y > max2) :
35 max2 = y
36 print(" 最大値2(while)", max2)
37 # 最大値の計算( for による多重ループ )
38 n = int(input("クラスの数? "))
39 for i1 in range(1, n+1) :
40 print(" クラス", i1, sep=' ', end='')
41 m = int(input(" の人数? "))
42 sum3 = 0
43 for i2 in range(1, m+1) :
44 print(" ", i2, sep=' ', end='')
45 z = int(input(" 番目の人の点数? "))
46 sum3 += z
47 sum3 /= m
48 if (i1 == 1 or sum3 > max3) :
49 max3 = sum3
50 print(" 最大値3(for による多重ループ)", max3)
if (y != 0) : if (max2 == 0 or y > max2) : max2 = y
if (sum3 > max3) :
01 /******************************/
02 /* 分岐を使用したプログラム例 */
03 /* coded by Y.Suganuma */
04 /******************************/
05 using System;
06
07 class Program
08 {
09 static void Main()
10 {
11 int i1;
12 // 1 から 5 までの和( for )
13 int sum1 = 0;
14 for (i1 = 1; i1 <= 5; i1++)
15 sum1 += i1;
16 Console.WriteLine("for による和 " + sum1);
17 // 1 から 5 までの和( while )
18 int sum2 = 0;
19 i1 = 1;
20 while (i1 <= 5) {
21 sum2 += i1;
22 i1++;
23 }
24 Console.WriteLine("while による和 " + sum2);
25 // 1 から 5 までの和( foreach )
26 int sum4 = 0;
27 int[] xx = {1, 2, 3, 4, 5};
28 foreach (int xi in xx)
29 sum4 += xi;
30 Console.WriteLine("foreach による和 " + sum4);
31 // 最大値の計算1( while )
32 Console.Write("整数データ(最大値1)?( 0 によって終了) ");
33 int x = int.Parse(Console.ReadLine());
34 int max1 = x;
35 while (x != 0) {
36 if (x > max1)
37 max1 = x;
38 Console.Write("整数データ(最大値1)?( 0 によって終了) ");
39 x = int.Parse(Console.ReadLine());
40 }
41 Console.WriteLine(" 最大値1(while) " + max1);
42 // 最大値の計算2( while )
43 int max2 = 0, y = 1;
44 while (y != 0) {
45 Console.Write("整数データ(最大値2)?( 0 によって終了) ");
46 y = int.Parse(Console.ReadLine());
47 if (y == 0)
48 break;
49 else {
50 if (max2 == 0 || y > max2)
51 max2 = y;
52 }
53 }
54 Console.WriteLine(" 最大値2(while) " + max2);
55 // 最大値の計算( for による多重ループ )
56 Console.Write("クラスの数? ");
57 int n = int.Parse(Console.ReadLine());
58 int max3 = 0; // 初期設定が必要
59 for (i1 = 0; i1 < n; i1++) { // i1 を定義できない
60 Console.Write(" クラス %d の人数? ", i1+1);
61 int m = int.Parse(Console.ReadLine());
62 int sum3 = 0;
63 for (int i2 = 0; i2 < m; i2++) {
64 Console.Write(" %d 番目の人の点数? ", i2+1);
65 int z = int.Parse(Console.ReadLine());
66 sum3 += z;
67 }
68 sum3 /= m;
69 if (i1 == 0 || sum3 > max3)
70 max3 = sum3;
71 }
72 Console.WriteLine(" 最大値3(for による多重ループ) " + max3);
73 }
74 }
01 int sum1 = 0;
02 for (int i1 = 1; i1 <= 5; i1++)
03 sum1 += i1;
04 int sum2 = 0;
05 i1 = 1;
06 while (i1 <= 5) {
07 sum2 += i1;
08 i1++;
09 }
01 int sum1 = 0;
02 for (int i1 = 1; i1 <= 5; i1++)
03 sum1 += i1;
04 int sum2 = 0;
05 int i1 = 1;
06 while (i1 <= 5) {
07 sum2 += i1;
08 i1++;
09 }
if (y != 0) {
if (max2 == 0 || y > max2)
max2 = y;
}
if (sum3 > max3)
01 ''''''''''''''''''''''''''''
02 ' 繰り返しのプログラム例 '
03 ' coded by Y.Suganuma '
04 ''''''''''''''''''''''''''''
05 Module Test
06 Sub Main()
07 Dim i1 As Integer
08 ' 1 から 5 までの和( For )
09 Dim sum1 As Integer = 0
10 For i1 = 1 To 5 Step 1
11 sum1 += i1
12 Next
13 Console.WriteLine("For による和 " & sum1)
14 ' 1 から 5 までの和( For Each )
15 Dim sum As Integer = 0
16 Dim kk() As Integer = {1, 2, 3, 4, 5}
17 For Each k As Integer in kk
18 sum += k
19 Next
20 Console.WriteLine("For Each による和 " & sum)
21 ' 1 から 5 までの和( While )
22 Dim sum2 As Integer = 0
23 i1 = 1
24 Do While i1 <= 5
25 sum2 += i1
26 i1 += 1
27 Loop
28 Console.WriteLine("While による和 " & sum2)
29 ' 最大値の計算1( While )
30 Console.Write("整数データ(最大値1)?( 0 によって終了) ")
31 Dim x As Integer = Integer.Parse(Console.ReadLine())
32 Dim max1 As Integer = x
33 Do While x <> 0
34 If x > max1 Then
35 max1 = x
36 End If
37 Console.Write("整数データ(最大値1)?( 0 によって終了) ")
38 x = Integer.Parse(Console.ReadLine())
39 Loop
40 Console.WriteLine(" 最大値1(While) " & max1)
41 ' 最大値の計算2( While )
42 Dim max2 As Integer = 0
43 Dim y As Integer = 1
44 Do While y <> 0
45 Console.Write("整数データ(最大値2)?( 0 によって終了) ")
46 y = Integer.Parse(Console.ReadLine())
47 If y = 0 Then
48 Exit Do
49 Else
50 If max2 = 0 or y > max2
51 max2 = y
52 End If
53 End If
54 Loop
55 Console.WriteLine(" 最大値2(While) " & max2)
56 ' 最大値の計算( For による多重ループ )
57 Console.Write("クラスの数? ")
58 Dim n As Integer = Integer.Parse(Console.ReadLine())
59 Dim max3 As Double
60 For i1 = 1 To n
61 Console.Write(" クラス " & i1 & " の人数? ")
62 Dim m As Integer = Integer.Parse(Console.ReadLine())
63 Dim sum3 As Double = 0
64 For i2 As Integer = 1 To m
65 Console.Write(" " & i2 & " 番目の人の点数? ")
66 Dim z As Integer = Integer.Parse(Console.ReadLine())
67 sum3 += z
68 Next
69 sum3 /= m
70 If i1 = 0 or sum3 > max3 Then
71 max3 = sum3
72 End If
73 Next
74 Console.WriteLine(" 最大値3(For による多重ループ) " & max3)
75 End Sub
76 End Module
01 Dim sum1 As Integer = 0 02 For i1 As Integer = 1 To 5 Step 1 03 sum1 += i1 04 Next 05 Dim sum2 As Integer = 0 06 i1 = 1 07 Do While i1 <= 5 08 sum2 += i1 09 i1 += 1 10 Loop
01 Dim sum1 As Integer = 0 02 For i1 As Integer = 1 To 5 Step 1 03 sum1 += i1 04 Next 05 Dim sum2 As Integer = 0 06 Dim i1 As Integer = 1 07 Do While i1 <= 5 08 sum2 += i1 09 i1 += 1 10 Loop
If y <> 0 Then If max2 = 0 or y > max2 max2 = y End If End If
if (sum3 > max3)
| 情報学部 | 菅沼ホーム | 目次 | 索引 |