Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIM313 – Advanced Programming Techniques Flow Control 1.

Similar presentations


Presentation on theme: "BIM313 – Advanced Programming Techniques Flow Control 1."— Presentation transcript:

1 BIM313 – Advanced Programming Techniques Flow Control 1

2 Contents Flow Control – if, switch – while, do-while, for, foreach – Binary operators 2

3 Flow Control Branching (if, switch, ternary operator) Looping (for, while, do-while, foreach) 3

4 Comparison Operators OperatorMeaning ==equal to !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to 4

5 Boolean Variables A Boolean variable may take values true or false – bool isWhite = true; – isWhite = false; Comparison results can be stored in Boolean variables – bool isLong = (height > 195); – bool isWhite = (color == Color.White); 5

6 Fundamental Logical Operators OperatorNameExample &&AND(a > 0) && (a < 10) ||OR(a = 10) !NOT!(a < 100) 6

7 The ‘if’ Statement int height; Console.Write("Enter your height (in cm.) "); height = int.Parse(Console.ReadLine()); if (height > 190) Console.WriteLine("You are a tall person!"); else Console.WriteLine("Your height is normal!"); 7

8 if Statement if (expression) ; if (expression) { ; } 8

9 if..else if (expression) ; else ; If there are more statements, use curly brackets. 9

10 Some Notes on ‘if’ Parentheses are required, they can’t be omitted Curly braces (‘{’ and ‘}’)should be used if there are more than one statements: if (test) { statement1; statement2; } else part can be omitted if statements can be nested 10

11 Example: Finding the smallest of 3 integers int a, b, c, min; Console.WriteLine("Enter 3 integers:"); Console.Write("a = "); a = int.Parse(Console.ReadLine()); Console.Write("b = "); b = int.Parse(Console.ReadLine()); Console.Write("c = "); c = int.Parse(Console.ReadLine()); if (a < b) { if (a < c) min = a; else min = c; } else { if (b < c) min = b; else min = c; } Console.WriteLine("The smallest one is {0}.", min); 11

12 Checking Conditions if (var1 == 1) { // Do something. } else { if (var1 == 2) { // Do something else. } else { if (var1 == 3 || var1 == 4) { // Do something else. } else { // Do something else. } if (var1 == 1) { // Do something. } else if (var1 == 2) { // Do something else. } else if (var1 == 3 || var1 == 4) { // Do something else. } else { // Do something else. } 12

13 Common Mistakes if (var1 = 1) {…} if (var1 == 1 || 2) {…} 13

14 The ‘switch’ Statement switch ( ) { case : == > break; case : == > break;... case : == > break; default: != comparisonVals> break; } 14

15 Example 1 switch (var1) { case 1: // Do something. break; case 2: // Do something else. break; case 3: case 4: // Do something else. break; default: // Do something else. break; } 15

16 Example 2 switch (option) { case 1: Console.WriteLine(“You select 1”); break; case 2: Console.WriteLine(“You select 2”); break; case 3: Console.WriteLine(“You select 3”); break; default: Console.WriteLine(“Please select an integer between 1 and 3.”); break; } 16

17 switch Example switch (strProfession) { case "teacher": MessageBox.Show("You educate our young"); break; case "programmer": MessageBox.Show("You are most likely a geek"); break; case "accountant": MessageBox.Show("You are a bean counter"); break; default: MessageBox.Show("Profession not found in switch statement"); break; } 17 In C, only integer values can be used as the expression but in C#, strings can be used too. Don’t forget to use breaks!

18 Example 3 switch (strAnimal) { case “bird”: Console.WriteLine(“It has 2 legs.”); break; case “horse”: case “dog”: case “cat”: Console.WriteLine(“It has 4 legs.”); break; case “centipede”: Console.WriteLine(“It has 40 legs.”); break; case “snake”: Console.WriteLine(“It has no legs.”); break; default: Console.WriteLine(“I don’t know that animal!”); break; } 18

19 The Ternary Operator ? : Tertiary operator because it acts on 3 operands (remember unary and binary operators acting on 1 and 2 operands resp.) Example: – if (a < b) min = a; else min = b; – min = (a < b) ? a : b; 19

20 Looping 20

21 for Loop for (initializers; check_condition; modifying_expressions) { } Example: for (i = 0; i < 10; i++) { Console.WriteLine("i = " + i.ToString()); } 21

22 while Loop while (expression) { } 22

23 do-while Loop do { } while (expression); 23

24 foreach Loop foreach ( in ) { } 24

25 Displaying Months using for Loop string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; for (int i = 0; i < months.Length; i++) { MessageBox.Show(months[i]); } 25

26 Displaying Months using foreach string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; foreach (string month in months) { MessageBox.Show(month); } 26

27 Exercise Display first ten prime numbers. 27

28 Interrupting Loops break – ends the loop immediately continue – ends the current loop cycle return – jumps out of the function goto – jumps to the specified location (don’t use) 28

29 Infinite Loops while (true) { … } 29

30 Example int num; while (true) { Console.Write(“Enter a number between 1 and 100: ”); num = Convert.ToInt32(Console.ReadLine()); if (num >= 1 && num <= 100) break; else { Console.WriteLine(“It should be between 1 and 100.”); Console.WriteLine(“Please try again!\n”); } 30

31 Bitwise Operators & (Bitwise AND) | (Bitwise OR) ~ (Bitwise NOT) ^ (Bitwise XOR) 31

32 Examples 0110 & 0101 = 0100 (1&1=1, otherwise 0) 0110 | 0101 = 0111 (0|0=0, otherwise 1) 0110 ^ 0101 = 0011 (same  0, different  1) ~0110 = 1001 (0  1, 1  0) 32

33 Examples option = Location.Left | Location.Bottom; if (option & Location.Left != 0) MessageBox.Show(“Indented to left.”); if (option & Location.Bottom != 0) MessageBox.Show(“Indented to right.”); 33

34 Shift Operators >> (Shift right) << (Shift left) >>= <<= 34

35 Examples int a = 16; int b = a >> 2; // b becomes 4 int c = a << 4; // c becomes 256 a >>= 2; // a becomes 4 a <<= 4; // a becomes 64 35


Download ppt "BIM313 – Advanced Programming Techniques Flow Control 1."

Similar presentations


Ads by Google