PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
4 Control Statements: Part 1.
Objectives Knows and understand about another kind of flowchart’s symbols (looping) Have ability to create structured solution on simple problem(s) Have.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Making Decisions and Working With Strings
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Programming Language Concepts
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
3 Logic The Study of What’s True or False or Somewhere in Between.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Control Structures Selections Repetitions/iterations
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Decisions If statements in C.
Chapter Three Arithmetic Expressions and Assignment Statements
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Types of selection structures
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
PSSA Preparation.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 4 Making Decisions
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Flow of Control.
Chapter 3 Branching Statements
Boolean Expressions and If
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Controlling Program Flow
Presentation transcript:

PERCABANGAN Bagaimana Jika YA? Bagaimana Jika TIDAK? Bagaimana Jika BENAR? Bagaimana jika SALAH?

2 Buat program java untuk menghitung luas segi tiga. Tes program dengan input: Alas = 20, tinggi = 10 Alas = 10, tinggi = -30 Catch-up Exercise

3 Flow of Control Cara kerja komputer memproses perintah dalam sebuah program: –Sequentially; baris per baris, –Conditionally; dengan perintah percabangan –Repetitively; menggunakan perintah perulangan Program yang selama ini dibuat adalah sekuensial. Dalam slide ini akan dibahas jenis Kondisional.

4 Perintah Seleksi Selection or branching statement menentukan the action that should be taken by the computer. Titik dimana a decision has to be made as to jalur yang akan diambil. IF yes no no

5 Perintah if-else In Java, we can write a selection statement using if-else Syntax: if else Jika ekspresi boolean bernilai true, maka statement_1 yang akan dieksekusi. Jika ekspresi boolean bernilai false, maka statement_2 yang akan dieksekusi.

6 Example 1 Perhatikan fragmen program berikut ini: int age; Scanner keyboard = new Scanner (System.in); age = keyboard.nextInt(); if (age < 21) System.out.println("too young to vote"); else System.out.println("register as voter"); What is the output if the user enters –16 –23 –21

7 count = 4; total = 5; if (count < 3) total = 0; else total = total + count; System.out.println(total); Example 2

8 Compound Statements To include multiple statements in a branch, enclose the statements in braces. if (count < 3) { total = 0; count = 0; }

9 Menghilangkan Bagian else Jika bagian else dihapus and the expression after the if is false, no action occurs. syntax if (Boolean_Expression) Statement example weight = 50; ideal = 60; if (weight > ideal) caloriesPerDay -= 500; //kalori = kalori-500;

10 Ekspresi Boolean When processing a selection statement such as an if statement, the computer must evaluate a boolean expression. Recall the boolean primitive data type that holds the value true or false. A boolean expression is sebuah ekspresi yang menghasilkan nilai true or false. Boolean expressions biasanya memakai operator perbandingan (also known as relational operators)

11 Operators Perbandingan

12 Exercise [Latihan] 1.Write a Java program that asks the user for a score and displays "You pass" if the score is greater than or equal to 50 and "You fail" otherwise. 2.Write a Java program that asks the user for a grade and displays "Wow!" if the grade is 'A' and "Try harder next time" otherwise.

13 Operator Logika Formula ekspresi boolean seringkali menjadi kompleks. Modifikasilah program sebelumnya, so that it will display "You pass" if the assignment score is greater than or equal to 50 DAN the examination score is greater than or equal to 60. We will need to use operator logika for more complex boolean expressions: –&& (and) –|| (or) –! (not)

14 Truth Table/ Tabel Kebenaran Refer to the following truth table for logical operators, assuming P and Q are boolean expressions: PQP && QP || Q!P!Q true false truefalse truefalse truefalsetrue false true

15 Presedensi Operator We have looked at operator Arithmetic, operator Relational and Operator Logical. Jika operators tersebut dipakai bersama dalam sebuah expression, they must be evaluated menurut the precedence rules: –Parentheses (or brackets) –Type cast(type) –Logical not! –Arithmetic * / %* / % –Arithmetic + – + – –Relational operators> >= < <= == != –Logical AND and OR&& ||

16 Ekspresi Boolean Majemuk Boolean expressions can be combined using the “or” ( || ) operator. example if ((quantity > 5) || (cost < 10))... syntax ( Sub_Expression_1) || (Sub_Expression_2)

17 Compound Statements/ Perintah Majemuk When a list of statements is enclosed in braces ( {} ), they form a single compound statement. syntax { Statement_1; Statement_2; … }

18 Exercise Evaluate the following expressions –(10 7) –!true && false –!(5==8) –true || !false –!(3 > 5 && < 3 * 2) || 3 != 3

