Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Visula C# Programming Lecture 3

800 views

Published on

lecture 3

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

Visula C# Programming Lecture 3

  1. 1. Lecture #03: Assignment Operator and Simple Control Structures
  2. 2. Assignment Operators Assignment operator Sample expression Explanation += -= *= /= %= c d e f g c d e f g += -= *= /= %= 7 4 5 3 2 = = = = = c d e f g + * / % 7 4 5 3 2 count = count + 1; // these two are equivalent count ++; count = count – 1; // these two are equivalent count --; 2
  3. 3. Precedence and Associativity Operators high low Associativity Type () ++ -++ -- + - (type) * / % + < <= > >= == != ?: = += -= *= /= %= left to right right to left parentheses unary postfix right to left unary prefix left to right multiplicative left to right additive left to right relational left to right equality right to left conditional right to left assignment 3
  4. 4. Sequential Programming Revisited 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 // Addition.cs // An addition program. using System; class Addition { static void Main( string[] args ) { string firstNumber, // first string entered by user secondNumber; // second string entered by user int number1, number2, sum; // first number to add // second number to add // sum of number1 and number2 // prompt for and read first number from user as string Console.Write( "Please enter the first integer: " ); firstNumber = Console.ReadLine(); 17 // prompt for and read first number from user as string 18 Console.Write( "Please enter the first integer: " ); 19 firstNumber = Console.ReadLine(); 20 21 // read second number from user as string 22 Console.Write( "nPlease enter the second integer: " ); 23 secondNumber = Console.ReadLine(); 24 25 // convert numbers from type string to type int // read second number from user as string Console.Write( "nPlease enter the second integer: " ); secondNumber = Console.ReadLine(); 26 number1 = Int32.Parse( firstNumber ); // convert numbers from type string to type int number1 = Int32.Parse( firstNumber ); number2 = Int32.Parse( secondNumber ); 27 number2 = Int32.Parse( secondNumber ); 28 // add numbers sum = number1 + number2; // display results Console.WriteLine( "nThe sum is {0}.", sum ); } // end method Main } // end class Addition 29 // add numbers 30 sum = number1 + number2; 31 32 // display results 33 Console.WriteLine( "nThe sum is {0}.", sum ); 4
  5. 5. Sequence Structure (Flowchart) Each of these statements could be: • a variable declaration • an assignment statement . . • a method call (e.g., Console.WriteLine( …); ) or more complex statements (to be covered later) 5
  6. 6. More Interesting: Control Statements Selection (a.k.a., conditional statements): decide whether or not to execute a particular statement; these are also called the selection statements or decision statements • if selection (one choice) • if/else selection (two choices) – Also: the ternary conditional operator e1?e2:e3 • switch statement (multiple choices) Repetition (a.k.a., loop statements): repeatedly executing the same statements (for a certain number of times or until a test condition is satisfied). • while structure • do/while structure • for structure • foreach structure 6
  7. 7. Why Control Statements ? Last few classes: a sequence of statements Sequential programming Most programs need more flexibility in the order of statement execution The order of statement execution is called the flow of control 7
  8. 8. Pseudocode & Flowcharts to Represent Flow of Control Pseudocode Artificial and informal language Helps programmers to plan an algorithm Similar to everyday English Not an actual programming language Flowchart --- a graphical way of writing pseudocode Rectangle used to show action Circles used as connectors Diamonds used as decisions 8
  9. 9. Sequence Structure add grade to total total = total + grade; add 1 to counter counter = counter + 1; 9
  10. 10. C# Control Structures: Selection switch structure (multiple selections) if/else structure (double selection) T F T break F T break F . . if structure (single selection) T . T break F F break 10
  11. 11. if Statement if ( <test> ) <code executed if <test> is true > ; The if statement Causes the program to make a selection Chooses based on conditional • <test>: any expression that evaluates to a bool type • True: perform an action • False: skip the action Single entry/exit point No semicolon after the condition 11
  12. 12. if Statement (cont’d) if ( <test> ) { <code executed if <test> is true > ; …… <more code executed if <test> is true > ; } The body of the branch can also be a block statement! No semicolon after the condition No semicolon after the block statement 12
  13. 13. if Statement (cont’d) true Grade >= 60 print “Passed” false if (studentGrade >= 60) Console.WriteLine (“Passed”); // beginning of the next statement 13
  14. 14. if Statement (cont’d) true Grade >= 60 print “Passed” false if (studentGrade >= 60) { Console.WriteLine (“Passed”); } // beginning of the next statement 14
  15. 15. if/else Statement if ( <test> ) <code executed if <test> is true > ; else <code executed if <test> is false > ; The if/else structure Alternate courses can be taken when the statement is false Rather than one action there are two choices Nested structures can test many cases 15
  16. 16. if/else Statement (cont’d) if ( <test> ) { <code executed if <test> is true > ; …… } else { <code executed if <test> is false > ; …… } Can use the block statement inside either branch 16
  17. 17. if/else Statement (cont’d) false true Grade >= 60 print “Failed” print “Passed” if (studentGrade >= 60) Console.WriteLine (“Passed”); else Console.WriteLine (“Failed”); // beginning of the next statement 17
  18. 18. if/else Statement (cont’d) false true Grade >= 60 print “Failed” print “Passed” if (studentGrade >= 60) { Console.WriteLine (“Passed”); } else Console.WriteLine (“Failed”); // beginning of the next statement 18
  19. 19. Nested if/else Statements The statement executed as a result of an if statement or else clause could be another if statement These are called nested if /else statements if (studentGrade >= 90) Console.WriteLine(“A”); else if (studentGrade >= 80) Console.WriteLine(“B”); else if (studentGrade >= 70) Console.WriteLine(“C”); else if (studentGrade >= 60) Console.WriteLine(“D”); else Console.WriteLine(“F”); // beginning of the next statement 19
  20. 20. Ternary Conditional Operator (?:) Conditional Operator (e 1 ?e 2 :e 3 ) C#’s only ternary operator Can be used to construct expressions Similar to an if/else structure string result; int numQ; ………… result = (numQ==1) ? “Quarter” : “Quarters”; // beginning of the next statement 20
  21. 21. More Complex (Compound) Boolean Expressions: Logical Operators Boolean expressions can also use the following logical and conditional operators: ! & | ^ && || Logical NOT Logical AND Logical OR Logical exclusive OR (XOR) Conditional AND Conditional OR They all take boolean operands and produce boolean results 21
  22. 22. Comparison: Logical and Conditional Operators Logical AND (&) and Logical OR (|) • Always evaluate both conditions Conditional AND (&&) and Conditional OR (||) • Would not evaluate the second condition if the result of the first condition would already decide the final outcome. • Ex 1: false && (x++ > 10) --- no need to evaluate the 2nd condition • Ex 2: if (count != 0 && total /count) { … } 22
  23. 23. Switch Statement int a=3; switch (a) { case 0: Console.Write("zero"); break; case 1: Console.Write("One"); break; ……. default: Console.Write ("False statement"); break; } 23

×