chapter 2 decesion making statements dk mamonai 09ce37

Upload: darya-memon

Post on 07-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    1/12

    Test Expression

    Test Expression

    OBJECT: TOBECOMEFAMILIARWITH DECISIONMAKINGSTATEMENTS.

    DECISIONMAKINGSTATEMENTS

    Decision making statements are the statements which take the decisions to make the

    conditions satisfied and execute their body. If the condition is satisfied (True) then thestatement will cause its body to be executed and if the condition is not satisfied (False)then the control will be transferred to the next condition or to the first statement afterthe body of the conditional statement. There are three types of decision making

    statement also known as conditional statements. These are so called conditionalstatements because they depend on certain conditions. They are,

    1) The if Statement

    2) The if-else Statement

    3) The switch Statement

    1) The if Statement

    It is the simplest decision making/conditional statement. The if statement contains thekeyword ifwhich is predefined in C++ followed by the condition in the parentheses ()with some relational and logical operators. If the condition is satisfied then the body ofthe if statement will be executed else the control will be transferred to the first existing

    statement after the if statement. If the body of the if statement contains only singlestatement then you dont have to enclose that statement in the braces but in case of

    multiple statements you should enclose the block of code in the curly braces. Thesyntax of the if statement is given below;

    if (a>b && a>c)

    statement; Single Statement if body

    if (a>b && a>c)

    {

    statement;

    statement; Multiple Statement if body

    statement;

    21

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    2/12

    }

    FLOWCHARTOFIF STATEMENT

    Program (If.cpp)

    #include

    #include

    void main()

    {

    int Age1=20, Age2=15, Age3=7;

    if(Age1>Age2&&Age1>Age3)

    {

    cout

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    3/12

    Test Expression

    Test Expression

    2) The if-else Statement

    The if-else statement is another decision making statement. The if-else statementcontains the keywords if and else which are predefined in C++. The condition iscontained in theparentheses () after the keyword if. If the condition is satisfied then the

    block of code after the keyword ifis executed while if the condition is not satisfied thenthe control is transferred to the else and the block of code contained after it is executed.

    If you want to execute the single statement if the condition is either satisfied or not thenyou dont have to enclose it in the curly braces but if you want to execute certain block

    of code (multiple statements) then you have to enclose the block of code in the curlybraces. The syntax of the if-else statement is given below;

    if (a>b && a>c)

    statement; Single Statement if body

    else

    statement; Single Statement else body

    if (a>b && a>c)

    {

    statement;

    statement; Multiple Statement if body

    statement;

    }

    else{

    statement;

    statement; Multiple Statement else body

    statement;

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    4/12

    }

    FLOWCHARTOFIF-ELSE STATEMENT

    Program (IfElse.cpp)

    #include

    #include

    void main()

    {

    int num1=100, num2=50;

    if(num1>num2)

    {

    cout

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    5/12

    Integer or character variable

    Note: no semicolon here

    Integer or character constant

    First case body

    Causes exit from switch

    Second case body

    Third case body

    Default case body

    Note: no semicolon here

    3) The switch Statement

    The switch statement is also the decision making statement like if statementand if-elsestatement. The switch statement is used when the bunch of decisions depend on the

    same variable. It works as if and if-else statements but it is some what clearer andeasily understandable at glance. The switch statement starts with the keyword switch

    (lower case) followed by the variable in the parentheses (). The switch statement takesthe decision according to the values of the variable in the parenthesis. The whole body

    of the switch statement is enclosed by curly braces. Then the switch statement matchesthe values of the variable with the cases in its body. The keyword case followed by thechoice and the colon makes the block of code below it to be executed if the value of theswitch variable matches with the choice in the case. You can give multiple cases in the

    switch statement and the switch statement will try to match the value of the variablewith the cases and if no case is matched the control is returned to the default. The key

    word defaultfollowed by the colon causes the block of code below it to be executed if

    the switch statement does not match any of the case. The breakstatement is used afterthe block of code in each case, which causes the control to be transferred out of theswitch statement when ever the case executes its block of code. The syntax of the

    switch statement is given below;

    switch (ch)

    {

    case 1:

    statement;statement;

    break;

    case 2:

    statement;

    statement;

    break;

    case 3:

    statement;statement;

    break;

    default:

    statement;

    statement;

    24

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    6/12

    True

    True

    True

    }

    FLOWCHARTOFSWITCHSTATEMENT

    Switch

    variableequals 1st

    case constant

    1st case body

    Exit

    False

    Switchvariable

    equals 2ndcase constant

    Switchvariable

    equals 3rdcase constant

    2nd case body

    3rd case body

    Default body

    False

    False

    25

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    7/12

    Program (Switch.cpp)

    #include

    #include

    void main()

    {

    int a, b;

    char ch;

    couta;

    coutb;

    cout

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    8/12

    EXERCISE

    1) Write a program that determines the year entered by user is leap year or not?

    2) Write a program which gives the following output

    Enter age>>

    And prints the following:

    If Age is greater than 45 then prints the message You are old stay at home and wait

    for call

    If Age is greater than 30 and less than 45 then prints Hey! man enjoy your life withyour kids

    If Age is greater than 20 and less than 30 then prints Cool! have a search for yourbest suit

    If Age is greater than 10 and less than 20 then prints Work hard! Your days to

    study

    If Age is less than 10 then prints the message Oh! Kid your days to cry

    Use if-else statement.

    3) Write the above program #02 by using the switch statement?

    4) Develop a C++ program which will compute the power dissipation of a resistor,when the user inputs current value and the resistance. Program warns the user if

    power dissipation is above 1 watt. The power dissipation is measured by P=I2R.

    27

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    9/12

    EXERCISESOLUTIONS

    ===================================================

    Program #01

    #include

    #include

    void main()

    {

    int year;

    coutyear;

    if(year%4==0)

    cout

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    10/12

    else if(Age>30&&Age

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    11/12

    {

    case 1:

    cout

  • 8/6/2019 Chapter 2 Decesion making statements Dk Mamonai 09CE37

    12/12

    ===================================================

    31