fp201 3.prog control

Upload: muhammad-mukhromin

Post on 07-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 FP201 3.Prog Control

    1/28

    Click to edit Master subtitle style4/14/12

    Chapter 3program control

    3.1 Solve problems using selection control

    structures3.2 Loops control structures

    FP201: ProgrammingFundamentals/ Chapter 3

  • 8/4/2019 FP201 3.Prog Control

    2/28

    4/14/12

    LETS DO PRE-TEST !

  • 8/4/2019 FP201 3.Prog Control

    3/28

    4/14/12

    3.1 SOLVE PROBLEMS USINGSELECTION CONTROL STRUCTURES

    Learning Outcomes 3.1At the end of this sub-chapterstudents should be able to:

    . 3.1.1 Explain the

    selection statement:

  • 8/4/2019 FP201 3.Prog Control

    4/28

    4/14/12

    At the end of this sub-chapter,students should be able to:

    . 3.1.1Explain the selection

    statement:

    3.1 SOLVE PROBLEMS USINGSELECTION CONTROL

    STRUCTURES

  • 8/4/2019 FP201 3.Prog Control

    5/28

    4/14/12

    SELECTION CONTROL ? It allows instructions to be executed in a non-

    sequential ways.

    The user can choose what he want to process.

    It compares two expressions.

    Based on the comparison, it takes a certain courseof action.

  • 8/4/2019 FP201 3.Prog Control

    6/28

    4/14/12

  • 8/4/2019 FP201 3.Prog Control

    7/28

    4/14/12

    Pairing Activity 1Comparing & identify a variety typeof selection control

    Instructions:

    1. You are given a few program sample.

    2. Identify the type of selection control foreach program code

    3. Your answer will be discuss for the nextlecture.

    http://var/www/apps/conversion/current/tmp/FP201.T&L%20Strategies/5%20minutes%20Pairing%20DiscussionProgram%20samples.docxhttp://var/www/apps/conversion/current/tmp/FP201.T&L%20Strategies/5%20minutes%20Pairing%20DiscussionProgram%20samples.docx
  • 8/4/2019 FP201 3.Prog Control

    8/28

    4/14/12

    Single if If else Nested if

    Used to execute a set ofstatements when the givencondition is satisfied.Conditional statementswithin the block areexecuted when thecondition in the ifstatement is satisfied.

    Executes the set ofstatements in if block,when the given condition issatisfied.Executes the statements inthe else block, when thecondition is not satisfied.

    The if statements writtenwithin the body of anotherif statement to testmultiple conditions iscalled nested if.

  • 8/4/2019 FP201 3.Prog Control

    9/28

    4/14/12

    Syntax : If selection control

    Single if If else Nested if if(){

    ;

    }

    if (){< statements1>;}else{

    ;

    }

    if (){

    if(){;}else{;}

    }

    else{;}

    Inner if

    Outer if

  • 8/4/2019 FP201 3.Prog Control

    10/28

    4/14/12

    Program 1

    /* Program to check whether the givennumber is less than 50 */

    #include

    using namespace std;

    void main()

    {

    int num;

    cin >> num;

    if (num < 50)

    cout

  • 8/4/2019 FP201 3.Prog Control

    11/28

    4/14/12

    Program 2

    /* Program to check whether the givennumber is less than 50 */

    #include

    using namespace std;

    void main()

    {

    int num;

    cin >> num;

    if (num < 50)" "

    Type of Selection control ? If else

    How manycondition?

    Which are the

    condition?

  • 8/4/2019 FP201 3.Prog Control

    12/28

    4/14/12

    Program 3#include

    using namespace std;

    void main()

    {

    int num;

    cout > num;

    if (num > 0)

    {

    if (num < 10)

    cout

  • 8/4/2019 FP201 3.Prog Control

    13/28

    4/14/12

    Program 4#include using namespace std;void main(){int num;

    cout > num;if (num < 0)cout

  • 8/4/2019 FP201 3.Prog Control

    14/28

    4/14/12

    Pairing Activity 2Develop flow chart and find the output

    Instructions:1. You are choose 2 program sample (from

    Activity 1).

    2. Develop flow chart

    3. Give possible input and output for theprogram

    http://var/www/apps/conversion/current/tmp/FP201.T&L%20Strategies/5%20minutes%20Pairing%20DiscussionProgram%20samples.docxhttp://var/www/apps/conversion/current/tmp/FP201.T&L%20Strategies/5%20minutes%20Pairing%20DiscussionProgram%20samples.docx
  • 8/4/2019 FP201 3.Prog Control

    15/28

    4/14/12

    iii. Switch statement Is a multi-way selection statement.

    Contains various case statements. The case statements are executed based on thevalue of the expression.

    Abreak statement passes the control outsideswitch structure.

  • 8/4/2019 FP201 3.Prog Control

    16/28

    4/14/12

    Syntax of Switch Statement

    switch (Expression)

    {

    case exp_1:

    statement 1;

    break;

    case exp_2:

    statement 2;

    break;

  • 8/4/2019 FP201 3.Prog Control

    17/28

    4/14/12

    Sample Program

    #include void main()

    {

    int Month;cout > Month;switch (Month)

    {

    case 1: cout

  • 8/4/2019 FP201 3.Prog Control

    18/28

    4/14/12

    At the end of this sub-chapter,students should be able to:

    . 3.1.2Convert the nested if

    statement to switch case

    3.1 SOLVE PROBLEMS USINGSELECTION CONTROL

    STRUCTURES

  • 8/4/2019 FP201 3.Prog Control

    19/28

    4/14/12

    Convert the switch to ifelse

    statement IF SWITCH#include

    void main()

    {

    int Month;

    cout Month;

    #include

    void main()

    {

    int Month;

    cout Month;

  • 8/4/2019 FP201 3.Prog Control

    20/28

    4/14/12

    At the end of this sub-chapter,students should be able to:

    . 3.1.2 Describe the function of breastatement

    3.1 SOLVE PROBLEMS USINGSELECTION CONTROL

    STRUCTURES

  • 8/4/2019 FP201 3.Prog Control

    21/28

    4/14/12

    Break is used withinloops and switch

    statements to jump tothe end of the codeblock.

    It causes the "//code..."above to be skipped andterminates the loop.

    In switch case

    statements, break causes

    loop

    {break;//code...

    }

    switch( variable )

    {case value:/* code */break;

    case value:/* code */break;

    }

  • 8/4/2019 FP201 3.Prog Control

    22/28

    4/14/12

    At the end of this sub-chapter,students should be able to:

    . 3.1.4 Solve a given problem bywriting algorithm and program,

    run, test and debug using selection

    3.1 SOLVE PROBLEMS USINGSELECTION CONTROL

    STRUCTURES

  • 8/4/2019 FP201 3.Prog Control

    23/28

    4/14/12

    Activity

  • 8/4/2019 FP201 3.Prog Control

    24/28

    4/14/12

    Program Sample 1:ifStatementint num1, num2, min;

    coutnum1 >> num2;

    min = num1;

    if (num1 > num2)min = num2;

    cout

  • 8/4/2019 FP201 3.Prog Control

    25/28

    4/14/12

    Program Sample 2:ifStatementint mark;

    cout mark);

    mark if (mark > 80)

    {

    cout

  • 8/4/2019 FP201 3.Prog Control

    26/28

    4/14/12

    Test your skill

    1. Write a program to input at least 2data from user. Process the data anddisplay the output.

    Example output

    Total Buying: RM 80

    Total Pay : RM 100

  • 8/4/2019 FP201 3.Prog Control

    27/28

    4/14/12

    3.2 LOOPS AND CONTROL STRUCTURES

    Learning Outcomes 3.2At the end of this sub-chapterstudents should be able to:

    .

    . 3.2.1 Identify the

  • 8/4/2019 FP201 3.Prog Control

    28/28

    4/14/12