19 The if Statement We can also use if without else: Syntax: –if If more than one statement is to be executed, they must be enclosed in curly brackets: if ((rich =='y') || (famous == 'y')) { System.out.println("I will be your friend"); System.out.println("You are either rich or famous"); }

20 Indentation Important to improve the clarity of your program. Show which statements are part of the if. However, remember that indents are ignored by the compiler. What is the output of the following program fragment for num = 8? num = 15? if (num < 10) num = num + 10; System.out.println("num is " + num); System.out.println("Finished!");

21 Nested [bersarang] if We can nest an if (or if-else) statement inside another if (or if-else) statement. Consider the following: –Jika seorang mahasiswa adalah angkatan tahun 1, maka nilai lulusnya adalah 50, jika mahasiswa angkatan tahun 2, maka nilai lulusnya adalah 60 dan untuk angkatan tahun 3, maka nilai lulusnya adalah 40. Write the code fragment to represent this. Remember, every else must have an if (but not necessary vice-versa)

22 Example if (year == 1) if (mark >= 50) System.out.println("You pass"); else System.out.println("You fail"); else if (year == 2) if (mark >= 60) System.out.println("You pass"); else System.out.println("You fail"); else if (year == 3) if (mark >= 40) System.out.println("You pass"); else System.out.println("You fail"); else System.out.println("Cannot be determined"); if-else pairs nested if-else

23 Exercise Write a Java program that will ask the user for a number and display the square root of the number if it is positive. –Use the Math.sqrt() method. Next modify the program so that if the user enters a negative number, an error message will be displayed.

24 Exercise Write a Java program that asks the user for his or her name and gender then displays "Hello Mr. xxx" or "Hello Ms. xxx" as appropriate. If an invalid value is entered for the gender, just return the name "xxx".

25 Nested Statements An if-else statement can contain any sort of statement within it. In particular, it can contain another if-else statement. –An if-else may be nested within the “if” part. –An if-else may be nested within the “else” part. –An if-else may be nested within both parts.

26 Nested Statements, cont. Each else is paired with the nearest unmatched if. If used properly, indentation communicates which if goes with which else. Braces can be used like parentheses to group statements.

27 Nested Statements, cont. syntax if (Boolean_Expression_1) if (Boolean_Expression_2) Statement_1) else Statement_2) else if (Boolean_Expression_3) Statement_3) else Statement_4);

28 Multibranch if-else Statements syntax if (Boolean_Expression_1) Statement_1 else if (Boolean_Expression_2) Statement_2 else if (Boolean_Expression_3) Statement_3 else if … else Default_Statement

29 Multibranch if-else Statements, cont. equivalent code

30 The switch Statement The switch statement is a mutltiway branch that makes a decision based on an integral (integer or character) expression. The switch statement begins with the keyword switch followed by an integral expression in parentheses and called the controlling expression.

31 The switch Statement, cont. A list of cases follows, enclosed in braces. Each case consists of the keyword case followed by –a constant called the case label –a colon –a list of statements. The list is searched for a case label matching the controlling expression.

32 Switch Statement The switch statement is a branching statement that allows multiple options. This is similar to using nested if-else statements but it is simpler and often clearer. switch (gender) { case 'm': case 'M': System.out.println("Hello, Mr. " + name); break; case 'f': case 'F': System.out.println("Hello, Ms. " + name); break; default: System.out.println("Hello, " + name); break; }

33 Syntax – switch switch (variable / expression) { case label1: statement; break; case label2: case label3: statement; break; case label4: case label5: statement; break; default: statement; break; } Must be of type byte, short, int or char ONLY Case label: must be constant value compatible type to the variable/ expression cannot be duplicated optional section to be executed if none of the case labels match the variable / expression If the value of the variable /expression matches the case label, the statements following the label will be executed until a break is reached.

34 Exercise Write a Java program that displays the following menu and allows the user to choose one of the options given. Then display the required nursery rhyme. Choose the rhyme: 1.winkle twinkle little star 2.Humpty Dumpty 3.London Bridge Your choice? 2 Humpty Dumpty sat on the wall Humpty Dumpty had a great fall

35 Java Character Set Java uses unicode to represents characters. Unicode uses 2 bytes (16 bits) and are thus capable of representing a total of different symbols, but only half of these are used. The first 256 unicode characters are the same as the ASCII characters. All characters (whether unicode or ASCII) are represented using numerical code. –The character '0' is represented by the numerical code of 48, '9'  57, 'A'  65, 'a'  97, etc. The expressions below are true: –'3' < '9' –'3' > 9 –'B' > '4' –'a' > 'A'

36 Exercise Write a program that asks the user to enter two grades, student1Grade and student2Grade. The grade must be a character from 'A' to 'F'. If the grade for either student is lower than 'C' then display 'Help required'. Then display whether student1 or student2 got the better grade. Use methods in your program Plan carefully!