Visual C 2010 Express 4 Statement C Programming

  • Slides: 23
Download presentation
Visual C# 2010 Express 4. Statement พนฐานการโปรแกรม ตองทราบการใชคำสง การควบคม C# Programming with Visual C#

Visual C# 2010 Express 4. Statement พนฐานการโปรแกรม ตองทราบการใชคำสง การควบคม C# Programming with Visual C# 2010 Express 1

������������������� If. . else ? : switch while do while for, foreach ���������� go

������������������� If. . else ? : switch while do while for, foreach ���������� go to, break, continue return C# Programming with Visual C# 2010 Express 2

������� if If �������������������������� else ������ if (Boolean-expression) else … �������� ���. . .

������� if If �������������������������� else ������ if (Boolean-expression) else … �������� ���. . . �������� number % 2 == ����������� 1. if ([condition]) 2. 3. 4. 5. 6. 7. 8. { ]code to execute if condition is true[ } else { ]code to execute if condition is false[ } C# Programming with Visual C# 2010 Express 5

��������� if. . else if 1. if ([condition 1]) 2. { [code to execute

��������� if. . else if 1. if ([condition 1]) 2. { [code to execute if condition 1 is true] 3. 4. 5. 6. 7. 8. 9. 10. } else if ([condition 2]) { [code to execute if condition is false and condition 2 is true] } else { [code to execute if condition 1 and condition 2 are both false] 11. 12. } C# Programming with Visual C# 2010 Express 6

��������� if…else if 1. if (score > 79) 2. { Console. Write. Line("Get A");

��������� if…else if 1. if (score > 79) 2. { Console. Write. Line("Get A"); 3. 4. } 5. else if (score > 69) 6. { Console. Write. Line("Get B"); 7. 8. } 9. else if (score > 59) { 10. Console. Write. Line("Get C"); 11. 12. } 13. else if (score > 49) { 14. Console. Write. Line("Get D"); 15. 16. } C# Programming with Visual C# 2010 Express 7

������� Ether…or �������������� If…else �������� Ether…or ������������� ? : ����������� Data. Type result =

������� Ether…or �������������� If…else �������� Ether…or ������������� ? : ����������� Data. Type result = [condition] ? [true expression] : [false expression] int age = 12; Console. Write. Line((age > 15) ? "You are adult. " : "You are boy. "); C# Programming with Visual C# 2010 Express 8

����������� switch ��������� ������ if. . else �������������� switch … case ����� switch ([expression

����������� switch ��������� ������ if. . else �������������� switch … case ����� switch ([expression to check)] ������������������� case[ ] �������������� switch ([expression to check]) { ������� break ; case [test 1]: . . . [exit case statement] case [test 2]: . . . [exit case statement] default: . . . [exit case statement] } C# Programming with Visual C# 2010 Express 9

������� switch ����� case ������������� case ��� enum Month. Name { January, February, March,

������� switch ����� case ������������� case ��� enum Month. Name { January, February, March, April, May, ���������� June, July, August, September, October, November, December } Month. Name current = Month. Name. June; int month. Days; switch (current) { case Month. Name. February : month. Days = 28; break; case Month. Name. April : case Month. Name. June : case Month. Name. September : case Month. Name. November : month. Days = 30; break; default : month. Days = 31; break; } C# Programming with Visual C# 2010 Express 10

������ goto �� switch C#��� ������ goto ������� (label) case ��� default �������� case

������ goto �� switch C#��� ������ goto ������� (label) case ��� default �������� case ��� default ��� switch ����������� switch (number % 10) { case 1: if (number / 10 == 1) { Console. Write. Line("case 1"); break; } goto case 2; case 2: if (number / 10 == 2) { Console. Write. Line("case 2"); break; } goto default; default: Console. Write. Line("default"); break; } C# Programming with Visual C# 2010 Express 11

������ While �������� (loop) ��� While ���������������������������� Boolean ���� /���� while ([condition]) { //Code

������ While �������� (loop) ��� While ���������������������������� Boolean ���� /���� while ([condition]) { //Code to loop. { int i = 0; while (i < 10) { Console. Wrile. Line(i++); } C# Programming with Visual C# 2010 Express 12

���������� while ������������������� int i = 0; while (i < 10) { Console. Wrile.

���������� while ������������������� int i = 0; while (i < 10) { Console. Wrile. Line(i++); } while (i < 10) { Console. Wrile. Line(i); i++; } C# Programming with Visual C# 2010 Express 13

���������� break, continue ������ break ����������� switch, while, do , foreach ����� continue ��������������������������

���������� break, continue ������ break ����������� switch, while, do , foreach ����� continue �������������������������� while, do, foreach �������������� int i = 0; while (true) { Console. Write. Line(i); i++; if (i < 5( continue; else break; { C# Programming with Visual C# 2010 Express 18

������������������ 1. A. B. C. D. E. if num – 2 ==0 { …}

������������������ 1. A. B. C. D. E. if num – 2 ==0 { …} if (num < 0) || (num > 100) { … } if (num == 60); num = 0; switch (num){ case 1, 2 : num = 2; case 3: num = 3; default: num= 0; } for (int i = 0, I < 5, i++) Console. Write. Line(i); C# Programming with Visual C# 2010 Express 21

������������������ 1. G. int i = 0; while (i < 10) Console. Write. Line(i);

������������������ 1. G. int i = 0; while (i < 10) Console. Write. Line(i); do { string r = Console. Read. Line( ); test = int. Parse(r); } H. While ( 0 != test) F. int i = 0; while (true){ Console. Write. Line(i); i++; if (i < 5) continue; else{ break; } i; ++ { C# Programming with Visual C# 2010 Express 22