3 walid

download 3 walid

of 13

Transcript of 3 walid

  • 7/31/2019 3 walid

    1/13

    Computing For Engineers

    Control Structures

    COMPUTING FOR ENGINEERS (EE209)

    Laboratory report

    LAB ACTIVITY 3: CONTROL STRUCTURES

    Walid Nabil Osman Ali

    ID #: 1000924355

    SCHOOL OF ENGINEERING

    FACULTY OF ENGINEERING, ARCHITECTURE & BUILT

    ENVIRONMENT

    16/2/2012

  • 7/31/2019 3 walid

    2/13

    Computing For Engineers

    Control Structures

    Computing For Engineers

    Lab Activity3: Control Structures

    Objectives:

    To be familiar with one of the control structures type: selection statements.Learning Outcomes: At the end of the session, the students are able:-

    To understand and use the control structures in programming. To understand the concept ofif, ifelse, ifelse if - else and switch - case statement.

    Apparatus:

    PC with C++ compilerSample Code 1: If statement

    The program code shown below reads an integer and determines and prints whether it is odd oreven.(Hint: Use the modulus operator. An even number is a multiple of two. Any multiple of two

    leaves a remainder of zero when divided by 2.)

    #include

    using namespace std;

    int main()

    {

    int num;

    cout

  • 7/31/2019 3 walid

    3/13

    Computing For Engineers

    Control Structures

    Sample Code 2: Ifelse statement

    int main()

    {

    int num;

    cout

  • 7/31/2019 3 walid

    4/13

    Computing For Engineers

    Control Structures

    else

    {

    cout

  • 7/31/2019 3 walid

    5/13

    Computing For Engineers

    Control Structures

    The program code shown below uses the switch case using characters.int main()

    {

    Char y;

    cout

  • 7/31/2019 3 walid

    6/13

    Computing For Engineers

    Control Structures

    Sample Code 3 : Switch case

    # include

    using namespace std;

    int main ()

    {

    char mood;

    cout

  • 7/31/2019 3 walid

    7/13

    Computing For Engineers

    Control Structures

    Sample Code 4a: Ifelse statement

    # include

    using namespace std;

    int main ( )

    {

    int y = 0;

    cout y;

    cout

  • 7/31/2019 3 walid

    8/13

    Computing For Engineers

    Control Structures

    Sample Code 4b : Ifelse-if - else

    # include

    using namespace std;

    int main ()

    {

    char y;

    cout y;

    cout

  • 7/31/2019 3 walid

    9/13

    Computing For Engineers

    Control Structures

    Flowchart 1

    Yes

    No

    Yes

    No

    Num

    %2 == 0

    Start

    Enter a number

    Display number is even

    Display number is odd

    End

    Num

    %2! = 0

  • 7/31/2019 3 walid

    10/13

    Computing For Engineers

    Control Structures

    Flowchart 2

    Yes

    No

    Start

    Enter a number

    Number is less and equal to

    10

    Number is greater than 10

    End

    Num

  • 7/31/2019 3 walid

    11/13

    Computing For Engineers

    Control Structures

    Flowchart 3

    Yes

    No

    Yes

    No

    Y y

    Start

    Enter y for yes, or N for No

    Are you happy?

    I am happy too!

    N n

    Cheer up!

    You entered a wrong

    character.

    End

  • 7/31/2019 3 walid

    12/13

    Computing For Engineers

    Control Structures

    Flowchart 4a

    Yes

    No

    Yes

    No

    Yes

    No

    Start

    Enter a character

    A a

    B b

    C c

    Default

    End

    Range is: 100 - 70

    Range is: 69 - 40

    Range is: 39 0

  • 7/31/2019 3 walid

    13/13

    Computing For Engineers

    Control Structures

    Flowchart 4b

    Yes

    No

    Yes

    No

    Yes

    No

    Start

    Enter number

    Case 0

    Case 1

    Case 5

    Default

    End

    You entered zero.

    You entered one.

    You entered five.