Conditional Statement C# Programming


Conditional Statement C# Programming:

Dear seekers of knowledge:
Here We will Study about Conditional Statements ( if , is-else, nested if and switch statement)

CONTROL FLOW:

“Control flow” is the order in which statements are executed
In  Sequential control flow  -- the next statement executed is the next one that appears, in order, in the C# program.

Conditional Control Flow:



  1. choosing which of two (or more) statements to execute before continuing
  2. choosing whether or not to to skip a statement before continuing.

    Conditional Execution:


    A conditional statement allows the computer to choose an execution path depending on the value of a variable or expression
    if the withdrawal is more than the bank
    balance, then print an error
    if today is my birthday, then add one to my age
    if using whole milk, add two eggs, otherwise add three eggs

    Conditional ("if ") Statement:

    if (condition)

    {statement

    }

    if (withdrawalAmount > balance)
                    Console.WriteLine( “Not enough money\n”);
    if (temperature > 98.6)
       Console.WriteLine(“You have a fever.\n”);
    if (x < 100) x = x + 1;


    CONDITIONS FOR IF STATEMENT:

    ¨In parentheses is a condition, also called a “logical” or “Boolean” expression
    ¨Made up of variables, constants, arithmetic expressions, and the relational operators
    ¨
    ¨Math symbols:  < ,  £,   >,   ³ ,    = ,   ¹
    ¨in C#:                     < ,<=,  > , >= , == ,  !=

    Conditional Expressions: 


    ¨

    ¨air_temperature  >  80.0

    ¨98.6 <= body_temperature

    ¨marital_status  ==  ‘M’
    ¨divisor  !=  0
    ¨
    ¨
    ¨Such expressions are used in “if” statements and numerous other places in C#.

    Value of Conditional Expressions:


    ¨What is the value of a conditional expression??

    ¨Answer: we think of it as TRUE or FALSE





    Complex Conditionals: 




    if I have at least Rs.100 or you have at least Rs.100, then we can go to the watch today’s match

    if the temperature is below 32 degrees and it’s raining, then it’s snowing

    if it’s not the case that it’s Saturday or Sunday, then it’s a work day

    Complex Conditionals in C : 

    We use Boolean operators  to code complex conditionals in C#.
    We’ll say lots more about this later!  For now, here is  some information for reference.
    Boolean operators         &&        ||        ! 
                                            and       or      not

    if (myMoney>=100 || yourMoney>=100)  
      canGoToAuditorium = TRUE;


    Multiple Actions:


    ¨What if there’s more than one conditional action? 

    ¨“If your temperature is high, then you have a fever and should take two aspirin and go to bed and call in sick tomorrow”



    Compound Statement: 




    ¨Groups together statements so that they are treated as a single statement:

    {

    statement1 ;

    statement2 ;
    ...
    }
    ¨Also called a "block."
    ¨Highly useful
    ¡Not just in conditionals, but many places in C#



    USING COMPOUND STATEMENT:



    if ( temperature > 98.6 ) {

    Console.WriteLine ( “You have a fever. \n” );
    aspirin  =  aspirin - 2 ;
    Console.WriteLine (“Go to bed/n”);
    Console.WriteLine (“Sleep in tomorrow/n”);



    IF-ELSE STATEMENT:


    ¨Print error message only if the condition is false:

    if ( balance >= withdrawal ) {

    balance = balance - withdrawal ;

    dispense_funds ( withdrawal ) ;
    }
    else {
    Console.WriteLine ( “Insufficient Funds! \n ” ) ;
    }


    Nested if Statements:


    if ( balance >= withdrawal ) {

    balance = balance - withdrawal ;

    dispense_funds ( withdrawal ) ;
    } else {
    if ( balance >= BILL_SIZE )
     Console.WriteLine ( “Try a smaller amount. \n ” ) ;
    else
    Console.WriteLine ( “Go away! \n ” ) ;
    }




     Nested ifs , Part II :


    if ( x == 5 )

    {

    if  ( y == 5 )
             Console.WriteLine ( “Both are 5 ”) ;
    else
             Console.WriteLine ( “x is 5, but y is not”) ;
    }
    else {
    if  ( y == 5 )
            Console.WriteLine ( “y is 5, but x is not”) ;
    else
            Console.WriteLine ( “Neither is 5”) ;
    }
    .////////////////////////CODE/////////////////////////

    if ( income < 15000 ) {

       Console.WriteLine( “No tax.” );

    }

    if ( income >= 15000 && income < 30000 ) {
       Console.WriteLine(“18%% tax.”);
    if ( income >= 30000 && income < 50000 ) {
       Console.WriteLine(“22%% tax.”);
    }
    if ( income >= 50000 && income < 100000 ) {
       Console.WriteLine(“28%% tax.”);
    }
    if ( income >=100000) {
       Console.WriteLine(“31%% tax.”);
    }
    ////////////////////////////////////////////////////////


    The World’s Last C# Bug: :D

    status = check_radar ( ) ;

    if (status = 1) {

      launch_missiles ( ) ;
    }

    Pitfalls of if :

    ¨&    is different from   &&
    ¨|  is different from   ||
    ¡
    ¡& and | are not used in this class
    ¡If used by mistake, no syntax error, but program may operate incorrectly



    ¨Beware  ==  and  !=  with doubles:
    ¡double x ;
    ¡x = 30.0 * (1.0 / 3.0) ;

    ¡if ( x == 10.0 )




    COMPUTER PROGRAMMING SWITCH STATEMENT:


    The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body. 
    The switch statement is unlike the if statement. It requires that each case be constant. This constraint allows various optimizations in the lower-level intermediate representation. Switch speeds up certain parts of a program.
    The problem with switch statements is essentially that of duplication. Often you find the same switch statement scattered around a program in different places. If you add a new clause to the switch, you have to find all these switch statements and change them.
    ///////////////////////////END/////////////////////////

    ////////////////////////KEEP EXPLORING THE KNOWLEDGE////////////
    ////////////////////KEEP VISITING FOR NEW LESSONS///////////

No comments:

Post a Comment