Pemrograman Dasar Control Flow Statements: Decision Making or Selection PTIIK - UB 1.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Control Structures.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
The if-else Statements
Control Flow Statements: Repetition/Looping
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Fundamental of C programming
Control Structures Selections Repetitions/iterations
Decisions If statements in C.
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Weekly Attendance by Class w/e 6 th September 2013.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Chubaka Producciones Presenta :.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Chapter 6 Control Structures.
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chicas, este calendario si es pa' nosotras !!!!!.
The switch statement: an N-way selection statement.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
DAT602 Database Application Development Lecture 5 JAVA Review.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Control statements Mostafa Abdallah
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Java Language Basics.
The switch Statement, and Introduction to Looping
Decisions Chapter 4.
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
Starting JavaProgramming
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
Professor Jodi Neely-Ritz CGS3460
McDonald’s Kalender 2009.
Problem Gambling Clicks to Opgr.org
2300 (11PM) September 21 Blue line is meridian..
Conditionals.
McDonald’s calendar 2007.
Program Flow.
Teacher name August phone: Enter text here.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
February 2007 Note: Source:.
McDonald’s calendar 2007.
Habitat Changes and Fish Migration
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Presentation transcript:

Pemrograman Dasar Control Flow Statements: Decision Making or Selection PTIIK - UB 1

Statements  Control flow statements regulate the order in which statements get executed.  Kinds of control flow statements:  Decision making:  if-then statement  if-then-else statement,  switch-case statement  Repetition/looping:  while statement  for statement  do-while statement  Branching statement:  break statement  continue statement  return statement 2

if-then statements  The most basic of all the control flow statements.  Tells your program to execute a certain section of code only if a particular test evaluates to true. if (isMoving){ // the "if" clause currentSpeed--; // the "then" clause } // or if (isMoving) // the "if" clause currentSpeed--; // the "then" clause 3

 Syntax : if (boolean_expression)statement; or if (boolean_expression) { statement1; statement2; …… } If the boolean_expression is true, then the statement or block statements are executed. 4 Block statement if-then statements

5 Eks.boolean Statement;... true false Statements before if Statements after if Boolean expression Statement;... Statement;... if-then statements

Syntax : if (boolean_expression) statement_01; else statement_02; atau if (boolean_expression) { statement_01; …… } else { statement_02; …... } If boolean_expression evaluates to true, then statement_01 or block statement_01 are executed. But if boolean_expression evaluates to false, then statement_02 or block statement_02 are executed. 6 Block statement_01 Block statement_02 if-then-else statements

7 Statements before if Statements after if Eks.boolean Statement_01; Statement_02;... true false Statement_11; Statement_12;... Boolean expression Statement_01; Statement_02;... Statement_01; Statement_02;... Statement_11; Statement_12;... Statement_11; Statement_12;... if-then-else statements

 if-then-else constructs may have more than one conditions to evaluate.  Example: statement_01, statement_02, and statement_03 are of the same level if (boolean_expression) { statement_01; } else if (boolean_expression) { statement_02; } else { statement_03; } 8 if-then-else statements

 if-then-else can be nested  Example: statement_01 and statement_03 are of the same level, but they are of the different level from statement_02a and statement_02b if (boolean_expression) { statement_01; } else if (boolean_expression) { if (boolean_expression) statement_02a; else statement_02b; } else { statement_03; } 9 if-then-else statements

 Example: 10 if(n > 0) if(a > b) z = a; else z = b; if(n > 0) if(a > b) z = a; else z = b; Keyword else berpasangan dengan if yang mana ? Perbaiki penulisan potongan program di atas agar terbaca jelas algoritmanya! Decision Making or Selection

11 Pernyataan if

 switch statements can be used as an alternative to if-then- else statements.  Syntax: switch (expression) { case constant1: statements1; [break;] case constant2: statements2; [break;] … [default : statements;] }  expression must be of type c har, byte, short, or int, or a corresponding wrapper class, an enum type, or String. 12 switch statements

13 switch statements case b case a case z case a action(s) case b action(s) case z action(s) break default action(s) true false case b case a case z case a action(s) case b action(s) case z action(s) break default action(s)

switch statements int month = 8; switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12: System.out.println("December"); break; default: System.out.println("Invalid month.");break; } 14

switch statements float bil1, bil2; String op; Scanner input = new Scanner( System.in ); System.out.print("Enter a number: "); bil1 = input.nextFloat(); System.out.print("Enter an operator: "); op = input.next(); System.out.print("Enter a number: "); bil2 = input.nextFloat(); switch(op){ case "+": System.out.println("Result: "+ (bil1 + bil2)); break; case "-": System.out.println("Result: "+ (bil1 - bil2)); break; case "*": System.out.println("Result: "+ (bil1 * bil2)); break; case "/": System.out.println("Result: "+ (bil1 / bil2)); break; default: System.out.println("Unknown operator"); } 15

16 Pernyataan switch-case