Using Decision Statements Bool Data Type Conditional expressions Boolean operators if statements to perform decision-making based on the result of a Boolean expression switch statements bool areYouReady; areYouReady = true; Console.WriteLine(areYouReady); // writes True Using Boolean Operators (!) !false is true and !true is false bool validPercentage; validPercentage = (percent >= 0) && (percent <= 100); bool invalidPercentage; invalidPercentage = (percent < 0) ││ (percent > 100); Short Circuiting The && and ││ operators both exhibit a feature called short circuiting. Sometimes the result of the && and ││ operators can be determined solely from their left-side Boolean expression. In these cases, the && and ││ operators will bypass the evaluation of their right-side Boolean expressions. Here are some examples: (percent >= 0) && (percent <= 100) In this expression, if the value of percent is less than zero, the Boolean expression on the left side of && evaluates to false. This value means that the result of the entire expression must be false regardless of the remaining expression; therefore, the Boolean expression on the right side of && is not evaluated. (percent < 0) ││ (percent > 100) In this expression, if the value of percent is less than zero, the Boolean expression on the left side of ││ evaluates to true. This value means that the result of the entire expression must be true; therefore, the Boolean expression on the right side of ││ is not evaluated. Summarizing Operator Precedence and Associativity The following table summarizes the precedence and associativity of all the operators you have learned so far. Operators in the same category have the same precedence. Operators in a higher category take precedence over operators in a lower category. Category Primary Unary Operators Description () Precedence override ! NOT * Multiplicative / Associativity Left Left Multiply Divide Division remainder Left Addition Subtraction Left % + Additive < <= Relational > Less than Less than or equal Greater than Greater Left than or equal >= == Equality Equal to Not equal to Left Logical AND Logical OR Left != && Boolean Assignment ││ = Right Understanding if Statement Syntax if ( booleanExpression ) statement-1 else statement-2 int seconds; if (seconds == 59) seconds = 0; else seconds = seconds + 1; Boolean Expressions Only Please! The expression in an if statement must be enclosed in parentheses. Additionally, the expression must be a Boolean expression. Some other languages (notably C and C++) allow you to write an integer expression and silently convert the integer value to true (nonzero) or false (zero). C# never does this silent conversion. Also, if you're writing an if statement and you accidentally write an assignment instead of an equality test, the C# compiler will recognize your mistake, for example: int seconds; if (seconds = 59) // compile-time error if (seconds == 59) // ok Finally, you can use a Boolean variable as the expression, as in this example: bool inWord; if (inWord == true) // ok, but non-idiomatic if (inWord) // better Cascading if Statements if (day == 0) dayName = "Sunday"; else if (day == 1) dayName = "Monday"; else if (day == 2) dayName = "Tuesday"; else if (day == 3) dayName = "Wednesday"; else if (day == 4) dayName = "Thursday"; else if (day == 5) dayName = "Friday"; else if (day == 6) dayName = "Saturday"; else dayName = "unknown"; Ejemplo: if (day == 0) dayName = "Sunday"; else if (day == 1) dayName = "Monday"; else if (day == 2) dayName = "Tuesday"; else if (day == 3) else dayName = "Unknown"; Understanding switch Statement Syntax The syntax of a switch statement is as follows (switch, case, and default are keywords): switch ( controllingExpression ) { case constantExpression : statements break; case constantExpression : statements break; default : statements break; } Ejemplo: switch (day) { case 0 : dayName = "Sunday"; break; case 1 : dayName = "Monday"; break; case 2 : dayName = "Tuesday"; break; default : dayName = "Unknown"; break; } Following the switch Statement Rules The switch statements are very useful but, unfortunately, you can't always use them when you might like to. Your switch statements must adhere to the following rules: You can use switch only on primitive data types (such as int) and string. If you need to use switch on any other types, you'll have to use an if statement. Your case labels must be constant expressions, such as 42 or "42". If you need to calculate your case label values at run time, you'll have to use an if statement. Your case labels must be unique expressions. In other words, you're not allowed to write two case labels with the same value. switch (trumps) { case Hearts : case Diamonds: color = "Red"; break; case Clubs : case Spades : color = "Black"; break; } No Fall-Through Demostración de uso de: Groupbox Radiobottons Eventos Click y CheckedChanged Ejercicio de práctica: Crear una interface para calcular el costo de las comisetas dependiendo del tamano y del número de camisetas.