conditional statements 1 variable scope
play

Conditional statements (1) +variable scope Based on slides by - PowerPoint PPT Presentation

Conditional statements (1) +variable scope Based on slides by Asst. Prof. Kitsana Waiyamai, Ph.D. Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand http://kdl.cpe.ku.ac.th If-Then-Else Statement 1


  1. Conditional statements (1) +variable scope Based on slides by Asst. Prof. Kitsana Waiyamai, Ph.D. Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand http://kdl.cpe.ku.ac.th If-Then-Else Statement 1

  2. Conditional statement � In the previous lessons, all the statements in the programs are executed sequentially without skipping � Conditional statement � Now, some statements may be executed only if certain conditions are satisfied, otherwise, the statements will be skipped number = int.Parse(Console.ReadLine()); if(number>=0) Console.WriteLine("The number is positive"); else Console.WriteLine("The number is negative"); 2 If-Then-Else Statement

  3. Data type: bool � bool � A kind of logical data type with value either true or false True Console.WriteLine(2+3 == 5); False Console.WriteLine(3*4 == 7*4/2); Console.WriteLine(100>90); True Console.WriteLine("Pascal" == "Pascal"); True 3 If-Then-Else Statement

  4. Boolean Expression � Either true or false depending on the result of the boolean expression evaluation � Boolean expression consists of two operands joined by a relational operator � What can the operands be? � Arithmetic expressions, e.g., 1+1*2/3 � Characters, e.g. ' a' == 'b' � Strings, e.g. " Pascal" == "Pascal" � Variables 4 If-Then-Else Statement

  5. Boolean Expression - Relational Operators � They are used to compare data items of the compatible type Relational Operator in Pascal Meaning == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to 5 If-Then-Else Statement

  6. Boolean Expression - Boolean Constants and Boolean Variable � Boolean constants and Boolean variables are of type bool static void Main(string[] args) { const bool sunRiseFromEast = true; const bool oneGreaterThanTwo = false; bool boy; Console.WriteLine("The sun rises from the east is " + sunRiseFromEast); boy = false; Console.WriteLine("It is " + boy + " that Mary is a boy"); } 6 If-Then-Else Statement

  7. Boolean Expression - Boolean Expressions with Operands of Arithmetic Expressions Boolean Expression Value 2.5 == 5 false 4%10 > 1-3 true 5.7 >= -2 true 5/2 <= 1.1*2 false 999!=999.9 false 7 If-Then-Else Statement

  8. Boolean Expression - Boolean Expressions with Character Operands � ASCII – American Standard Code for Information Interchange � Contains 128 characters 8 If-Then-Else Statement

  9. Boolean Expression - Boolean Expressions with Character Operands � Characters are compared using their ASCII code values. ASCII of First ASCII of Second Boolean Expression Result Operand Operand ’\’ != ’j’ true 92 106 ’9’ > ’4’ true 57 52 ’{’ >= ’}’ 123 125 false 9 If-Then-Else Statement

  10. Boolean Expression - Boolean Expressions with String Operands � Strings are compared character by character from left to right Boolean Expression Result Remark "Z" > "Other" true ‘Z’ has larger ASCII value then ‘O’ "small" > "smaller" false ‘small’ does not have the sixth character "  Space" < "NoSpace" The character ‘  true ’ has smaller ASCII value than the character ‘N’ "Timmy" >= "Tommy" false The character ‘i’ of the first operand has smaller ASCII value then ‘o’ of the second operand 10 If-Then-Else Statement

  11. Compound Boolean Expressions � Simple Boolean Expressions may be joined by logical/conditional operators to form compound Boolean expressions � Conditional Operators � && - conditional AND � || - conditional OR � ^ - logical exclusive OR (logical XOR) � ! - logical NOT � Examples � (age >= 18) && (age <= 20) � (grade == 'F') || (grade == 'W') 11 If-Then-Else Statement

  12. Compound Boolean Expressions - Conditional Operators � Truth Table for conditional AND operator (&&) Boolean expression P Boolean expression Q P && Q TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE � Truth Table for conditional OR operator (||) Boolean expression P Boolean expression Q P || Q TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE 12 If-Then-Else Statement

  13. Compound Boolean Expressions - Conditional Operators � Truth Table for logical exclusive OR operator Boolean expression P Boolean expression Q P ^ Q TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE � Truth table for logical NOT operator Boolean Expression P ! P TRUE FALSE FALSE TRUE 13 If-Then-Else Statement

  14. Compound Boolean Expressions - Logical Operators � Logical AND (&) � Logical OR ( | ) � Similar to conditional AND (&&) and conditional OR ( || ) operators in terms of truth tables and associativity (left to right). 14 If-Then-Else Statement

  15. Difference between conditional and logical operators � conditional AND (&&) and conditional OR ( || ) stops evaluating if the leftmost condition is either false or true, respectively. � can reduce execution time � logical AND (&) and logical OR ( | ) always evaluate both sides of operands 15 If-Then-Else Statement

  16. Precedence and associativity of operators Operators Associativity Type () left to right parentheses ++ -- right to left unary postfix ++ -- + - ! (type) right to left unary prefix * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality & left to right logical AND ^ left to right logical XOR | left to right logical OR && left to right conditional AND || left to right conditional OR ?: right to left conditional = += -= *= /= %= right to left assignment 16 If-Then-Else Statement

  17. Boolean Expression Evaluation !('F'!='M')||(19 > 12)&&(19 < 18) !( true )||(19 > 12)&&(19 < 18) ( false )||(19 > 12)&&(19 < 18) ( false )||( true )&&(19 < 18) ( false )||( true )&&( false ) ( false ) || ( false ) ( false ) 17 If-Then-Else Statement

  18. Evaluation: Short-cutting (1) !('F'=='M')||(19 > 12)&&(19 < 18) !( false )||(19 > 12)&&(19 < 18) ( true )||(19 > 12)&&(19 < 18) (true) || ?? true ( true ) 18 If-Then-Else Statement

  19. Evaluation: Short-cutting (2) !('F‘!='M')||(19 < 12)&&(19 > 18) !( true )||(19 < 12)&&(19 > 18) ( false )||(19 < 12)&&(19 > 18) ( false )||( false )&&(19 > 18) ( false )||( false ) ( false ) 19 If-Then-Else Statement

  20. Conditional Statements � Two kinds of conditional statements in C# are if statements and switch statements � If statements � If-else statements � Nested if statements � Switch statements 20 If-Then-Else Statement

  21. Flowcharts � Graphical representation Terminator Process Input/output Condition Connector Flow line 21 If-Then-Else Statement

  22. Conditional Statements - If-then statement � Example � If a student has score higher than 90, then print he is “a very good student,” otherwise, do something else False Score > 90 True ‘very good student’ 22 If-Then-Else Statement

  23. If statement � Syntax : if (< boolean value >) statement ; � statement is executed, if the boolean value is true. � If more than one statements are to be executed when the boolean value is true, we need to group them with { }. e.g., if (< boolean value >) { statement1 ; statement2 ; } 23 If-Then-Else Statement

  24. IF class MainClass { static void Main() { int num; Console.Write("Please input a number :"); num = int.Parse(Console.ReadLine()); if(num < 0) Console.WriteLine("negative"); if(num == 0) Console.WriteLine("zero"); if(num > 0) Console.WriteLine("positive"); } } 24 If-Then-Else Statement

  25. Conditional Statements - If-then-else statement � If-then-else statement � When there is only one of two alternative statements to be executed � Example False True Mark >= 50 � If Mark variable is greater Output ‘fail’ Output ‘pass’ than or equal to 50, output ‘you pass the exam’ , � otherwise, output ‘you fail the exam’ 25 If-Then-Else Statement

  26. IF – ELSE if( condition ) statement1 ; condition True False else statement2 ; Statement1 Statement2 26 If-Then-Else Statement

  27. Conditional Statements - If-then-else statement � When the condition is true, the statement after the condition will be executed; otherwise, the statement after the reserved word else will be executed class PassOrFail { static void Main() { Console.Write("Enter mark: "); int mark = int.Parse(Console.ReadLine()); if(mark >= 50) Console.WriteLine("you pass the exam"); else Console.WriteLine("you fail the exam"); } } 27 If-Then-Else Statement

Recommend


More recommend