bim313 – advanced programming techniques flow control 1
TRANSCRIPT
BIM313 – Advanced Programming Techniques
Flow Control
1
Contents
• Flow Control– if, switch– while, do-while, for, foreach– Binary operators
2
Flow Control
• Branching (if, switch, ternary operator)• Looping (for, while, do-while, foreach)
3
Comparison OperatorsOperator 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 isWhite = true;– isWhite = false;
• Comparison results can be stored in Boolean variables– bool isLong = (height > 195);– bool isWhite = (color == Color.White);
5
Fundamental Logical OperatorsOperator 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.ReadLine());if (height > 190) Console.WriteLine("You are a tall person!");else Console.WriteLine("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){ statement1; statement2;}
• else part can be omitted• if statements can be nested
10
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
Checking Conditionsif (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
Common Mistakes
• if (var1 = 1) {…}• if (var1 == 1 || 2) {…}
13
The ‘switch’ Statementswitch (<testVar>){ case <comparisonVal1>: <code to execute if <testVar> == <comparisonVal1> > break; case <comparisonVal2>: <code to execute if <testVar> == <comparisonVal2> > break; . . . case <comparisonValN>: <code to execute if <testVar> == <comparisonValN> > break; default: <code to execute if <testVar> != comparisonVals> break;}
14
Example 1switch (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
Example 2switch (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
switch Exampleswitch (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!
Example 3switch (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
The Ternary Operator
• ? :• <test> ? <resultIfTrue> : <resultIfFalse>• 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.WriteLine("i = " + i.ToString());}
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++){ MessageBox.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){ MessageBox.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
Exampleint 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
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 (same0, different1)• ~0110 = 1001 (01, 10)
32
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
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