BIM 313 Advanced Programming Techniques Flow Control 1
BIM 313 – Advanced Programming Techniques Flow Control 1
Contents • Flow Control – if, switch – while, do-while, foreach – Binary operators 2
Flow Control • Branching (if, switch, ternary operator) • Looping (for, while, do-while, foreach) 3
Comparison Operators Operator Meaning == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to 4
Boolean Variables • A Boolean variable may take values true or false – bool is. White = true; – is. White = false; • Comparison results can be stored in Boolean variables – bool is. Long = (height > 195); – bool is. White = (color == Color. White); 5
Fundamental Logical Operators Operator Name Example && AND (a > 0) && (a < 10) || OR (a <= 0) || (a >= 10) ! NOT !(a < 100) 6
The ‘if’ Statement int height; Console. Write("Enter your height (in cm. ) "); height = int. Parse(Console. Read. Line()); if (height > 190) Console. Write. Line("You are a tall person!"); else Console. Write. Line("Your height is normal!"); 7
if Statement if (expression) <statement to execute when expression is true>; if (expression) { <statement 1>; <statement 2>; } 8
if. . else if (expression) <statement to execute when expression is true>; else <statement to execute when expression is false>; • If there are more statements, use curly brackets. 9
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) { statement 1; statement 2; } • else part can be omitted • if statements can be nested 10
Example: Finding the smallest of 3 integers int a, b, c, min; Console. Write. Line("Enter 3 integers: "); Console. Write("a = "); a = int. Parse(Console. Read. Line()); Console. Write("b = "); b = int. Parse(Console. Read. Line()); Console. Write("c = "); c = int. Parse(Console. Read. Line()); if (a < b) { if (a < c) min = a; else min = c; } else { if (b < c) min = b; else min = c; } Console. Write. Line("The smallest one is {0}. ", min); 11
Checking Conditions if (var 1 == 1) { // Do something. } else { if (var 1 == 2) { // Do something else. } else { if (var 1 == 3 || var 1 == 4) { // Do something else. } else { // Do something else. } } } if (var 1 == 1) { // Do something. } else if (var 1 == 2) { // Do something else. } else if (var 1 == 3 || var 1 == 4) { // Do something else. } else { // Do something else. } 12
Common Mistakes • if (var 1 = 1) {…} • if (var 1 == 1 || 2) {…} 13
The ‘switch’ Statement switch (<test. Var>) { case <comparison. Val 1>: <code to execute if <test. Var> == <comparison. Val 1> > break; case <comparison. Val 2>: <code to execute if <test. Var> == <comparison. Val 2> > break; . . . case <comparison. Val. N>: <code to execute if <test. Var> == <comparison. Val. N> > break; default: <code to execute if <test. Var> != comparison. Vals> break; } 14
Example 1 switch (var 1) { 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
Example 2 switch (option) { case 1: Console. Write. Line(“You select 1”); break; case 2: Console. Write. Line(“You select 2”); break; case 3: Console. Write. Line(“You select 3”); break; default: Console. Write. Line(“Please select an integer between 1 and 3. ”); break; } 16
switch Example switch (str. Profession) In C, only integer { case "teacher": values can be used as Message. Box. Show("You educate our young"); the expression but in break; C#, strings can be case "programmer": Message. Box. Show("You are most likely a geek"); used too. break; case "accountant": Message. Box. Show("You are a bean counter"); break; default: Message. Box. Show("Profession not found in switch statement"); break; } Don’t forget to use breaks! 17
Example 3 switch (str. Animal) { case “bird”: Console. Write. Line(“It has 2 legs. ”); break; case “horse”: case “dog”: case “cat”: Console. Write. Line(“It has 4 legs. ”); break; case “centipede”: Console. Write. Line(“It has 40 legs. ”); break; case “snake”: Console. Write. Line(“It has no legs. ”); break; default: Console. Write. Line(“I don’t know that animal!”); break; } 18
The Ternary Operator • ? : • <test> ? <result. If. True> : <result. If. False> • 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
Looping 20
for Loop for (initializers; check_condition; modifying_expressions) { <statements> } • Example: for (i = 0; i < 10; i++) { Console. Write. Line("i = " + i. To. String()); } 21
while Loop while (expression) { <statements> } 22
do-while Loop do { <statements> } while (expression); 23
foreach Loop foreach (<type> <name> in <list>) { <statements> } 24
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++) { Message. Box. Show(months[i]); } 25
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) { Message. Box. Show(month); } 26
Exercise • Display first ten prime numbers. 27
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
Infinite Loops while (true) { … } 29
Example int num; while (true) { Console. Write(“Enter a number between 1 and 100: ”); num = Convert. To. Int 32(Console. Read. Line()); if (num >= 1 && num <= 100) break; else { Console. Write. Line(“It should be between 1 and 100. ”); Console. Write. Line(“Please try again!n”); } } 30
Bitwise Operators • • & (Bitwise AND) | (Bitwise OR) ~ (Bitwise NOT) ^ (Bitwise XOR) 31
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
Examples option = Location. Left | Location. Bottom; if (option & Location. Left != 0) Message. Box. Show(“Indented to left. ”); if (option & Location. Bottom != 0) Message. Box. Show(“Indented to right. ”); 33
Shift Operators • • >> (Shift right) << (Shift left) >>= <<= 34
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
- Slides: 35