Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall 2012 1.

Similar presentations


Presentation on theme: "CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall 2012 1."— Presentation transcript:

1 CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall 2012 1

2 Objectives In this chapter, you will: – Learn more control structures Repetition statements – for, do … while Selection – switch – Know how to use break and continue statements to terminate the loop or current iteration – Learn how to use logical operators in C# 2

3 Repetition Structure Visual C# provides 3 repetition statements – while – for – do … while 3

4 In the Last Class: Counter Controlled while Loop – int Counter =1; – while (Counter <=10) – { … Counter ++; – }//does it 10 times 4

5 Example of for Repetition Statement Output even numbers between 2 and 10 for (int counter = 2; counter<= 10; counter+=2) { Console.Write("{0} ", counter); Console. WriteLine(); } 5

6 Discussions on for Repetition Statement for (int counter = 2; counter <= 10; counter += 2) – Keywords: for – "int counter" declares a counter of integer data type counter – control variable name int – control variable type – Initial value of control variable – 2 – Final value of control variable – 10 6

7 General Form of a for Statement For (initialization; loopContinuationCondition; increment) { statement } 7

8 Other Examples of for Statement (1) Declaring the control variable before a for statement – int counter; for (counter = 2; counter <= 10; counter+=2) Console.Write("{0} ", counter); Using expressions in the for statement – for (counter = x; counter <= 4*x*y; counter+=y/x) Console.Write("{0} ", counter); 8

9 Other Examples of for Statement (2) Increment: 1 for (counter = 2; counter <= 10; counter++) Decrement for (counter = 10; counter >= 2; counter-=2) 9

10 Example of Interest decimal amount; decimal principal = 1000; double rate = 0.05; Console.WriteLine("Year{0, 20}", "Amount on deposit"); for (int year = 1; year <= 10; year++) { amount = principal *((decimal)Math.Pow(1.0+rate, year)); } 10 field width

11 do…while Repetition Statements The loop body is always executed at least once int product = 1 do { product = product * 3; }while (product <=100); 11

12 Use break to Terminate Repetition Statements for (int counter = 1; counter <=10; counter ++) { if (counter == 5) break; Console.Write("{0} ", counter); } // answer: 1 2 3 4 12

13 Use continue to Terminate the Current Iteration for (int counter = 1; counter <=10; counter ++) { if (counter == 5) continue; Console.Write("{0} ", counter); } // answer: 1 2 3 4 6 7 8 9 10 13

14 In the Last Class: Selection Structures in C# if – single selection statement if … else – double selection statement switch – multiple selection statement 14 display "passed" [grade>=60] [grade<60]

15 Example of Multiple Selection Statement public void CalculateGrade(int grade) { switch (grade/10) { case 9: // 90-99 case 10: // 100 Console.WriteLine("Grade: A"); break; case 8: Console.WriteLine("Grade: B"); break; // 80-89 case 7: Console.WriteLine("Grade: C"); break; // 70-79 case 6: Console.WriteLine("Grade: D"); break; // 60-69 default: Console.WriteLine("Grade: F"); break; // < 60 } 15

16 Logical Operators Conditional AND operator (&&) – if (gender == 'F' && age >=65) – seniorFemales += 1; 16 Expression 1Expression 2Expression 1 && Expression 2 false truefalse truefalse true

17 Logical Operators (cont'd) Conditional OR operator (||) 17 Expression 1Expression 2Expression 1 || Expression 2 false true falsetrue

18 Short-Circuit Evaluation Conditional AND operator (&&) – if (gender == 'F' && age >=65) – If gender is not "F", then "age >=65" will not be evaluated Conditional OR operator (||) – if (gender == 'F' || age >=65) – If gender is "F", then "age >=65" will not be evalutated 18

19 Boolean Logical Operators Boolean Logical AND ( & ) – if (gender == 'F' & age >=65) – No matter what value gender has, “age>=65” will always be evaluated Boolean Logical OR ( | ) Logical Negative ( ! ) 19

20 Other Boolean Logical Operators Boolean Logical Exclusive OR (^) – If both conditions are the same, then output is false 20 Expression 1Expression 2Expression 1 ^ Expression 2 False True FalseTrue False


Download ppt "CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall 2012 1."

Similar presentations


Ads by Google