bim313 – advanced programming techniques flow control 1

35
BIM313 – Advanced Programming Techniques Flow Control 1

Upload: aron-simmons

Post on 31-Dec-2015

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BIM313 – Advanced Programming Techniques Flow Control 1

BIM313 – Advanced Programming Techniques

Flow Control

1

Page 2: BIM313 – Advanced Programming Techniques Flow Control 1

Contents

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

2

Page 3: BIM313 – Advanced Programming Techniques Flow Control 1

Flow Control

• Branching (if, switch, ternary operator)• Looping (for, while, do-while, foreach)

3

Page 4: BIM313 – Advanced Programming Techniques Flow Control 1

Comparison OperatorsOperator Meaning

== equal to

!= not equal to

< less than

> greater than

<= less than or equal to

>= greater than or equal to

4

Page 5: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 6: BIM313 – Advanced Programming Techniques Flow Control 1

Fundamental Logical OperatorsOperator Name Example

&& AND (a > 0) && (a < 10)

|| OR (a <= 0) || (a >= 10)

! NOT !(a < 100)

6

Page 7: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 8: BIM313 – Advanced Programming Techniques Flow Control 1

if Statement

if (expression) <statement to execute when expression is true>;

if (expression){ <statement 1>; <statement 2>;}

8

Page 9: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 10: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 11: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 12: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 13: BIM313 – Advanced Programming Techniques Flow Control 1

Common Mistakes

• if (var1 = 1) {…}• if (var1 == 1 || 2) {…}

13

Page 14: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 15: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 16: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 17: BIM313 – Advanced Programming Techniques Flow Control 1

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!

Page 18: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 19: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 20: BIM313 – Advanced Programming Techniques Flow Control 1

Looping

20

Page 21: BIM313 – Advanced Programming Techniques Flow Control 1

for Loop

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

21

Page 22: BIM313 – Advanced Programming Techniques Flow Control 1

while Loop

while (expression){ <statements>}

22

Page 23: BIM313 – Advanced Programming Techniques Flow Control 1

do-while Loop

do{ <statements>} while (expression);

23

Page 24: BIM313 – Advanced Programming Techniques Flow Control 1

foreach Loop

foreach (<type> <name> in <list>){ <statements>}

24

Page 25: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 26: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 27: BIM313 – Advanced Programming Techniques Flow Control 1

Exercise

• Display first ten prime numbers.

27

Page 28: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 29: BIM313 – Advanced Programming Techniques Flow Control 1

Infinite Loops

while (true){ …}

29

Page 30: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 31: BIM313 – Advanced Programming Techniques Flow Control 1

Bitwise Operators

• & (Bitwise AND)• | (Bitwise OR)• ~ (Bitwise NOT)• ^ (Bitwise XOR)

31

Page 32: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 33: BIM313 – Advanced Programming Techniques Flow Control 1

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

Page 34: BIM313 – Advanced Programming Techniques Flow Control 1

Shift Operators

• >> (Shift right)• << (Shift left)• >>=• <<=

34

Page 35: BIM313 – Advanced Programming Techniques Flow Control 1

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