Conditional Statements - 2019-01-05 آ  The conditional statements if and if-else provide conditional

  • View
    0

  • Download
    0

Embed Size (px)

Text of Conditional Statements - 2019-01-05 آ  The conditional statements if and if-else provide...

  • Conditional StatementsConditional Statements Implementing Control Logic in C#Implementing Control Logic in C#

    Svetlin NakovSvetlin Nakov Telerik CorporationTelerik Corporation www.telerik.comwww.telerik.com

    http://www.telerik.com/

  • Table of ContentsTable of Contents

    1.1. Comparison and Logical OperatorsComparison and Logical Operators

    2.2. The The ifif Statement Statement

    3.3. The The if-elseif-else Statement Statement

    4.4. Nested Nested ifif Statements Statements

    5.5. The The switch-caseswitch-case Statement Statement

    2

  • Comparison and Comparison and Logical OperatorsLogical Operators

  • Comparison OperatorsComparison Operators

    4

    OperatorOperator Notation in C#Notation in C#

    EqualsEquals ====

    Not EqualsNot Equals !=!=

    Greater ThanGreater Than >>

    Greater Than or EqualsGreater Than or Equals >=>=

    Less ThanLess Than

  • Logical OperatorsLogical Operators

     De Morgan lawsDe Morgan laws

     !!A !!A  A A

     !(A || B) !(A || B)  !A && !B !A && !B

     !(A && B) !(A && B)  !A || !B !A || !B

    OperatorOperator Notation in C#Notation in C#

    Logical NOTLogical NOT !!

    Logical ANDLogical AND &&&&

    Logical ORLogical OR ||||

    Logical Exclusive OR (XOR)Logical Exclusive OR (XOR) ^̂

    5

  • ifif and and if-elseif-else Implementing Conditional LogicImplementing Conditional Logic

  • The The ifif Statement Statement  The most simple conditional statementThe most simple conditional statement  Enables you to test for a conditionEnables you to test for a condition  Branch to different parts of the code Branch to different parts of the code

    depending on the resultdepending on the result  The simplest form of an The simplest form of an ifif statement: statement:

    if (condition) if (condition) {{ statements;statements; }}

    7

  • Condition and StatementCondition and Statement  The condition can be:The condition can be:

     Boolean variableBoolean variable

     Boolean logical expressionBoolean logical expression

     Comparison expressionComparison expression  The condition cannot be integer variable (like The condition cannot be integer variable (like

    in C / C++)in C / C++)  The statement can be:The statement can be:

     Single statement ending with a semicolonSingle statement ending with a semicolon

     Block enclosed in bracesBlock enclosed in braces 8

  • How It Works?How It Works?

     The condition is evaluatedThe condition is evaluated

     If it is true, the statement is executedIf it is true, the statement is executed

     If it is false, the statement is skipped If it is false, the statement is skipped

    truetrue

    conditioncondition

    statementstatement

    falsefalse

    9

  • The The ifif Statement – Example Statement – Example

    10

    static void Main()static void Main() {{ Console.WriteLine("Enter two numbers.");Console.WriteLine("Enter two numbers.");

    int biggerNumber = int.Parse(Console.ReadLine());int biggerNumber = int.Parse(Console.ReadLine()); int smallerNumber = int.Parse(Console.ReadLine());int smallerNumber = int.Parse(Console.ReadLine());

    if (smallerNumber > biggerNumber)if (smallerNumber > biggerNumber) {{ biggerNumber = smallerNumber;biggerNumber = smallerNumber; }}

    Console.WriteLine("The greater number is: {0}",Console.WriteLine("The greater number is: {0}", biggerNumber);biggerNumber); }}

  • The The ifif Statement Statement Live DemoLive Demo

  • The The if-elseif-else StatementStatement

     More complex and useful conditional statementMore complex and useful conditional statement

     Executes one branch if the condition is true, and Executes one branch if the condition is true, and another if it is false another if it is false

     The simplest form of an The simplest form of an if-elseif-else statement: statement:

    if (expression) if (expression) {{ statement1; statement1; }} else else {{ statement2; statement2; }}

    12

  • How It Works ?How It Works ?

     The condition is evaluatedThe condition is evaluated

     If it is true, the first statement is executedIf it is true, the first statement is executed

     If it is false, the second statement is executedIf it is false, the second statement is executed

    conditioncondition

    firstfirst statementstatement

    truetrue

    secondsecond statementstatement

    falsefalse

    13

  • if-elseif-else Statement – ExampleStatement – Example  Checking a number if it is odd or evenChecking a number if it is odd or even

    string s = Console.ReadLine();string s = Console.ReadLine(); int number = int.Parse(s);int number = int.Parse(s);

    if (number % 2 == 0)if (number % 2 == 0) {{ Console.WriteLine("This number is even.");Console.WriteLine("This number is even."); }} elseelse {{ Console.WriteLine("This number is odd.");Console.WriteLine("This number is odd."); }}

    14

  • The The if-elseif-else Statement Statement Live DemoLive Demo

  • Nested Nested ifif Statements Statements Creating More Complex LogicCreating More Complex Logic

  • Nested Nested ifif Statements Statements  ifif and and if-elseif-else statements can be statements can be nestednested, i.e. used , i.e. used

    inside another inside another ifif or or elseelse statementstatement

     Every Every elseelse corresponds to its closest preceding corresponds to its closest preceding ifif

    if (expression) if (expression) {{ if (expression) if (expression) {{ statement;statement; }} else else {{ statement;statement; }} }} elseelse statement; statement;

    17

  • Nested Nested ifif – Good Practices – Good Practices

     Always use Always use {{ …… }} blocks to avoid ambiguity blocks to avoid ambiguity  Even when a single statement followsEven when a single statement follows

     Avoid using more than three levels of nested Avoid using more than three levels of nested ifif statementsstatements

     Put the case you normally expect to process Put the case you normally expect to process first, then write the unusual casesfirst, then write the unusual cases

     Arrange the code to make it more readableArrange the code to make it more readable

    18

  • Nested Nested ifif Statements – Example Statements – Example if (first == second)if (first == second) {{ Console.WriteLine(Console.WriteLine( "These two numbers are equal.");"These two numbers are equal."); }} elseelse {{ if (first > second)if (first > second) {{ Console.WriteLine(Console.WriteLine( "The first number is bigger.");"The first number is bigger."); }} elseelse {{ Console.WriteLine("The second is bigger.");Console.WriteLine("The second is bigger."); }} }}

    19

  • Nested Nested ifif StatementsStatements

    Live DemoLive Demo

  • Multiple if-else-if-else-…Multiple if-else-if-else-…  Sometimes we need to use another Sometimes we need to use another ifif--

    construction in the construction in the elseelse block block

     Thus Thus else ifelse if can be used: can be used:

    21

    int ch = 'X';int ch = 'X';

    if (ch == 'A' || ch == 'a')if (ch == 'A' || ch == 'a')

    {{

    Console.WriteLine("Vowel [ei]");Console.WriteLine("Vowel [ei]");

    }}

    else if (ch == 'E' || ch == 'e')else if (ch == 'E' || ch == 'e')

    {{

    Console.WriteLine("Vowel [i:]");Console.WriteLine("Vowel [i:]");

    }}

    else if …else if …

    else …else …

  • Multiple Multiple if-elseif-else StatementsStatements

    Live DemoLive Demo

  • switch-caseswitch-case Making Several Comparisons at OnceMaking Several Comparisons at Once

  • The The switch-caseswitch-case Statement Statement  Selects for execution a statement from a list Selects for execution a statement from a list

    depending on the value of the depending on the value of the switchswitch expression expression

    switch (day)switch (day) {{

    case 1: Console.WriteLine("Monday"); break;case 1: Console.WriteLine("Monday"); break; case 2: Console.WriteLine("Tuesday"); break;case 2: Console.WriteLine("Tuesday"); break; case 3: Console.WriteLine("Wednesday"); break;case 3: Console.WriteLine("Wednesday"); break; case 4: Console.WriteLine("Thursday"); break;case 4: Console.WriteLine("Thursday"); break; case 5: Console.WriteLine("Friday"); break;case 5: Console.WriteLine("Friday"); break; case 6: Console.WriteLine("Saturday"); break;case 6: Console.WriteLine("Saturday"); break; case 7: Console.WriteLine("Sunday"); break;case 7: Console.WriteLine("Sunday"); break; default: Console.WriteLine("Error!"); break;default: Console.WriteLine("Error!"); break;

    }}

    24