logika dan algoritma, pertemuan 2

12
LOGICS AND ALGORITHMS by: Joz The 2 nd Subject Logical Operations 2-branch Conditional Structures Many-branch Conditional Structures

Upload: defraoi

Post on 23-Nov-2014

152 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Logika Dan Algoritma, Pertemuan 2

LOGICS AND ALGORITHMS

by: Joz

The 2nd Subject

Logical Operations ●2-branch Conditional Structures ●

Many-branch Conditional Structures ●

Page 2: Logika Dan Algoritma, Pertemuan 2

BOOLEAN TYPES

0FALSE

NO

1TRUEYES

Page 3: Logika Dan Algoritma, Pertemuan 2

LOGICAL OPERATIONS

notthe inverter !!!

operators

orone is enough !!!

andmandatory !!!

Page 4: Logika Dan Algoritma, Pertemuan 2

LOGICAL TABLE (THE TRUTH TABLE)

A B not A not B

FALSE

FALSE

TRUE

TRUE

FALSE

TRUE

FALSE

TRUE

TRUE

TRUE

FALSE

FALSE

TRUE

FALSE

TRUE

FALSE

A or B

FALSE

TRUE

TRUE

TRUE

A and B

FALSE

FALSE

FALSE

TRUE

Page 5: Logika Dan Algoritma, Pertemuan 2

2-BRANCH SELECTIONS (“IF” STRUCTURES)

CONDITION ?Y

N

IF CONDITION THEN ACTIONS_IF_TRUEEND IF

IF CONDITION THEN ACTIONS_IF_TRUEELSE ACTIONS_IF_FALSEEND IF

Page 6: Logika Dan Algoritma, Pertemuan 2

MANY-BRANCH SELECTIONS (“CASE” STRUCTURES)

CASESELECTOR

OF1

3

CASE SELECTOR OF VALUE_1: ACTIONS_1 VALUE_2: ACTIONS_2 ... VALUE_N: ACTIONS_NEND CASE

2 N

ELSECASE SELECTOR OF VALUE_1: ACTIONS_1 VALUE_2: ACTIONS_2 ... VALUE_N: ACTIONS_NELSE ACTIONS_ELSEEND CASE

Page 7: Logika Dan Algoritma, Pertemuan 2

EXAMPLE #1 – INPUT CONSTRAINTS

inputing the score between 0 and 100 BEGIN OUTPUT('PLEASE GIVE ME A SCORE 0-100 > ') INPUT(SCORE) IF SCORE ≥ 0 THEN IF SCORE ≤ 100 THEN OUTPUT('INPUT CORRECT!', CR) ELSE OUTPUT('INPUT OUT OF RANGE!', CR) END IF ELSE OUTPUT('INPUT OUT OF RANGE!', CR) END IFEND.

BEGIN OUTPUT('PLEASE GIVE ME A SCORE 0-100 > ') INPUT(SCORE) IF (SCORE ≥ 0) AND (SCORE ≤ 100) THEN OUTPUT('INPUT CORRECT!', CR) ELSE OUTPUT('INPUT OUT OF RANGE!', CR) END IFEND.

Page 8: Logika Dan Algoritma, Pertemuan 2

EXAMPLE #2 – GRADE YOURSELF!!!

grading to the STIKI’s policy BEGIN OUTPUT('PLEASE GIVE ME A SCORE 0-100 > ') INPUT(SCORE) IF (SCORE ≥ 0) AND (SCORE ≤ 100) THEN OUTPUT('YOU HAVE GOT ') IF SCORE ≥ 80 THEN OUTPUT('A', CR) ELSE IF SCORE ≥ 75 THEN OUTPUT('B+', CR) ELSE IF SCORE ≥ 70 THEN OUTPUT('B', CR) ELSE IF SCORE ≥ 61 THEN OUTPUT('C+', CR) ELSE IF SCORE ≥ 56 THEN OUTPUT('C', CR) ELSE IF SCORE ≥ 75 THEN OUTPUT('D', CR) ELSE OUTPUT('E', CR) END IF END IF END IF END IF END IF END IF ELSE OUTPUT('INPUT OUT OF RANGE!', CR) END IFEND.

Page 9: Logika Dan Algoritma, Pertemuan 2

EXAMPLE #3 – GRADE YOURSELF!!! (CASE VERSION)

grading to the STIKI’s policy BEGIN OUTPUT('PLEASE GIVE ME A SCORE 0-100 > ') INPUT(SCORE) IF (SCORE ≥ 0) AND (SCORE ≤ 100) THEN OUTPUT('YOU HAVE GOT ') CASE TRUNC(SCORE) OF 80..100: OUTPUT('A', CR) 75..79: OUTPUT('B+', CR) 70..74: OUTPUT('B', CR) 61..69: OUTPUT('C+', CR) 56..60: OUTPUT('C', CR) 40..55: OUTPUT('D', CR) ELSE OUTPUT('E', CR) END CASE ELSE OUTPUT('INPUT OUT OF RANGE!', CR) END IFEND.

Page 10: Logika Dan Algoritma, Pertemuan 2

EXAMPLE #4 – GRADE YOURSELF!!! (PURE CASE VERSION)

grading to the STIKI’s policy BEGIN OUTPUT('PLEASE GIVE ME A SCORE 0-100 > ') INPUT(SCORE) OUTPUT('YOU HAVE GOT ') CASE TRUNC(SCORE) OF 80..100: OUTPUT('A', CR) 75..79: OUTPUT('B+', CR) 70..74: OUTPUT('B', CR) 61..69: OUTPUT('C+', CR) 56..60: OUTPUT('C', CR) 40..55: OUTPUT('D', CR) 0..39: OUTPUT('E', CR) ELSE OUTPUT('YOUR INPUT OUT OF RANGE!', CR) END CASEEND.

Page 11: Logika Dan Algoritma, Pertemuan 2

ROUTINE TASKS

• Find out the area of a circle, filter the inputs as neccessary.• Find out the area of a triangle, filter the inputs as neccessary.• Find out the area of a rectangle, filter the inputs as neccessary.• Find out the area of a circle, a triangle, or a rectangle, accessed via a

menu, and filter the inputs as neccessary.• Find out the y-value from given x-value of

6253)( 23 xxxxf

• Find out the y-value from given x-value and a, b, c, and d of

dcxbxaxxf 23)(

Page 12: Logika Dan Algoritma, Pertemuan 2

WHAT’S NEXT?

repetitive statements (while, repeat, for), flow control, iteration, ...