Chapter 3 - Fundamentals of Programming Language

Embed Size (px)

Citation preview

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    1/106

    1

    FP101PROGRAMMINGPRINCIPLES

    CHAPTER 3

    FUNDAMENTALS OF

    PROGRAMMING LANGUAGE

    Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    2/106

    Course Learning Outcome(CLO):Upon completion of this course, students should be able to:

    1) Explain the basic computer and programming fundamentals with

    appropriate examples of language and technology.

    2) Apply the different types of algorithm to solve problem efficiently.

    3) Solve problem effectively by applying related theories of the basic

    programming language to a given particular scenario using

    programming life cycle.

    2Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    3/106

    Topic Specification3.1 Understand data and identifier

    3.2 Solve problem using operators in aprogram

    3.3 Apply program control structure

    3Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    4/106

    3.1 Understand Data & Identifier

    4Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    5/106

    Understand Data & Identifier What is data?

    Input for the program

    Pre-processing fact Data types:

    Numeric

    Non-numeric

    5Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    6/106

    Data Types: Numeric A type of data that use to do a calculation

    only.

    2 types:

    Integer Number (..-2,-1,0,1,2..)

    Explicit Number (Floating number)

    6Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    7/106

    Data Types: Non Numeric Consist character, number, words, and

    specific symbol.

    We put them inside the apostrophe ( ).

    Example: A, task2 , & , ()

    7Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    8/106

    Data Types Table :

    8

    Data Types Description Example

    Numeric Integer Integers only -5, -90, 5, 0, 34

    Floating Integers + floating

    numbers

    -8.9 , 9.6, 2

    9.001, 0.7

    Non-numeric Characters, numbers andsymbol

    A, a, +, ()

    Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    9/106

    EXERCISE Determine the data types:

    2337

    0.56

    4.7

    2

    %

    /

    9Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    10/106

    Data Structures/ Hierarchy : Data Hierarchy refers to the systematic

    organization of data, often in a hierarchical

    form. Data organization involves fields,

    records, files and database. Bit

    Byte

    Field

    Record

    File

    Database

    10Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    11/106

    BIT Basic unit of memory.

    Either 0 or 1 can be stored.

    Smallest data of unit storage

    11Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    12/106

    BYTE Ordered collection of 8 bits

    Series of bits that represent characters.

    12Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    13/106

    FIELD Holds a single fact

    Consider a date field, e.g. "September

    19, 2004 have 3 fields Month

    Day of month

    Year

    Column in database

    13Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    14/106

    Example of Field

    14Norzimah Che Hassan

    DAY

    MONTH

    YEAR

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    15/106

    Example of Field

    15Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    16/106

    RECORD Collection of related fields

    Combination field about a thing, person,

    place etc. Row in database

    16Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    17/106

    EXAMPLE OF RECORD

    17Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    18/106

    FILE Collection of related records

    Table in database

    18Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    19/106

    EXAMPLE OF FILE

    19Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    20/106

    DATABASE One or more data files / table

    A database is a collection of data that is

    organized so that it can easily beaccessed, managed, and updated.

    20Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    21/106

    EXAMPLE OF DATABASE

    21Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    22/106

    Data Structures

    22

    BIT

    BYTE or CHARACTER (8 bits)

    FIELD or COLUMN (one or more Bytes)

    RECORD or ROW (one or more Fields)

    FILE or TABLE

    (one or more Record)

    DATABASE

    ( one or more Files)

    DATA SYSTEM

    (one or more

    Database)

    Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    23/106

    Identify Terms: Identifier

    Variables

    Constant

    23Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    24/106

    IDENTIFIER The given name to multiple elements in

    programming such as constant, variables

    and function.

    24Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    25/106

    VARIABLES Location memory

    Will keep data value

    The value are changeableduring entireprogramming execution.

    25Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    26/106

    VARIABLES

    26Norzimah Che Hassan

    int a;int b;

    int c;

    a = 5;b = 2;

    a = a + 1;

    result = a - b;

    Declaration of variables

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    27/106

    VARIABLES

    27Norzimah Che Hassan

    // operating with variables#include

    using namespace std;

    int main ()

    {

    // declaring variables:

    int a, b;int result;

    // process:

    a = 5;

    b = 2;

    a = a + 1;

    result = a - b;// print out the result:

    cout

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    28/106

    CONSTANT Constants are expressions with a fixed

    value.

    The value are not changeable duringentire programming execution.

    Example:

    28Norzimah Che Hassan

    const int days_in_year = 365;

    const float ChickenPerKg= 17.5;

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    29/106

    CONSTANT vs. VARIABLES

    29

    Constant Variables

    Value :- 25 Variables name: Age

    Value :- 25, 30, 15

    Value : - 3.2 Variables name: Cash

    Value :- 2.5, 3.0, 1.5

    Value :- Kangar Variables name: CityValue :- Ipoh, Kuala Lumpur,

    Georgetown

    Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    30/106

    How to write identifier?

    30

    Combination of: Character (A - Z), (a - z)

    Digit (0 - 9)

    Underscore(_)

    Cannot start with digit

    Not have reserve words

    No blank space

    No limit of character usage but the system will

    identify first 32 character

    Case sensitive

    Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    31/106

    EXERCISE

    31

    Determine either this is a valid identifier:

    1. my_school

    2. 100students

    3. Vari,ables

    4. PoliteknikBalikPulau

    5. PLAYINGFOOTBALL

    6. Playing Football7. Saya_suka_membaca

    8. %fail

    Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    32/106

    3.2 Solve problem UsingOperators in a Program

    32Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    33/106

    Operators in a Program What is OPERATOR?

    A symbol to represent particular computer

    operation. Consists of:

    i. Arithmetic Operators

    ii. Relational Operators

    iii. Logical Operators

    33Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    34/106

    ARITHMETICOPERATOR

    34Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    35/106

    Arithmetic Operator It have 5 basic operator in programming

    language: Symbol Operator

    + Addition

    _ Subtraction

    * Multiplication

    / Division

    % Modulus

    35Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    36/106

    Arithmetic Operator Modulus (%)? To get balance from two handling division.

    i.e.:

    a) 5 % 3 is 2

    13

    5

    3

    2 BALANCE IS 2!

    36Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    37/106

    b) 17 % 4 is 1.

    BALANCE IS 1!

    4 17

    16

    1

    4

    37Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    38/106

    Operator Priority

    Operator Priority

    ( ) Highest

    * / % Higher

    + - Lower

    It have a basic priority that should know :

    38Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    39/106

    Operator Prioritya) x = 5 + 2 * 4 1

    x = 5 + 2 * 4 1

    x = 5 + 8 1x = 13 - 1

    x = 12for the same priority,start from left side.

    39Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    40/106

    Operator Priorityb) x = ( 5 + 2 ) * ( 4 - 1)

    x = ( 5 + 2 ) * ( 4 - 1)

    x = 7 * 3

    x = 21

    Bracket has a highest

    priority!!

    40Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    41/106

    EXERCISEGet the answer for this question:

    a) 9 + 1 2

    b) 2 * 5 + 2 / 1

    c) 12 % 5

    d) 4 + (7 - 3) * 3

    e) ( 2 * 4 ) * 2 ( 6 + 2 ) / 8

    41Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    42/106

    RELATIONALOPERATOR

    42Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    43/106

    Relational Operation To compare 2 operator. Same data type, i.e. integer, character or string.

    The result is eitherTRUEorFALSE.

    Symbol Description

    > Greater than

    < Less than

    >= Greater or equal than

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    44/106

    LOGICALOPERATOR

    44Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    45/106

    Logical Operator To test some operation

    Have 3 symbol:

    Symbol Description

    && AND

    || OR

    ! NOT

    45Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    46/106

    Logical OperatorBase on TRUTH TABLE.

    P Q P && Q P || Q

    FALSE FALSE FALSE FALSE

    FALSE TRUE FALSE TRUE

    TRUE FALSE FALSE TRUE

    TRUE TRUE TRUE TRUE

    46Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    47/106

    Logical OperatorExample:

    Given a=3 and b=5;

    a) x = (a > 0) && (b > 0)The x value is TRUEbecause 3 is greater

    than 0 AND 5 is greater than 0 as well.

    b) x = ( a < b ) && ( b == 0 )The x value is FALSE . Although 3 is less

    than 5, but 5 is not equal with zero.

    47Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    48/106

    EXERCISEFind x value either TRUE or FALSE from the

    following equation:

    Given a=2 and b=4;

    a) x = ( a != 0 ) | | ( b != 0 )

    b) x = ( a == b ) | | (b==0)

    c) x = ! ( a == b )

    d) x = ! ( a < b)

    48Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    49/106

    Another Example:a = ! ( 2 > 5) | | 6 + 3 >= 4 3;

    ! (FALSE) | | (9 >= 1 )

    TRUE | | TRUE

    a = TRUE

    49Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    50/106

    INCREMENTANDDECREMENT OPERATOR

    50Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    51/106

    Increment And DecrementOperator Sometimes, we need to increment or

    decrement a single value in programming.

    The value is as below:

    Valid for variables only i.e. a++, b++, a--, b--,

    x++, y-- and invalid for constant

    i.e. 5-- or 9++.

    Symbol Operator

    ++ Add 1

    -- Minus 1

    51Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    52/106

    Increment And DecrementOperator The ++ and -- can be put before or

    after variables.

    i.e. we can write a++ or ++a, b-- or --b.

    52Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    53/106

    ExampleGiven; a = 3

    b = 5

    a++

    a value is 4a-- a value is 2

    b++ b value is 6

    b-- b value is 4

    53Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    54/106

    EXERCISEGiven; x = 8

    y = 6

    Find value for:

    1. x++2. x--

    3. y++

    4. y5. ++x

    6. --y

    54Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    55/106

    3.3 Apply Program ControlStructures

    55Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    56/106

    Logical Structures 3 types of program control structures:

    i. Sequence

    ii. Selection

    iii. Repetition

    56Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    57/106

    SEQUENCE

    57Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    58/106

    Sequence Command set which is execute line by line. Follows logic flow.

    Example:

    You need to develop the program which canread student name and count their total mark

    for one semester. Then the program can print

    the marks.

    Formula:

    Total marks = continuous evaluation + final evaluation

    58Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    59/106

    Answer : Algorithm1. Read student name

    2. Read continuous evaluation marks

    3. Read final evaluation marks4. Count total marks by add continuous

    evaluation and final evaluation

    5. Print student name and their total marks.

    59Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    60/106

    Answer: Pseudo CodeBegin

    Read student name, continuous evaluation

    marks, final evaluation marks

    Count total marks=

    continuous evaluation + final evaluation

    Print student name and total marks

    End

    60Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    61/106

    Answer : Flow ChartStart

    Read student name,

    continuous evaluation,

    final evaluation

    Count

    Total marks = continuous

    evaluation + final evaluation

    Print students name, totalmarks

    End

    61Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    62/106

    EXERCISE.. Develop a program which can count a

    room wide.

    Wide = Length x Width

    62Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    63/106

    SELECTION

    63Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    64/106

    Selection The expression which result on TRUE or

    FALSE.

    Consists 2 keywords: if

    else

    64Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    65/106

    Selection The selection control structure can be

    categorized into 4 groups :

    1. ifstatement

    2. if-else statement

    3. if-else statement (nested)

    4. Switch statement

    65Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    66/106

    IF Statement The IF statement will perform an action if

    the condition is true and ignorethe

    action if the condition is false.

    66Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    67/106

    Example : IF Statementif speed > 110 print penalty

    67Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    68/106

    Answer : Algorithm1. Read speed

    2. If speed more than 110, print penalty

    68Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    69/106

    Answer: Pseudo CodeBegin

    Read speed

    if speed> 110, print penaltyend if

    End

    69Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    70/106

    Answer: Flow ChartBegin

    Read speed

    Speed

    > 110

    True

    End

    False

    Print

    Penalty

    70Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    71/106

    EXERCISE.. If marks < 40 print fail.

    71Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    72/106

    IF-ELSE Statement The IF statement will perform an action if the

    condition is true and perform another action if

    the condition is false.

    72Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    73/106

    Example: if-else statementIf speed > 110 print penalty

    Else print no penalty

    73Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    74/106

    Answer : Algorithm1. Read speed

    2. If speed more than 110, print penalty

    3. Else, print no penalty

    74Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    75/106

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    76/106

    Answer : Flow ChartStart

    Read speed

    Speed

    > 110

    TrueFalse

    End

    Print

    Penalty

    Print No

    Penalty

    76Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    77/106

    EXERCISE..If marks < 40 print fail

    Else print pass

    77Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    78/106

    IF-ELSE Statement(Nested) The IF-ELSE nested statement is a condition

    when we have an if in another if body.

    We use this control structure if we have many

    selection to be handle with.

    Syntax:

    if .

    else if

    else

    78Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    79/106

    Example : If-Else NestedStatementCreate a program that can recognize and print

    the price base on user prompt code. If user

    enter a non-available value, the program will

    print Code Not Recognize.

    Code Price (RM)

    1 2.00

    2 4.00

    3 6.00

    4 8.00

    79Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    80/106

    Example: Algorithm1.Read code

    2. If code equal to one, then print RM2.00

    3. Else if code equal to 2, then print RM4.00

    4. Else if code equal to 3, then print RM6.00

    5. Else if code equal to 4, then print RM8.00

    6. Else print Code Not Recognize

    80Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    81/106

    Example: Pseudo CodeBeginRead code

    i fcode = 1

    print RM2.00

    else ifcode = 2

    print RM4.00

    else ifcode = 3

    print RM6.00

    else ifcode = 4

    print RM8.00else

    Print Code Not Recognized

    End

    81Norzimah Che Hassan Example: Flow Chart

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    82/106

    Start

    Read

    code

    Code

    =1

    Code

    =2

    Code

    =3

    T

    F

    T

    F

    T

    Code

    =4

    F

    T

    Print Code

    Not

    Recognized

    F

    End

    Print

    RM2.00

    Print

    RM4.00

    Print

    RM6.00

    Print

    RM8.00

    82Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    83/106

    EXERCISE..Develop a program which can determinea driver expertise by count their training

    day.Training day Expertise

    0 None

    1 - 3 Weak

    4 - 10 Average

    >10 Expert

    83Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    84/106

    REPETITION

    84Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    85/106

    Repetition Using loop structure.

    3 types of loop:

    While Do .. While

    For

    85Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    86/106

    While Loop Repeat as pre-condition is true

    while (condition)

    {

    loop body

    }

    86Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    87/106

    While Loop The while construct consists of a block of code

    and a condition.

    The condition is evaluated, and if the condition

    is true, the code within the block is executed. This repeats until the condition becomes false.

    Because while loops check the conditionbefore the block is executed, the control

    structure is often also known as a pre-testloop.

    87Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    88/106

    While General

    Condition

    Loop body

    True

    False

    88Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    89/106

    While Example Create a program that can print the value

    which is less than 5.

    89Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    90/106

    While Pseudo CodeBegin

    Read value= 0

    while value < 5Print value

    value=value+1

    end while

    End

    90Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    91/106

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    92/106

    While Source Code

    92Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    93/106

    Do-While Loop Repeat as post-condition is true

    do

    {

    loop body}

    while (condition)

    93Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    94/106

    Do-While Loop The do whileconstruct consists of a block of

    code and a condition.

    First, the code within the block is executed, and

    then the condition is evaluated. If the condition is true the code within the block is

    executed again. This repeats until the conditionbecomes false.

    Because do while loops check the condition afterthe block is executed, the control structure isoften also known as a post-test loop.

    94Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    95/106

    Do-While General

    ConditionLoop body

    True

    False

    95Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    96/106

    Do-While Example Create a program that can print the value

    which is less than 5.

    96Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    97/106

    Do-While Pseudo codeBegin

    Read value= 0

    repeatPrint value

    value=value+1

    until value < 5

    End

    97Norzimah Che Hassan

    Do-While Flow Chart

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    98/106

    Value < 5

    ?

    Start

    Value = 0

    End

    Print value

    True

    False

    Value =

    Value+1

    98Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    99/106

    Do-While Source Code

    99Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    100/106

    For loop Suitable if we know the number of iterations.

    expr1 is executed only once before looping.

    expr2 is a Boolean expression. If not given, it is

    assumed to be true. If expr2 is false, the loop is terminated.

    After execution of the repeat section, expr3 isexecuted.

    for (expr1;expr2;expr3)

    {

    s1;

    s2 ;

    }

    100Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    101/106

    For loop The for loop is often distinguished by an

    explicit loop counter or loop variable.

    This allows the body of the for loop (the code

    that is being repeatedly executed) to know

    about the sequencing of each iteration.

    for loops are also typically used when the

    number of iterations is known before enteringthe loop.

    101Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    102/106

    For loop

    for ( , , )

    {

    printf( );

    }

    12

    5

    3 4

    102Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    103/106

    For Example Create a program that can print the value

    which is less than 5.

    103Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    104/106

    For Pseudo codeBegin

    Read value

    For value < 5

    Print value

    value=value+1

    End for

    End

    104Norzimah Che Hassan

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    105/106

    For Example

  • 7/28/2019 Chapter 3 - Fundamentals of Programming Language

    106/106