其他路徑:
CSDN: https://blog.csdn.net/wodehao0808
微信公衆號:程序喵星人
更多資源和視頻教程,QQ:1902686547
4. 流程控制
4.1 關係運算符
4.2 邏輯運算符
4.3 位運算符
4.4 goto語句
goto 語句將執行轉移到語句塊中的另一個標籤。
4.5 Example:關係運算符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 關係運算符
namespace Lesson_3_1
{
class Program
{
static void Main(string[] args)
{
// example_1
int iScore = 90;
bool bResult = iScore > 30; // 比較運算返回一個bool值,真爲true,假爲false
Console.WriteLine("bResult = " + bResult);
// example_2
int iNum1 = 56;
int iNum2 = 36;
bool bRet1 = iNum1 == iNum2; // false
bool bRet2 = iNum1 != iNum2; // true
bool bRet3 = iNum1 > iNum2; // true
bool bRet4 = iNum1 < iNum2; // false
bool bRet5 = iNum1 >= iNum2; // true
bool bRet6 = iNum1 <= iNum2; // false
Console.WriteLine("bRet1 = " + bRet1);
Console.WriteLine("bRet2 = " + bRet2);
Console.WriteLine("bRet3 = " + bRet3);
Console.WriteLine("bRet4 = " + bRet4);
Console.WriteLine("bRet5 = " + bRet5);
Console.WriteLine("bRet6 = " + bRet6);
// example_3
bool b = true;
bool b2 = false;
bool bRes1 = b & b2; // false
bool bRes2 = b | b2; // true
bool bRes3 = b ^ b2; // true
bool bRes4 = !b; // false
bool bRes5 = (b && b2); // false
bool bRes6 = (b || b2); // true
Console.WriteLine("bRes1 = " + bRes1);
Console.WriteLine("bRes2 = " + bRes2);
Console.WriteLine("bRes3 = " + bRes3);
Console.WriteLine("bRes4 = " + bRes4);
Console.WriteLine("bRes5 = " + bRes5);
Console.WriteLine("bRes6 = " + bRes6);
// example_4: goto
int iCount = 90;
goto myGotoLabel; // 跳轉到 myGotoLabel標籤處繼續執行代碼,這樣 ++iCount 將不會執行
++iCount;
myGotoLabel:
Console.WriteLine("iCount = " + iCount); // ++iCount 不會執行,所以輸出結果是90
// 對數字1到5進行迭代,模擬for循環:
int i = 1;
startLoop:
if (i <= 5)
{
Console.Write(i + " ");
i++;
goto startLoop;
}
Console.ReadKey();
}
}
}
4.6 判斷語句
4.6.1 if語句
if (<A>)
{
// 語句塊A
}
else
{
// 語句塊else
}
如果<A>爲真,則執行語句塊A;否則執行語句塊else。
if(<A>)
{
// 語句塊A
}
else if (<B>)
{
// 語句塊B
}
...
else
{
// 語句else
}
如果<A>爲真,則執行語句塊A;否則判斷<B>,如果<B>爲真,則執行語句塊B;否則...,如果所有if裏面的條件都不滿足,則執行語句塊else。
4.6.2 :?語句
語法:
<test> ? <resultfTrue> : <resultIfFalse>;
如果<test>爲真,則返回將結果<resultIfTrue>;否則返回結果<resultIfFalse>。
例如:
int i = 10;
int count = i > 9? i : -1; // 結果是10
int m = 8;
int num = m > 9 ? m : -1; // 結果是-1
4.6.3 Example: if 語句,和 ?:語句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// if 語句,和 ?:語句
namespace Lesson_3_2
{
class Program
{
static void Main(string[] args)
{
// example_1: if
Console.WriteLine("請輸入分數:");
int iScore = Convert.ToInt32(Console.ReadLine());
if (iScore >= 60)
{
Console.WriteLine("及格");
}
else
{
Console.WriteLine("未及格");
}
// example_2: ?:
Console.WriteLine("請輸入語文分數:");
int iYWScore = Convert.ToInt32(Console.ReadLine());
string strOut = iYWScore >= 90 ? "語文及格" : "語文未及格";
Console.WriteLine("語文分數結果是(90分及格):" + strOut);
// example_3: if...if else ... if else ... else
Console.WriteLine("請輸入英語分數(0~150):");
int iYYScore = Convert.ToInt32(Console.ReadLine());
string str = "";
if (iYYScore >= 130)
{
str = "英語優秀";
}
else if (iYYScore >= 100)
{
str = "英語良好";
}
else if (iYYScore > 90)
{
str = "英語一般";
}
else
{
str = "英語偏科";
}
Console.WriteLine("英語評價是(總分150):" + str);
Console.ReadKey();
}
}
}
4.6.4 switch語句
語法:
switch(<test>)
{
case A:
...
break;
case B:
...
break;
...
default:
...
break;
}
當<test>中的內容,與case中的條件相匹配時,就會執行case中的語句塊。break會終止switch繼續向下進行並退出switch語句。
default,當所有case中的條件都不滿足時,就會執行default中的語句塊。
case允許多個條件判斷通過後,執行同一個語句塊。例如:
int i = 2;
swtich(i)
{
case 0:
case 1:
case 2:
case 3:
// 以上4個條件滿足任意一個,都執行該語句塊
Console.WriteLine("小於4的數");
break;
case 4:
Console.WriteLine("等於4的數");
break;
case 5:
case 6:
// 以上2個條件滿足任意一個,都執行該語句塊
Console.WriteLine("大於4,小於7的數");
break;
default:
Console.WriteLine("大於6的數");
break;
}
4.6.5 Example: switch
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// switch
namespace Lesson_3_3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入月份(1-12):");
int iMonth = Convert.ToInt32(Console.ReadLine());
switch (iMonth)
{
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("輸入的月份有誤,輸入的月份是:" + iMonth);
break;
}
Console.WriteLine("請輸入方向鍵(a:左,s:下,d:右,w:上):");
char ch = Convert.ToChar(Console.ReadLine().ToLower());
switch (ch)
{
case 'a':
Console.WriteLine("向左");
break;
case 's':
Console.WriteLine("向下");
break;
case 'd':
Console.WriteLine("向右");
break;
case 'w':
Console.WriteLine("向上");
break;
default:
Console.WriteLine("輸入的方向有誤,輸入的方向是:" + ch);
break;
}
Console.ReadKey();
}
}
}
4.7 循環語句
4.7.0 死循環
死循環:
循環會一直執行,永不停止。
例如:
while( true ) // 條件永遠爲真,循環一直執行不會停止,即死循環。
{ }
4.7.1 do...while循環
語法:
do
{
// 循環體
} while(<條件>);
注意:
1.循環體一定會執行一次;
2.while後面有分號 ";" ,容易遺忘;
do...while循環,是先執行一次循環體,再進行條件判斷;如果條件爲真,則繼續執行循環體,直到條件爲假(false)退出循環。
4.7.2 while循環
語法:
while(<條件>)
{
// 循環體
}
while循環,先進行條件判斷,如果爲真,就執行循環體,直到條件爲假(false)退出循環。
4.7.3 for循環
for循環語法:
for (init; condition; increment)
{
// 循環體
}
for 循環的控制流:
1.init會首先被執行,且只會執行一次。這一步允許聲明並初始化任何循環控制變量。也可以不在這裏寫任何語句,只要有一個分號出現即可。
2.接下來,會判斷 condition。如果爲真,就執行循環體。如果爲假,則不執行循環體,退出循環語句;
3.在執行完 for 循環主體後,控制流會跳回上面的 increment 語句。該語句允許你更新循環控制變量。該語句可以留空,只要在條件後有一個分號出現即可。
4.條件再次被判斷。如果爲真,則執行循環,這個過程會不斷重複(循環主體,然後增加步值,再然後重新判斷條件)。在條件變爲假時,for 循環終止。
例如:
// 1
for (int i = 1; i < 9; ++i)
{
Console.WriteLine(i);
}
// 2
int i = 1; // 初始化寫在外面。
for (; i < 9; ++i)
{
Console.WriteLine(i);
}
// 3
int i = 1;
// 初始化寫在外面。
for (; i < 9; )
{
Console.WriteLine(i);
++i; // 控制變量的數值的修改,寫在循環體裏面,沒有寫在for後面。
}
4.7.4 foreach循環
使用foreach可以迭代數組或者一個集合對象。
不要在foreach中試圖增刪,這樣會報異常!需使用for!
以下實例有三個部分:
1.通過 foreach 循環輸出整型數組中的元素。
2.通過 for 循環輸出整型數組中的元素。
3.foreach 循環設置數組元素的計算器。
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int element in fibarray)
{
System.Console.WriteLine(element);
}
System.Console.WriteLine();
// 類似 foreach 循環
for (int i = 0; i < fibarray.Length; i++)
{
System.Console.WriteLine(fibarray[i]);
}
System.Console.WriteLine();
// 設置集合中元素的計算器
int count = 0;
foreach (int element in fibarray)
{
count += 1;
System.Console.WriteLine("Element #{0}: {1}", count, element);
}
System.Console.WriteLine("Number of elements in the array: {0}", count);
4.7.5 循環控制語句
1.break語句
break:
終止當前循環語句;
2.continue語句
continue:
跳過本次循環體執行,進入下一次的循環邏輯處理;
3.return語句
return:
終止函數所有邏輯,退出該函數。
4.7.6 Example:循環語句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 循環語句
namespace Lesson_3_4
{
class Program
{
static void Main(string[] args)
{
// do...while
do
{
Console.WriteLine("do...while 循環體一定會執行一次");
} while (false);
int i = 1;
do
{
Console.WriteLine("do...while i = " + i);
++i;
} while (i < 10);
// while
i = 1;
while (i < 10)
{
Console.WriteLine("while i = " + i);
++i;
}
// for
i = 1;
for (; i < 10; ++i)
{
Console.WriteLine("for 1 i = " + i);
}
i = 1;
for (; i < 10; )
{
Console.WriteLine("for 2 i = " + i);
++i;
}
for (int j = 1; j < 10; ++j)
{
Console.WriteLine("for 3 j = " + j);
}
// break
for (int j = 1; j < 10; ++j)
{
if (5 == j)
{
break; // 結束循環 // 這裏是5及5以上的數字不會輸出
}
Console.WriteLine("break j = " + j);
}
Console.WriteLine("break,這裏會執行");
// break
for (int j = 1; j < 10; ++j)
{
if (5 == j)
{
continue; // 結束本次循環體,繼續下一個條件判斷 // 這裏是 數字5 不會輸出
}
Console.WriteLine("continue j = " + j);
}
Console.WriteLine("continue,這裏會執行");
// return
for (int j = 1; j < 10; ++j)
{
if (5 == j)
{
return; // 結束循環,並退出函數 // 這裏是5及5以上的數字不會輸出
}
Console.WriteLine("continue j = " + j);
}
Console.WriteLine("return了,這裏和該函數後面的代碼都不會執行了");
Console.ReadKey();
}
}
}
4.8 練習
練習1
編寫一個程序,對輸入的4個整數,求出其中的最大值和最小值,並顯示出來。
練習2
讓用戶輸入兩個整數,然後再輸入0-3之間的一個數,0代表加法,1代表減法,2代表乘法,3代表除法,計算這兩個數字的結果。
練習3
求出1~1000之間的所有能被7整除的數,並計算和輸出每5個的和。
練習4
編寫一個控制檯程序,分別輸出1~100之間的平方、平方根。
練習6
編程輸出1~100中能被3整除但不能被5整除的數,並統計有多少個這樣的數。
練習7
編程輸出1000以內的所有素數
練習8
編程輸出九九乘法表。
練習9
編寫一個擲篩子100次的程序,並打印出各種點數的出現次數。
練習10
一個控制檯應用程序,輸出1~5的平方值,要求:用for語句實現。用while語句實現。用do-while語句實現。
練習11
一個控制檯應用程序,要求用戶輸入5個大寫字母,如果用戶輸入的信息不滿足要求,提示幫助信息並要求重新輸入。
練習12
一個控制檯應用程序,要求完成寫列功能。
1)接收一個整數n。
2)如果接收的值n爲正數,輸出1~n間的全部整數。
3)如果接收的值n爲負值,用break或者return退出程序。
4)如何n爲0的話 轉到1繼續接收下一個整數。
練習13
一個控制檯應用程序,求1000之內的所有“完數”。所謂“完數”是指一個數恰好等於它的所有因子之和。例如6是完數,因爲6=1+2+3。
4.8.1 Example:練習1、2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 練習1、練習2
namespace Lesson_3_5
{
class Program
{
static void Main(string[] args)
{
// example_1: 編寫一個程序,對輸入的4個整數,求出其中的最大值和最小值,並顯示出來。
Console.WriteLine("輸入4個整數,請輸入第一個整數:");
int iNum1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("輸入4個整數,請輸入第二個整數:");
int iNum2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("輸入4個整數,請輸入第三個整數:");
int iNum3 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("輸入4個整數,請輸入第四個整數:");
int iNum4 = Convert.ToInt32(Console.ReadLine());
int iMax = iNum1, iMin = iNum2; // 最大數 和 最小數
if (iMax < iNum2)
{
iMax = iNum2;
}
if (iMax < iNum3)
{
iMax = iNum3;
}
if (iMax < iNum4)
{
iMax = iNum4;
}
if (iMin > iNum2)
{
iMin = iNum2;
}
if (iMin > iNum3)
{
iMin = iNum3;
}
if (iMin > iNum4)
{
iMin = iNum4;
}
Console.WriteLine("最大數:{0},最小數:{1}", iMax, iMin);
// example_2: 讓用戶輸入兩個整數,然後再輸入0-3之間的一個數,0代表加法,1代表減法,2代表乘法,3代表除法,計算這兩個數字的結果
Console.WriteLine("輸入兩個整數,請輸入第一個整數:");
int iInt1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("輸入兩個整數,請輸入第二個整數:");
int iInt2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入0-3之間的一個數:");
int iOperator = Convert.ToInt32(Console.ReadLine());
int iTemp = 0;
switch (iOperator)
{
case 0:
iTemp = iInt1 + iInt2;
break;
case 1:
iTemp = iInt1 - iInt2;
break;
case 2:
iTemp = iInt1 * iInt2;
break;
case 3:
iTemp = iInt1 / iInt2;
break;
default:
Console.WriteLine("輸入的操作數有誤,不是0-3。");
break;
}
Console.WriteLine("執行操作後的結果爲:" + iTemp);
Console.ReadKey();
}
}
}
4.8.2 Example:練習3、4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 練習3、4
namespace Lesson_3_6
{
class Program
{
static void Main(string[] args)
{
// example_3: 求出1~1000之間的所有能被7整除的數,並計算和輸出每5個的和。
int iCount = 0, iSum = 0;
for (int i = 1; i < 1001; ++i)
{
if(i % 7 == 0)
{
Console.WriteLine("能被7整除的數:" + i);
iSum += i;
++iCount;
if (5 == iCount)
{
Console.WriteLine("每5個的和:" + iSum);
iSum = 0;
iCount = 0;
}
}
}
Console.WriteLine("example_3 finish------------------------------------------------");
// example_4: ,編寫一個控制檯程序,分別輸出1~100之間的平方、平方根。
for (int i = 1; i < 101; ++i)
{
Console.WriteLine(i + "的平方是:" + i * i);
// double d = Math.Sqrt(i);
Console.WriteLine(i + "的平方根是:" + Math.Sqrt(i)); // Math.Sqrt方法爲:求平方根的方法。
}
Console.WriteLine("example_4 finish------------------------------------------------");
Console.ReadKey();
}
}
}
4.8.3 Example:練習6、7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 練習6、7
namespace Lesson_3_7
{
class Program
{
static void Main(string[] args)
{
// example_6: 編程輸出1~100中能被3整除但不能被5整除的數,並統計有多少個這樣的數。
int iCount = 0;
for (int i = 1; i < 101; ++i)
{
if (i % 3 == 0 && i % 5 != 0)
{
Console.WriteLine("能被3整除但不能被5整除的數:" + i);
++iCount;
}
}
Console.WriteLine("總計有:" + iCount);
Console.WriteLine("example_6 finish -----------------------------------------");
// example_7: 編程輸出1000以內的所有素數
for (int i = 2; i < 1001; ++i)
{
bool b = true;
for (int j = 2; j < i; ++j)
{
if (i % j == 0) // 素數只能被1和自身整除,不能被其他數整除。所以,如果能被其他數整除,就不是素數。
{
b = false;
break;
}
}
if (b)
{
Console.WriteLine("素數是:" + i);
}
}
Console.WriteLine("example_7 finish -----------------------------------------");
Console.ReadKey();
}
}
}
4.8.4 Example:練習8、9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 練習8、9
namespace Lesson_3_8
{
class Program
{
static void Main(string[] args)
{
// example_8: 編程輸出九九乘法表。
for (int i = 1; i < 10; ++i)
{
for (int j = i; j < 10; ++j)
{
Console.Write("{0}*{1}={2}\t", i, j, i * j);
}
Console.WriteLine();
}
Console.WriteLine("example_8 finish ----------------------------------------");
// example_9: 編寫一個擲篩子100次的程序,並打印出各種點數的出現次數。
int iNum1 = 0, iNum2 = 0, iNum3 = 0, iNum4 = 0, iNum5 = 0, iNum6 = 0;
int iPointNum = 0;
Random rand = new Random();
for (int i = 0; i < 100; ++i)
{
iPointNum = rand.Next(1, 7); // 隨機數,前包後不包[1,7),獲得的是隨機1~6的數
switch (iPointNum)
{
case 1:
++iNum1;
break;
case 2:
++iNum2;
break;
case 3:
++iNum3;
break;
case 4:
++iNum4;
break;
case 5:
++iNum5;
break;
case 6:
++iNum6;
break;
}
}
Console.WriteLine("1出現的次數是:" + iNum1);
Console.WriteLine("2出現的次數是:" + iNum2);
Console.WriteLine("3出現的次數是:" + iNum3);
Console.WriteLine("4出現的次數是:" + iNum4);
Console.WriteLine("5出現的次數是:" + iNum5);
Console.WriteLine("6出現的次數是:" + iNum6);
Console.WriteLine("example_9 finish ----------------------------------------");
Console.ReadKey();
}
}
}
4.8.5 Example:練習10、11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 練習10、11
namespace Lesson_3_9
{
class Program
{
static void Main(string[] args)
{
// example_10: 一個控制檯應用程序,輸出1~5的平方值,要求:用for語句實現。用while語句實現。用do-while語句實現。
for (int i = 1; i < 6; ++i)
{
Console.WriteLine("for {0}的平方值是:{1}", i, i * i);
}
int j = 1;
while (j < 6)
{
Console.WriteLine("while {0}的平方值是:{1}", j, j * j);
++j;
}
int m = 1;
do
{
Console.WriteLine("do...while {0}的平方值是:{1}", m, m * m);
++m;
} while (m < 6);
Console.WriteLine("example_10 finish-----------------------------------------");
// example_11: 一個控制檯應用程序,要求用戶輸入5個大寫字母,如果用戶輸入的信息不滿足要求,提示幫助信息並要求重新輸入。
bool bNeedInput = true;
Console.WriteLine("請輸入字符串:");
while (bNeedInput)
{
bNeedInput = false;
string str = Console.ReadLine();
if (str.Length == 5)
{
for (int i = 0; i < 5; ++i)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
//
}
else
{
bNeedInput = true;
break;
}
}
}
else
{
bNeedInput = true;
}
if (bNeedInput)
{
Console.WriteLine("請重新輸入字符串:");
}
else
{
Console.WriteLine("符合5個大寫字母");
}
}
Console.ReadKey();
}
}
}
4.8.6 Example:練習12、13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// 練習12、13
namespace Lesson_3_10
{
class Program
{
static void Main(string[] args)
{
// example_12:
/*
* 一個控制檯應用程序,要求完成寫列功能。
1)接收一個整數n。
2)如果接收的值n爲正數,輸出1~n間的全部整數。
3)如果接收的值n爲負值,用break或者return退出程序。
4)如何n爲0的話 轉到1繼續接收下一個整數。
*/
bool b = true;
while (b)
{
b = false;
Console.WriteLine("請輸入一個整數:");
int iNum = Convert.ToInt32(Console.ReadLine());
if (iNum > 0)
{
for(int i = 1; i <= iNum; ++i)
{
Console.WriteLine("1~n的全部整數:" + i);
}
}
else if (iNum <0)
{
break;
}
else
{
b = true;
}
}
Console.WriteLine("example_12 finish-------------------------------");
// example_13: 一個控制檯應用程序,求1000之內的所有“完數”。所謂“完數”是指一個數恰好等於它的所有因子之和。例如6是完數,因爲6=1+2+3。
int iSum = 0;
string str = "";
for (int i = 1; i < 1001; ++i)
{
iSum = 0;
str = "";
for (int j = 1; j < i - 1; ++j)
{
if (i % j == 0)
{
if (string.IsNullOrEmpty(str))
{
str = j + "";
}
else
{
str += "+" + j;
}
iSum += j;
}
}
if (iSum == i)
{
Console.WriteLine("完數:{0} = {1}", i, str);
}
}
Console.WriteLine("example_13 finish-------------------------------");
Console.ReadKey();
}
}
}