Modul Dasar Pemrograman [TM10]

download Modul Dasar Pemrograman [TM10]

of 8

Transcript of Modul Dasar Pemrograman [TM10]

  • 8/10/2019 Modul Dasar Pemrograman [TM10]

    1/8

  • 8/10/2019 Modul Dasar Pemrograman [TM10]

    2/8

    Jump statements

    Break statement

    Fungsi breakadalah untuk meninggalkan sebuah loop ketika kondisi untuk akhiri

    perulangan tidak terpenuhi. Break dapat digunakan untuk mengakhiri sebuah infinite loop

    atau mengakhiri dengan tidak normal.

    Contoh 1

    Program untuk melakukan perhitungan mundur . Jika nilai hitungan sudah sampai dengan !maka proses dihentikan.

    Program

    #include

    void main (){ int n; or (n!1"; n>"; n)

    {cout

  • 8/10/2019 Modul Dasar Pemrograman [TM10]

    3/8

    Contoh

    Program untuk menghitung total nilai #ang diinputkan! dimana nilai #ang diinputkan adalah

    maksimum ' nilai. Jika total nilai #ang diinput ('''! maka proses dihentikan.

    Program

    #include

    void main (){ int i%n%t; t!"; or (i!1;i

    cout

  • 8/10/2019 Modul Dasar Pemrograman [TM10]

    4/8

    Continuemen#ebabkan program di-skip pada bagian tertentu.

    Contoh

    Program untuk men-skip angka ) pada sebuah perhitungan mundur:

    3he goto statement

    goto allows to make an absolute *ump to another point in the program. +ou should use this

    feature with %aution sin%e its e,e%ution %auses an un%onditional *ump ignoring an# t#pe of

    nesting limitations.

    he destination point is identified b# a label! whi%h is then used as an argument for the goto

    statement. label is made of a /alid identifier followed b# a %olon 0:1.

    2enerall# speaking! this instru%tion has no %on%rete use in stru%tured or ob*e%t oriented

    programming aside from those that low-le/el programming fans ma# find for it. For e,ample!

    here is our %ountdown loop using goto:

    (#") "

    DasarPemrogramanPusat*a!an A+ar dan eLearning

    Desi"ama#anti http:$$www.mer%ubuana.a%.id

    3

    4

    )

    5

    6

    7

    8

    '

    3

    $$ %ontinue loop e,ample

    9in%lude iostream(

    usingnamespa%estd;

    intmain 01

    ;

    ?

    %out >F@"An>;

    return';

    ?

    '! 8! 7! 6! 5! 4! ! 3! ! F@"A

  • 8/10/2019 Modul Dasar Pemrograman [TM10]

    5/8

    3

    4

    )

    5

    6

    7

    8

    '

    3

    4

    )

    $$ goto loop e,ample

    9in%lude iostream(

    usingnamespa%estd;

    intmain 01

    F@"An>;

    return';

    ?

    '! 8! 7! 6! 5! )! 4! ! 3! ! F@"A

    3he e4it unction

    e,it is a fun%tion defined in the %stdlib librar#.

    he purpose of e,it is to terminate the %urrent program with a spe%ifi% e,it %ode. @ts protot#pe

    is:

    /oide,it 0inte,it%ode1;

    he e,it%ode is used b# some operating s#stems and ma# be used b# %alling programs. B#

    %on/ention! an e,it %ode of ' means that the program finished normall# and an# other /alue

    means that some error or une,pe%ted results happened.

    (#") #

    DasarPemrogramanPusat*a!an A+ar dan eLearning

    Desi"ama#anti http:$$www.mer%ubuana.a%.id

  • 8/10/2019 Modul Dasar Pemrograman [TM10]

    6/8

    3he selective structure sitch.

    he s#nta, of the swit%h statement is a bit pe%uliar. @ts ob*e%ti/e is to %he%k se/eral possible

    %onstant /alues for an e,pression. omething similar to what we did at the beginning of this

    se%tion with the %on%atenation of se/eralif and else if instru%tions. @ts form is the following:

    swit%h 0e,pression1