C#ゼロ基礎学習ノート003-フロー制御文
3.プロセス制御文
制御文は、プログラムフローの選択、ループ、ステアリング、リターンなどの制御を実現するために使用される.
用途:制御文はプログラムの流れを制御し、プログラムの様々な構造方式を実現する.
これらは、特定の文定義子で構成されます.C#言語には9つの制御文があります.次の3つに分類できます.
1.条件判断文
if文、switch文;
2.ループ実行文
do while文、while文、for文、foreach文
3.ステアリング文
break文、continue文、return文、goto文(この文は構造化されたプログラム設計に不利であるため、乱用するとプログラムの流れが呉の法則になり、可読性が悪い)
1.構造文の選択
1.if条件制御文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//if文
namespace Lesson_10_1{class Program{static void Main(string[]args){/*//example_1:大人かどうかを判断するConsole.WriteLine("年齢を入力してください");string strAge=Console.ReadLine();//ユーザーの入力を受け入れるint intAge=Convert.ToInt 32(strAge);//選択文if(intAge>=18){Console.WriteLine("ユーザーは大人");}else{Console.WriteLine("ユーザ未成年");}Console.ReadKey();//ユーザの入力を待ち、画面が点滅しないように*/
}
2.switch条件制御文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//switch文
namespace Lesson_11_1{class Program{static void Main(string[]args){/*//example_1:節気を入力し、対応する節気Console.WriteLine(「節気を入力してください、1-4」);string strInput=Console.ReadLine();switch(strInput){case"1":Console.WriteLine("春");break;//breakを加えると、下で比較しないことを示し、switch case「2」:Console.WriteLine(「夏」);break;case「3」:Console.WriteLine(「秋」);break;case「4」:Console.WriteLine(「冬」);break;default:Console.WriteLine(「正しい節気を入力してください(1-4)」);break;*
}
2.循環構造文
定義:
ループ構造は、指定された条件が真であるまで、コマンドのセットに対して一定の回数を実行したり、コマンドのセットを繰り返したりするために使用されます.
1.whileループ制御文
定義:whileループは、指定した条件が偽になるまで、指定した文を繰り返し実行します.
構文:
while(条件)
{
//文
}
ケース1:
計算は1+2+3+...+100の和から?
ケース2:
銀行は5000元人民元を預け入れて、年利率は1.0414で、計算式は:
1年目:5000*1.0414;
翌年:5000*1.0414*1.0414;
…
計算して何年の預金を経て、元利の合計は10000元を超えることができますか?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//whileループ文
namespace Lesson_12_1{class Program{static void Main(string[]args){/*//example_1:1+2+3...+100の和int i=1を求め、sum=0;//whileループ、whileの後の論理式の値を判断し、値が真の場合、whileの中の文{}を実行します.while(i<=100){sum+=i;++i;+}Console.WriteLine(sum);Console.ReadKey();//ぴかぴかしないように*/
}
2.do…while循環制御文
do...whileサイクルはwhileサイクルと同様であり、両者の違いはdo...whileサイクルにおける即時条件が偽である場合でも、少なくとも1回このサイクル体の文を実行することである.
構文:
do
{
//文
}while(条件)
ケーススタディ–do...whileループを使用して実現
1.1+2+3+4...+nから78までの値を算出します.nの値を求めますか?
2.銀行は5000元人民元を預け入れて、年利率は1.0414で、計算式は:1年目:5000*1.0414;2年目:5000*1.0414*1.0414;...;計算:何年の預金を経て、元利の合計は10000元を超えることができますか?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//do whileサイクル
namespace Lesson_13_1{class Program{static void Main(string[]args){/*//example_1:do...whileループint i=10;do//どうしても1回のループ体{Console.WriteLine(i);}while(i<0);*/
}
3.デッドサイクル
コードの作成エラーやプログラムロジックエラーによって永遠に終了できないループが定義されると、無限ループとなり、デッドループとも呼ばれる.
例:
int i = 1;
while( true )
{
++i;
}
//while条件は永遠にtrueであり、死循環である.
4.for循環制御文
forループは,特定の条件を判断した後にのみループを許可することを要求する.
このループは、ある文または文ブロックを所定回数繰り返し実行する場合に使用される.
構文:
for(初期値;条件;増減)
{
//文
}
ケース:
forループを用いて,1+2+3+...+nの値を求め,nはコンソール入力の数値である.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//forサイクル
namespace Lesson 14_1{class Program{static void Main(string[]args){/*//example_1:1から100までのループfor(int i=1;i<=100;++i){Console.WriteLine(i)}*/
}
3.ステアリング文
C#は、ループの実行を制御する4つのコマンドを提供します.
break–ループをすぐに終了します.
continue–現在のループをすぐに終了し、次のループを行います.
goto–ループから飛び出して、マークされたコードの位置に移動できます.
return–ループとその含まれる関数体から飛び出します.
ケーススタディ:
1.1+2+3+...+n>=78からnの最小値を計算しますか?
ジョブ:
1.フィボナッチ数列を求めます.ここでnはコンソールにランダムに入力された数字です.
a.说明:フィボナッチ数列(Fibonacci Sequence)は、黄金分割数列とも呼ばれ、1,1,2,3,5,8,13,21...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//ステアリング文
namespace Lesson 15_1{class Program{static void Main(string[]args){/*//example_1:1から100までbreakはループ全体for(int i=1;i<=100;++i){if(50==i){break;//ループ全体から飛び出し}Console.WriteLine(i);//出力は1~49}*/
}
制御文は、プログラムフローの選択、ループ、ステアリング、リターンなどの制御を実現するために使用される.
用途:制御文はプログラムの流れを制御し、プログラムの様々な構造方式を実現する.
これらは、特定の文定義子で構成されます.C#言語には9つの制御文があります.次の3つに分類できます.
1.条件判断文
if文、switch文;
2.ループ実行文
do while文、while文、for文、foreach文
3.ステアリング文
break文、continue文、return文、goto文(この文は構造化されたプログラム設計に不利であるため、乱用するとプログラムの流れが呉の法則になり、可読性が悪い)
1.構造文の選択
1.if条件制御文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//if文
namespace Lesson_10_1{class Program{static void Main(string[]args){/*//example_1:大人かどうかを判断するConsole.WriteLine("年齢を入力してください");string strAge=Console.ReadLine();//ユーザーの入力を受け入れるint intAge=Convert.ToInt 32(strAge);//選択文if(intAge>=18){Console.WriteLine("ユーザーは大人");}else{Console.WriteLine("ユーザ未成年");}Console.ReadKey();//ユーザの入力を待ち、画面が点滅しないように*/
// example_2: 18 ,18~35 ,36~60 ,61
Console.WriteLine( " :" );
string strAge = Console.ReadLine(); //
int intAge = Convert.ToInt32( strAge );
if ( intAge < 18 ) {
Console.WriteLine( " " );
}
else if ( intAge >= 18 && intAge <= 35 ) {
Console.WriteLine( " " );
}
else if ( intAge >= 36 && intAge <= 60 ) {
Console.WriteLine( " " );
}
else {
Console.WriteLine( " " );
}
Console.ReadKey(); // , 。
}
}
}
2.switch条件制御文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//switch文
namespace Lesson_11_1{class Program{static void Main(string[]args){/*//example_1:節気を入力し、対応する節気Console.WriteLine(「節気を入力してください、1-4」);string strInput=Console.ReadLine();switch(strInput){case"1":Console.WriteLine("春");break;//breakを加えると、下で比較しないことを示し、switch case「2」:Console.WriteLine(「夏」);break;case「3」:Console.WriteLine(「秋」);break;case「4」:Console.WriteLine(「冬」);break;default:Console.WriteLine(「正しい節気を入力してください(1-4)」);break;*
// example_2: ,
Console.WriteLine( " ,1-3 ,4-6 ,7-9 ,10-12 " );
string strInput = Console.ReadLine();
switch( strInput )
{
case "1":
case "2":
case "3":
// , 。
Console.WriteLine( " " );
break;
case "4":
case "5":
case "6":
// , 。
Console.WriteLine( " " );
break;
case "7":
case "8":
case "9":
// , 。
Console.WriteLine( " " );
break;
case "10":
case "11":
case "12":
// , 。
Console.WriteLine( " " );
break;
default:
Console.WriteLine( " (1-12)" );
break;
}
Console.ReadKey(); //
}
}
}
2.循環構造文
定義:
ループ構造は、指定された条件が真であるまで、コマンドのセットに対して一定の回数を実行したり、コマンドのセットを繰り返したりするために使用されます.
1.whileループ制御文
定義:whileループは、指定した条件が偽になるまで、指定した文を繰り返し実行します.
構文:
while(条件)
{
//文
}
ケース1:
計算は1+2+3+...+100の和から?
ケース2:
銀行は5000元人民元を預け入れて、年利率は1.0414で、計算式は:
1年目:5000*1.0414;
翌年:5000*1.0414*1.0414;
…
計算して何年の預金を経て、元利の合計は10000元を超えることができますか?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//whileループ文
namespace Lesson_12_1{class Program{static void Main(string[]args){/*//example_1:1+2+3...+100の和int i=1を求め、sum=0;//whileループ、whileの後の論理式の値を判断し、値が真の場合、whileの中の文{}を実行します.while(i<=100){sum+=i;++i;+}Console.WriteLine(sum);Console.ReadKey();//ぴかぴかしないように*/
// example_2: 10000
double dblBen = 5000;
int intCount = 0;
while( dblBen < 10000 )
{
dblBen *= 1.0414;
Console.WriteLine( dblBen );
++intCount;
}
Console.WriteLine( " {0} , 10000 ", intCount ); // {0}, intCount {0}。
Console.ReadKey(); //
}
}
}
2.do…while循環制御文
do...whileサイクルはwhileサイクルと同様であり、両者の違いはdo...whileサイクルにおける即時条件が偽である場合でも、少なくとも1回このサイクル体の文を実行することである.
構文:
do
{
//文
}while(条件)
ケーススタディ–do...whileループを使用して実現
1.1+2+3+4...+nから78までの値を算出します.nの値を求めますか?
2.銀行は5000元人民元を預け入れて、年利率は1.0414で、計算式は:1年目:5000*1.0414;2年目:5000*1.0414*1.0414;...;計算:何年の預金を経て、元利の合計は10000元を超えることができますか?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//do whileサイクル
namespace Lesson_13_1{class Program{static void Main(string[]args){/*//example_1:do...whileループint i=10;do//どうしても1回のループ体{Console.WriteLine(i);}while(i<0);*/
/*
// example_2: 1 + 2 + 3 + 4 ... + n 78 。 n ?
int intNum = 1, intSum = 0;
do
{
intSum += intNum;
++intNum;
} while ( intSum < 78 );
Console.WriteLine( intNum );
*/
// example_3: , 10000
double dblTotal = 5000;
int intYear = 0;
do
{
dblTotal *= 1.0414;
++intYear;
} while ( dblTotal < 10000 );
Console.WriteLine( intYear );
}
}
}
3.デッドサイクル
コードの作成エラーやプログラムロジックエラーによって永遠に終了できないループが定義されると、無限ループとなり、デッドループとも呼ばれる.
例:
int i = 1;
while( true )
{
++i;
}
//while条件は永遠にtrueであり、死循環である.
4.for循環制御文
forループは,特定の条件を判断した後にのみループを許可することを要求する.
このループは、ある文または文ブロックを所定回数繰り返し実行する場合に使用される.
構文:
for(初期値;条件;増減)
{
//文
}
ケース:
forループを用いて,1+2+3+...+nの値を求め,nはコンソール入力の数値である.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//forサイクル
namespace Lesson 14_1{class Program{static void Main(string[]args){/*//example_1:1から100までのループfor(int i=1;i<=100;++i){Console.WriteLine(i)}*/
// example_2: 1 + 2 + 3 + ... + n
Console.WriteLine( " n :" );
int intNum = Convert.ToInt32( Console.ReadLine() ), intSum = 0;
for( int i = 1; i <= intNum; ++i )
{
intSum += i;
}
Console.WriteLine( intSum );
/*
// example_3:
for (int i = 1; i < 10; ++i)
{
for (int j = 1; j <= i; ++j) //
{
Console.Write( "{0}*{1}={2}\t", j, i, j * i ); // \t
}
Console.WriteLine();
}
*/
}
}
}
3.ステアリング文
C#は、ループの実行を制御する4つのコマンドを提供します.
break–ループをすぐに終了します.
continue–現在のループをすぐに終了し、次のループを行います.
goto–ループから飛び出して、マークされたコードの位置に移動できます.
return–ループとその含まれる関数体から飛び出します.
ケーススタディ:
1.1+2+3+...+n>=78からnの最小値を計算しますか?
ジョブ:
1.フィボナッチ数列を求めます.ここでnはコンソールにランダムに入力された数字です.
a.说明:フィボナッチ数列(Fibonacci Sequence)は、黄金分割数列とも呼ばれ、1,1,2,3,5,8,13,21...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
//ステアリング文
namespace Lesson 15_1{class Program{static void Main(string[]args){/*//example_1:1から100までbreakはループ全体for(int i=1;i<=100;++i){if(50==i){break;//ループ全体から飛び出し}Console.WriteLine(i);//出力は1~49}*/
/*
// example_2: 1 100, continue ,
for ( int i = 1; i <= 100; ++i )
{
if ( 50 == i )
{
continue; // ,
}
Console.WriteLine( i ); // , :50
}
*/
/*
// example_3: 。 abc, a * a * a + b * b * b + c * c * c = abc,
for ( int i = 100; i < 1000; ++i )
{
int a = i / 100; //
int b = i % 100 / 10; //
int c = i % 10; //
if ( ( a * a * a + b * b * b + c * c * c ) != i )
{
continue;
}
Console.WriteLine( i );
}
*/
// example_4: 1~1000
for (int i = 2; i <= 1000; ++i)
{
bool bSu = true;
for (int j = 2; j < i; ++j)
{
if (i % j == 0)
{
bSu = false;
break;
}
}
if (bSu)
{
Console.WriteLine(i);
}
}
}
}
}