fcp_3_new

Upload: jagdish750

Post on 07-Jul-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/19/2019 FCP_3_new

    1/22

     Introduction to C

     Programming

    Introduction

  • 8/19/2019 FCP_3_new

    2/22

    Control Instruction

    alter our actions by circumstances. If the weather is fine, then I will go for a stroll

    If the highway is busy I would take a diversion

    notice that all these decisions depend on some condition being

    met C language too must be able to perform different sets of

    actions depending on the circumstances

    C has three major decision making instructions

    If statement

    if-else statement

    SThe conditional operators.

    switch statement

  • 8/19/2019 FCP_3_new

    3/22

     Decisions! Decisions!

    Last class programs steps are eecuted se!uentially i.e. in thesame order in which they appear in the program.

    "any a times, we want a set of instructions to be eecuted in

    one situation, and an entirely different set of instructions to be

    eecuted in another situation. This kind of situation is dealt in C programs using a decision

    control instruction

    #a$ The if statement

    #b$ The if-else statement #c$ The conditional operators.

  • 8/19/2019 FCP_3_new

    4/22

    The if Statement  %eneral form

    if # this condition is true $&

    eecute this statement '

    (

    )ut how do we epress the condition itself in C*

    we epress a condition using C+s relational+ operators.

    this expression is true if 

    -- y is e!ual to y

    - y is not e!ual to y

    / y is less than y

    0 y is greater than y

    /- y is less than or e!ual to y

    0- y is greater than or e!ual to y

  • 8/19/2019 FCP_3_new

    5/22

    12 3emonstration of if statement 21

    4include/stdio.h0

    void main# $

    &

    int num '

    printf # 56nter a number less than 78 5 $ '

    scanf # 59d5, :num $ '

    if # num /- 78 $

    printf # 5;hat an obedient servant you are 5 $ '(

  • 8/19/2019 FCP_3_new

    6/22

  • 8/19/2019 FCP_3_new

    7/22

    ;hile purchasing certain items, a discount of 789 is offered ifthe !uantity purchased is more than 7888. If !uantity and price

    per item are input through the keyboard, write a program to

    calculate the total epenses.

    void main# $

    &

    int !ty, dis - 8 'float rate, tot '

    printf # 56nter !uantity and rate 5 $ '

    scanf # 59d 9f5, :!ty, :rate$ '

    if # !ty 0 7888 $dis - 78 '

    tot - # !ty 2 rate $ < # !ty 2 rate 2 dis 1 788 $ '

    printf # 5Total epenses - =s. 9f5, tot $ '

    (

  • 8/19/2019 FCP_3_new

    8/22

  • 8/19/2019 FCP_3_new

    9/22

    The current year and the year in which the employee joined the

    organi>ation are entered through the keyboard. If the number ofyears for which the employee has served the organi>ation isgreater than ? then a bonus of =s. @A881< is given to theemployee. If the years of service are not greater than ?, thenthe program should do nothing.

  • 8/19/2019 FCP_3_new

    10/22

    4include/stdio.h0

    void main# $&

    int bonus, cy, yoj, yrBofBser '

    printf # 56nter current year and year of joining 5 $ '

    scanf # 59d 9d5, :cy, :yoj $ '

    yrBofBser - cy < yoj '

    if # yrBofBser 0 ? $

    &

    bonus - @A88 '

    printf # 5)onus - =s. 9d5, bonus $ '

    (

    (

  • 8/19/2019 FCP_3_new

    11/22

    The Real Thing  %eneral form

    if # this condition is true $& eecute this statement ' (

    )ut if # epression $

     & eecute this statement ' (

    epression can be any valid epressionif # ? @ 9 A $

    printf # 5This works5 $ '

    if # a - 78 $

    printf # 56ven this works5 $ '

    if #

  • 8/19/2019 FCP_3_new

    12/22

    The if-else Statement 

    Can we eecute one group of statements if the epression

    evaluates to true and another group of statements if theepression evaluates to false*

    Df course This is what is the purpose of the else statement

    %eneral Eorm

    if # this condition is true $

    &

    eecute this statement '

    (else

    &

    eecute this statement '

    (

  • 8/19/2019 FCP_3_new

    13/22

    The group of statements after the if upto and not including the

    else  is called an if block+. Similarly, the statements after theelse form the else block’.

    Fotice that the else is written exactly below the if.

    Gad there been only one statement to be eecuted in the if

    block and only one statement in the else block we could have

    dropped the pair of braces.

     Hs with the if statement, the default scope of  else is also the 

    statement immediately after the else.

  • 8/19/2019 FCP_3_new

    14/22

    Example. In a company an employee is paid as under If his

    basic salary is less than =s. 7A88, then G=H - 789 of basicsalary and 3H - J89 of basic salary. If his salary is either

    e!ual to or above =s. 7A88, then G=H - =s. A88 and 3H -

    JK9 of basic salary. If the employees salary is input through

    the keyboard write a program to find his gross salary.

  • 8/19/2019 FCP_3_new

    15/22

    4include/stdio.h0

    void main# $

    &

    float bs, gs, da, hra '

    printf # 56nter basic salary 5 $ 'scanf # 59f5, :bs $ '

    if # bs / 7A88 $

    &

    hra - bs 2 78 1 788 '

    da - bs 2 J8 1 788 '

    (

    else

    &

    hra - A88 '

    da - bs 2 JK 1 788 '

    (

    gs - bs hra da '

    printf # 5gross salary - =s. 9f5, gs $ '

    (

  • 8/19/2019 FCP_3_new

    16/22

     Nested if-elses if we write an entire if-else construct within either the body of the if

    statement or the body of an else statement. This is called nesting+ of ifs.

     if # condition $

    &

    if # condition $

    do this '

    else

    &

    do this '

    and this '

    (

    (

      else

    do this '

  • 8/19/2019 FCP_3_new

    17/22

    4include/stdio.h0.

    void main# $

    &

    int i 'printf # 56nter either 7 or @ 5 $ '

    scanf # 59d5, :i $ '

    if # i -- 7 $

    printf # 5Mou would go to heaven 5 $ 'else

    &

      if # i -- @ $

    printf # 5Gell was created with you in mind5 $ '  else

    printf # 5Gow about mother earth 5 $ '

    (

    (

  • 8/19/2019 FCP_3_new

    18/22

    Forms of if 

    The if statement can take any of the following forms:

    #a$ if # condition $

    do this '

    #b$ if # condition $

    &

    do this '

    and this '

    (

    #c$ if # condition $do this '

      else

    do this '

  • 8/19/2019 FCP_3_new

    19/22

    Forms of if 

    #d$ if # condition $

    &

    do this '

    and this '

    (

      else

    &

    do this '

    and this '(

  • 8/19/2019 FCP_3_new

    20/22

    Forms of if 

    #e$ if # condition $

    do this '

      else

    &

    if # condition $

    do this '

    else

    &

    do this 'and this '

    (

    (

  • 8/19/2019 FCP_3_new

    21/22

    #f$ if # condition $

    &

    if # condition $

    do this '

    else

    &

    do this '

    and this '

    ((

      else

    do this '

  • 8/19/2019 FCP_3_new

    22/22

    Use of Logical Oerators

    C allows usage of three logical operators, namely, ::, NN and .

    These are to be read as HF3+ D=+ and FDT+ respectively. The first two operators, && and || allow two or more

    conditions to be combined in an if statement.