Code Snippets (Conditional Statements)

Embed Size (px)

Citation preview

  • 8/3/2019 Code Snippets (Conditional Statements)

    1/36

    Conditional StatementsConditional Statements

    Prepared by: Eisen SyPrepared by: Eisen Sy

  • 8/3/2019 Code Snippets (Conditional Statements)

    2/36

    Conditional StatementConditional Statement

    Conditional statements allow yourConditional statements allow yourprogram to make a decision based on aprogram to make a decision based on a

    setofconditions.setofconditions. Conditional statements in C# may beConditional statements in C# may be

    implemented using theimplemented using the IfIfstatementstatement..

    Conditional statements have variousConditional statements have variousstructures which we will study in the nextstructures which we will study in the nextslide.slide.

  • 8/3/2019 Code Snippets (Conditional Statements)

    3/36

    Conditional Statement StructuresConditional Statement Structures

    IfstatementIfstatement IfIf Else statementElse statement IfIf Else IfstatementElse Ifstatement Switch statementSwitch statement

  • 8/3/2019 Code Snippets (Conditional Statements)

    4/36

    IfStatementIfStatement

    The first type ofThe first type ofifstatementifstatementallows our programallows our programto perform a setof instructions ifa certainto perform a setof instructions ifa certain

    condition iscondition is truetrue.. This type ofThis type ofifstatementifstatementsimply proceeds to thesimply proceeds to the

    next instruction should the condition of thenext instruction should the condition of the ififstatementstatementturned out to beturned out to be falsefalse..

    This is the most basic form ofThis is the most basic form ofifstatementifstatement..

  • 8/3/2019 Code Snippets (Conditional Statements)

    5/36

    IfStatementIfStatement

    Syntax:Syntax:

    if ( Boolean Condition )if ( Boolean Condition )

    {{

    True embeddedTrue embedded--statements;statements;

    }}

    Note: Using the curly brackets allowus towriteNote: Using the curly brackets allowus towritemore than 1 statementof instructions.more than 1 statementof instructions.

  • 8/3/2019 Code Snippets (Conditional Statements)

    6/36

    IfStatement TipIfStatement Tip

    Syntax:Syntax:

    if ( Boolean Condition )if ( Boolean Condition )

    {{

    True embeddedTrue embedded--statements;statements;

    }}

    If Statementmust not end witha semicolon ( ; )

    Embedded-Statementhowever, must end witha semicolon ( ; )

  • 8/3/2019 Code Snippets (Conditional Statements)

    7/36

    More IfStatement TipMore IfStatement Tip

    Syntax:Syntax:

    if ( Boolean Condition )if ( Boolean Condition )

    {{

    True embeddedTrue embedded--statements;statements;

    }}

    Indentions is not required but is considered a goodprogramming habit to promote code readability.

  • 8/3/2019 Code Snippets (Conditional Statements)

    8/36

    IfStatement ExampleIfStatement Example

    Code:Code:string password;string password;

    Console.Write (Input Password : );Console.Write (Input Password : );

    password = Console.ReadLine ();password = Console.ReadLine ();

    if ( password == wind )if ( password == wind )

    {{

    Console.Write (Access Granted!);Console.Write (Access Granted!);

    }}

    Console.ReadKey(true);Console.ReadKey(true);

    Output:Output:Input Password :Input Password :windwind

    Access Granted!Access Granted!

  • 8/3/2019 Code Snippets (Conditional Statements)

    9/36

    IfStatement ExampleIfStatement Example

    Code:Code:string password;string password;

    Console.Write (Input Password : );Console.Write (Input Password : );

    password = Console.ReadLine ();password = Console.ReadLine ();

    if ( password == wind )if ( password == wind )

    {{

    Console.Write (Access Granted!);Console.Write (Access Granted!);

    }}

    Console.ReadKey(true);Console.ReadKey(true);

    Output:Output:Input Password :Input Password : earthearth

  • 8/3/2019 Code Snippets (Conditional Statements)

    10/36

    IfStatementfor 1 EmbeddedIfStatementfor 1 Embedded

    StatementStatementSyntax:Syntax:

    if ( Boolean Condition )if ( Boolean Condition )

    True embeddedTrue embedded--statements;statements;

    Note: Without the curly brackets, only oneNote: Without the curly brackets, only onestatement may be executed if thestatement may be executed if thecondition is true.condition is true.

  • 8/3/2019 Code Snippets (Conditional Statements)

    11/36

    IfStatement ExampleIfStatement Example

    Code:Code:string name;string name;

    Console.Write (Please write your name : );Console.Write (Please write your name : );

    name = Console.ReadLine ();name = Console.ReadLine ();

    if ( name == Eisen )if ( name == Eisen )

    Console.Write (Hello Boss.Console.Write (Hello Boss.\\n);n);

    Console.Write (This is the Demo Program.);Console.Write (This is the Demo Program.);

    Console.ReadKey(true);Console.ReadKey(true);

    Output:Output:Please write your name :Please write your name : EisenEisen

    Hello Boss.Hello Boss.

    This is the Demo Program.This is the Demo Program.

  • 8/3/2019 Code Snippets (Conditional Statements)

    12/36

    IfStatement ExampleIfStatement Example

    Code:Code:string name;string name;

    Console.Write (Please write your name : );Console.Write (Please write your name : );

    name = Console.ReadLine ();name = Console.ReadLine ();

    if ( name == Eisen )if ( name == Eisen )

    Console.Write (Hello Boss.Console.Write (Hello Boss.\\n);n);

    Console.Write (This is the Demo Program.);Console.Write (This is the Demo Program.);

    Console.ReadKey(true);Console.ReadKey(true);

    Output:Output:Please write your name :Please write your name :MikeMike

    This is the Demo Program.This is the Demo Program.

  • 8/3/2019 Code Snippets (Conditional Statements)

    13/36

    IfIf Else StatementElse Statement

    This type of Ifstatement allows theThis type of Ifstatement allows theprogram to execute 2 instructions, one setprogram to execute 2 instructions, one set

    of instructions if the condition isof instructions if the condition is truetrue andandanother if the condition isanother if the condition is falsefalse..

  • 8/3/2019 Code Snippets (Conditional Statements)

    14/36

    IfIf Else StatementElse Statement

    Syntax:Syntax:if ( Boolean Condition )if ( Boolean Condition )

    {{

    True embeddedTrue embedded--statements;statements;}}

    elseelse

    {{

    False embeddedFalse embedded--statements;statements;

    }}

    Note: The rule ofcurly brackets applies in a similar way.Note: The rule ofcurly brackets applies in a similar way.

  • 8/3/2019 Code Snippets (Conditional Statements)

    15/36

    IfIf Else Statement ExampleElse Statement Example

    Code:Code:Console.Write (Input Grade : );Console.Write (Input Grade : );

    int grade = int.Parse ( Console.ReadLine() );int grade = int.Parse ( Console.ReadLine() );

    if ( grade >= 70 )if ( grade >= 70 )

    {{

    Console.Write (You Passed);Console.Write (You Passed);

    }}

    elseelse

    {{

    Console.Write (You Failed);Console.Write (You Failed);

    }}

    Console.ReadKey(true);Console.ReadKey(true);

    Output:Output:Input Grade :Input Grade : 8080

    You PassedYou Passed

  • 8/3/2019 Code Snippets (Conditional Statements)

    16/36

    IfIf Else Statement ExampleElse Statement Example

    Code:Code:Console.Write (Input Grade : );Console.Write (Input Grade : );

    int grade = int.Parse ( Console.ReadLine() );int grade = int.Parse ( Console.ReadLine() );

    if ( grade >= 70 )if ( grade >= 70 )

    {{

    Console.Write (You Passed);Console.Write (You Passed);

    }}

    elseelse

    {{

    Console.Write (You Failed);Console.Write (You Failed);

    }}

    Console.ReadKey(true);Console.ReadKey(true);

    Output:Output:Input Grade :Input Grade : 5050

    You FailedYou Failed

  • 8/3/2019 Code Snippets (Conditional Statements)

    17/36

    IfIf Else Statement (Alternative)Else Statement (Alternative)

    Syntax:Syntax:

    if ( Boolean Condition )if ( Boolean Condition )

    True embeddedTrue embedded--statements;statements;elseelse

    False embeddedFalse embedded--statements;statements;

    Note: Without the curly braces, onlyNote: Without the curly braces, only 1 embedded1 embeddedstatementstatementmay be inserted per section of themay be inserted per section of the IfIfstatementstatement..

  • 8/3/2019 Code Snippets (Conditional Statements)

    18/36

    IfIf Else Statement ExampleElse Statement Example

    Code:Code:Console.Write (Input Grade : );Console.Write (Input Grade : );

    int grade = int.Parse ( Console.ReadLine() );int grade = int.Parse ( Console.ReadLine() );

    if ( grade >= 70 )if ( grade >= 70 )

    Console.Write (You Passed);Console.Write (You Passed);

    elseelse

    Console.Write (You Failed);Console.Write (You Failed);

    Console.ReadKey(true);Console.ReadKey(true);

    Output:Output:Input Grade :Input Grade : 8080

    You PassedYou Passed

  • 8/3/2019 Code Snippets (Conditional Statements)

    19/36

    IfIf Else Statement ExampleElse Statement Example

    Code:Code:Console.Write (Input Grade : );Console.Write (Input Grade : );

    int grade = int.Parse ( Console.ReadLine() );int grade = int.Parse ( Console.ReadLine() );

    if ( grade >= 70 )if ( grade >= 70 )

    Console.Write (You Passed);Console.Write (You Passed);

    elseelse

    Console.Write (You Failed);Console.Write (You Failed);

    Console.ReadKey(true);Console.ReadKey(true);

    Output:Output:Input Grade :Input Grade : 5050

    You FailedYou Failed

  • 8/3/2019 Code Snippets (Conditional Statements)

    20/36

    IfIf Else IfStatementElse IfStatement

    This type of Ifstatement allows us toThis type of Ifstatement allows us tospecify multiple conditions.specify multiple conditions.

  • 8/3/2019 Code Snippets (Conditional Statements)

    21/36

    IfIf Else IfStatementElse IfStatement

    Syntax:Syntax:if ( Boolean Condition A )if ( Boolean Condition A )

    {{

    EmbeddedEmbedded--Statements for Condition AStatements for Condition A;;

    }}

    else if ( Boolean Condition B )else if ( Boolean Condition B ){{

    EmbeddedEmbedded--Statements for Condition BStatements for Condition B;;

    }}

    else if ( Boolean Condition C )else if ( Boolean Condition C )

    {{

    EmbeddedEmbedded--Statements for Condition CStatements for Condition C;;

    }}

    elseelse{{

    EmbeddedEmbedded--Statements for Conditions not metStatements for Conditions not met;;

    }}

    Note: You can have variable number ofconditions when using this type ofNote: You can have variable number ofconditions when using this type ofIfstatementIfstatement..

  • 8/3/2019 Code Snippets (Conditional Statements)

    22/36

    Sample Code:Sample Code:Console.Write (Input Age : );Console.Write (Input Age : );

    int age = int.Parse ( Console.ReadLine() );int age = int.Parse ( Console.ReadLine() );

    if ( age >= 0 && age = 0 && age = 21 && age = 21 && age = 51 && age = 51 && age

  • 8/3/2019 Code Snippets (Conditional Statements)

    23/36

    Output Sample 1:Output Sample 1:

    Input Age :Input Age : 3737

    Youre an adult.Youre an adult.

    Output Sample 2:Output Sample 2:

    Input Age :Input Age : 2020

    Youre still young.Youre still young.

    Output Sample 3:Output Sample 3:Input Age :Input Age : 9090

    Youre old.Youre old.

  • 8/3/2019 Code Snippets (Conditional Statements)

    24/36

    Switch StatementSwitch Statement

    Switch statementSwitch statementis an alternative tousing Ifis an alternative tousing IfElse Ifstatement.Else Ifstatement.

    Switch statementSwitch statementevaluate the value ofa certainevaluate the value ofa certainvariable usingvariable usingcasescases..

    AA break statementbreak statementallows the program to exitallows the program to exitthe entirethe entire switch statementswitch statementsection.section.

    Thus, aThus, a break statementbreak statementprevents a case ofaprevents a case ofaswitch statementswitch statementfrom moving to the next case.from moving to the next case.

  • 8/3/2019 Code Snippets (Conditional Statements)

    25/36

    Switch StatementSwitch Statement

    Syntax:Syntax:switch (variable)switch (variable)

    {{

    case A:case A:

    Embedded statements if variable is A;Embedded statements if variable is A;

    break;break;

    case B:case B:

    Embedded statements if variable is B;Embedded statements if variable is B;

    break;break;

    case C:case C:

    Embedded statements if variable is C;Embedded statements if variable is C;

    break;break;default:default:

    Statements if variable is other than A, B or C;Statements if variable is other than A, B or C;

    break;break;

    }}

  • 8/3/2019 Code Snippets (Conditional Statements)

    26/36

    Switch Statement TipsSwitch Statement Tips

    Syntax:Syntax:switch (variable)switch (variable)

    {{

    case A:case A:

    Embedded statements if variable is A;Embedded statements if variable is A;

    break;break;

    case B:case B:

    Embedded statements if variable is B;Embedded statements if variable is B;

    break;break;

    case C:case C:

    Embedded statements if variable is C;Embedded statements if variable is C;

    break;break;default:default:

    Statements if variable is other than A, B or C;Statements if variable is other than A, B or C;

    break;break;

    }}

    No Semi-colon ( ; )

  • 8/3/2019 Code Snippets (Conditional Statements)

    27/36

    Switch Statement TipsSwitch Statement Tips

    Syntax:Syntax:switch (variable)switch (variable)

    {{

    case A:case A:

    Embedded statements if variable is A;Embedded statements if variable is A;

    break;break;

    case B:case B:

    Embedded statements if variable is B;Embedded statements if variable is B;

    break;break;

    case C:case C:

    Embedded statements if variable is C;Embedded statements if variable is C;

    break;break;default:default:

    Statements if variable is other than A, B or C;Statements if variable is other than A, B or C;

    break;break;

    }}

    Enclosed in CurlyBraces { }

  • 8/3/2019 Code Snippets (Conditional Statements)

    28/36

    Switch Statement TipsSwitch Statement Tips

    Syntax:Syntax:switch (variable)switch (variable)

    {{

    case A:case A:

    Embedded statements if variable is A;Embedded statements if variable is A;

    break;break;

    case B:case B:

    Embedded statements if variable is B;Embedded statements if variable is B;

    break;break;

    case C:case C:

    Embedded statements if variable is C;Embedded statements if variable is C;

    break;break;default:default:

    Statements if variable is other than A, B or C;Statements if variable is other than A, B or C;

    break;break;

    }}

    Use Colons forcases ( : )

  • 8/3/2019 Code Snippets (Conditional Statements)

    29/36

    Switch Statement TipsSwitch Statement Tips

    Syntax:Syntax:switch (variable)switch (variable)

    {{

    case A:case A:

    Embedded statements if variable is A;Embedded statements if variable is A;

    break;break;

    case B:case B:

    Embedded statements if variable is B;Embedded statements if variable is B;

    break;break;

    case C:case C:

    Embedded statements if variable is C;Embedded statements if variable is C;

    break;break;default:default:

    Statements if variable is other than A, B or C;Statements if variable is other than A, B or C;

    break;break;

    }}

    Use break for allcases

  • 8/3/2019 Code Snippets (Conditional Statements)

    30/36

    Switch StatementSwitch Statement

    When usingWhen usingstringstring data types, case valuesdata types, case valuesare enclosed inare enclosed in double quotes ( )double quotes ( )..

    Cases areCases are not enclosednot enclosed when usingwhen usingnumericnumeric data types.data types.

  • 8/3/2019 Code Snippets (Conditional Statements)

    31/36

    Code:Code:Console.Write (Input A, B or C : );Console.Write (Input A, B or C : );

    string choice = Console.ReadLine ();string choice = Console.ReadLine ();

    switch (choice)switch (choice){{

    case A:case A:

    Console.Write (You selected A.);Console.Write (You selected A.);

    break;break;

    case B:case B:Console.Write (You selected B.);Console.Write (You selected B.);

    break;break;

    case C:case C:

    Console.Write (You selected C.);Console.Write (You selected C.);

    break;break;

    default:default:Console.Write (Invalid Input!);Console.Write (Invalid Input!);

    break;break;

    }}

    Console.ReadKey(true);Console.ReadKey(true);

  • 8/3/2019 Code Snippets (Conditional Statements)

    32/36

    Output Sample 1:Output Sample 1:Input A, B or C :Input A, B or C :AA

    You selected A.You selected A.

    Output Sample 2:Output Sample 2:Input A, B or C :Input A, B or C : BB

    You selected B.You selected B.

    Output Sample 3:Output Sample 3:Input A, B or C :Input A, B or C : CC

    You selected C.You selected C.

    Output Sample 4:Output Sample 4:Input A, B or C :Input A, B or C : DD

    Invalid Input!Invalid Input!

  • 8/3/2019 Code Snippets (Conditional Statements)

    33/36

    Code:Code:Console.Write (Input 1, 2 or 3 : );Console.Write (Input 1, 2 or 3 : );

    int choice = int.Parse ( Console.ReadLine() );int choice = int.Parse ( Console.ReadLine() );

    switch (choice)switch (choice){{

    case 1:case 1:

    Console.Write (One!);Console.Write (One!);

    break;break;

    case 2:case 2:Console.Write (Two!!);Console.Write (Two!!);

    break;break;

    case 3:case 3:

    Console.Write (Three!!!);Console.Write (Three!!!);

    break;break;

    default:default:Console.Write (Invalid Input!!!!);Console.Write (Invalid Input!!!!);

    break;break;

    }}

    Console.ReadKey(true);Console.ReadKey(true);

  • 8/3/2019 Code Snippets (Conditional Statements)

    34/36

    Output Sample 1:Output Sample 1:Input 1, 2 or 3 :Input 1, 2 or 3 : 11

    One!One!

    Output Sample 2:Output Sample 2:Input 1, 2 or 3 :Input 1, 2 or 3 : 22

    Two!!Two!!

    Output Sample 3:Output Sample 3:Input 1, 2 or 3 :Input 1, 2 or 3 : 33

    Three!!!Three!!!

    Output Sample 4:Output Sample 4:Input 1, 2 or 3 :Input 1, 2 or 3 : 44

    Invalid Input!!!!Invalid Input!!!!

  • 8/3/2019 Code Snippets (Conditional Statements)

    35/36

    DEMO Programs for the LessonDEMO Programs for the Lesson

    1.1. Avoiding division by zero in a divide operationAvoiding division by zero in a divide operation2.2. Checking textboxes for empty in a summationChecking textboxes for empty in a summation

    programprogram3.3. Validating numerical entries in a problemValidating numerical entries in a problem

    similar to item 1similar to item 1

    4.4. Retrievinguser response in a message boxRetrievinguser response in a message box5.5. Check ifusername is not empty andCheck ifusername is not empty and passwordpassword

    has minimumhas minimum of7 charactersof7 characters

  • 8/3/2019 Code Snippets (Conditional Statements)

    36/36

    The End. Thank youfor listening.The End. Thank youfor listening.