Relational Expression return Boolean results. operands must be of numerical types. == equal to != not equal to < less than > greater than <= less than.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Control Structures Corresponds with Chapters 3 and 4.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
1 Repetition structures Overview while statement for statement do while statement.
1 CS 177 Week 4 Recitation Slides Conditionals. 2 Announcements Project 1 is due on Feb. 7th 9pm (extended) Project 2 will be posted today (Feb. 5th)
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Looping Yong Choi School of Business CSU, Bakersfield.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Reading Input -- the Keyboard Class The Keyboard class is NOT part of the Java standard class library The Keyboard class is in package cs1. It provides.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
JAVA Control Statement.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 5 Loops.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Summer 2008 Q1Version 1.21 Introduction to Programming Session 2 Point Park University CMPS 322 Summer 2008 Q1.
By Mr. Muhammad Pervez Akhtar
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Programming Principles Operators and Expressions.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Chapter 4: Control Structures I
INTERMEDIATE PROGRAMMING USING JAVA
Chapter 4: Control Structures I
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Control Structures.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
SELECTION STATEMENTS (1)
Chapter 5 Control Statements
CS 177 Week 4 Recitation Slides
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 4: Control Structures I
Java so far Week 7.
Lab5 PROGRAMMING 1 Loop chapter4.
PROGRAM FLOWCHART Iteration Statements.
Control Statements:.
Presentation transcript:

Relational Expression return Boolean results. operands must be of numerical types. == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to

Logical Expression (!a) negation (a || b) or (a && b) and

Distributive laws Original Expression: (p && q) || (p && r) (p || q) && (p || r) Equivalent terms p && (q || r) p || (q && r)

DeMorgan’s Laws Original Expressions !(p && q) !(p || q) Equivalent Terms (!p) || (!q) (!p) && (!q)

Conditional Expression condition ? exp1 : exp2 Example: (x >= 0) ? x : -x (a >= b) ? a : b

Postfix/Prefix Operators x (before) expr x(after) Postfix increment x Postfix decrement x— Prefix increment ++x Prefix decrement --x 1 0 0

Sequence Statements are executed top to bottom. S1; S2; Example: temp = x*x + y*y; d = Math.sqrt(temp); x = 5; y = 11; z = x + y; System.out.println(z);

Block a sequence of statements and local variable declarations enclosed by braces { and } { S1; S2;...; Sn; } Example: { double temp, d; temp = x*x + y*y; d = Math.sqrt(temp); }

if Statement Execute a block of code (Action) if a condition is true. The block of code is executed at most once. if (condition) { statements; } Example: if (x % 2 == 0) { System.out.println("The number is even."); }

If-Else Statement. if (condition) { statement1; } else { statement2; } if (condition1) { statement1; } else if (condition2) { statement2; } else if (condition3) { statement3; } else { statement4; }

Example: if.. else if.. else String sDay; if (day == Calendar.SUNDAY) { sDay = "Sunday"; } else if (day == Calendar.MONDAY) { sDay = "Monday"; } else if (day == Calendar.TUESDAY) { sDay = "Tuesday"; } else if (day == Calendar.WEDNESDAY) { sDay = "Wednesday"; } else if (day == Calendar.THURSDAY) { sDay = "Thursday"; } else if (day == Calendar.FRIDAY) { sDay = "Friday"; } else if (day == Calendar.SATURDAY) { sDay = "Saturday"; } else { sDay = "Unknown"; }

Switch Statement switch (expression) { case value1: statement1; break; case value2: statement2; break; case value3: statement3; break; default: statement4; }

Example: Day3.java String sDay; switch (day) { case Calendar.SUNDAY: sDay = "Sunday"; break; case Calendar.MONDAY: sDay = "Monday"; break; case Calendar.TUESDAY: sDay = "Tuesday"; break; case Calendar.WEDNESDAY: sDay = "Wednesday"; break; case Calendar.THURSDAY: sDay = "Thursday"; break; case Calendar.FRIDAY: sDay = "Friday"; break; case Calendar.SATURDAY: sDay = "Saturday"; break; default: sDay = "Unknown"; }

While Loop Repeat a block of code while a condition is true. There is no limit to the number of times that the block can be executed. while (condition) { statements; } When the body of a while statement, or of a for statement consists of a single statement, no braces are necessary.

Examples: While Loop int i = 1; while (i <= 20) { System.out.println(i); i++; } // int i = 20; while (i >= 0) { System.out.println(i); i--; }

For Loop for (initialization; condition; incrementation) { statement(s); }

Examples: For Loop for (int i = 1; i <= 20; i++) { System.out.println(i); } // for (int i = 20; i >= 0; i--) { System.out.println(i); }

Do-While Loop do { statements; } while (condition);

Examples: Do-While Loop int i = 1; do { System.out.println(i); i++; } while (i <= 20) // int i = 20; do { System.out.println(i); i--; } while (i >= 0);

Break and Continue A break statement in a loop terminates the loop. A continue statement in a loop terminates the current iteration of the loop, and continue with the next iteration of the loop.

Examples: Break and Continue int i = 0; while (i <= 20) { i++; if (i % 5 == 0) break; System.out.println(i); } // int i = 0; while (i <= 20) { i++; if (i % 5 == 0) continue; System.out.println(i); }

Static Methods Some of the methods of a class can be declared static. These methods can be called directly without instances of the class. ClassName.methodName(parameters,...)

Reading Input -- the Keyboard Class The Keyboard class is NOT part of the Java standard class library The Keyboard class is in package cs1. It provides several static methods for reading particular types of data from standard input. public static String readString() public static String readWord() public static boolean readBoolean() public static char readChar() public static int readInt() public static long readLong() public static float readFloat() public static double readDouble()

Example: AmountDue1 import cs1.Keyboard; public class AmountDue1 { public static void main(String[] args) { int qty; double price, amountDue; System.out.print(“Enter quantity purchased: "); qty = Keyboard.readInt(); System.out.print(“Enter the unit price: "); price = Keyboard.readDouble(); amountDue = price * qty; System.out.println(“Amount due: " + amountDue); }

Install Keyboard Copy the cs1.jar file Paste it in the C:\jdk1.3\jre\lib\ext directory If the above doesn’t work by itself, then also paste it in C:\Program Files\JavaSoft\JRE\1.3\lib\ext

Install Keyboard – Method 2 Create a cs1 subdirectory to the directory containing your java programs Copy Keyboard.java into the cs1 directory Homework directory | +-Java source files | +- cs1 subdirectory | +- Keyboard.java

Install Keyboard – Method 3 Copy Keyboard.java into the same directory as your programs.

Reading Input –JOptionPane import javax.swing.JOptionPane; public class AmountDue2 { public static void main(String[] args) { int qty; double price, amountDue; String temp; temp = JOptionPane.showInputDialog ("Please enter the quantity purchased: "); qty = Integer.parseInt(temp); temp = JOptionPane.showInputDialog ("Please enter the unit price: "); price = Double.parseDouble(temp); amountDue = price * qty; JOptionPane.showMessageDialog (null,"Amount due: " + amountDue); }

Reading Input – java.io import java.io.*; public class AmountDue3 { public static void main(String[] args) throws IOException { int qty; double price, amountDue; BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); System.out.print("Please enter the quantity purchased: "); qty = Integer.parseInt(br.readLine()); System.out.print("Please enter the unit price: "); price = Double.parseDouble(br.readLine()); amountDue = price * qty; System.out.println(“Amount due: " + amountDue); }

NumberFormat Class x = /3.0; System.out.println(x); // Printing a number with System.out.print(x) will normally print the maximum number of non-zero digits. The NumberFormat class in the java.text package can format Numbers Currency values Percentage values

Number Format import java.text.*; public class FormattedOutput{ public static void main(String[] args) { int qty; double price, amountDue; double x = /3.0; System.out.println("Default output is \t" + x); // NumberFormat num_format = NumberFormat.getNumberInstance(); String s = num_format.format(x); System.out.println("Number format is \t" + s);// 3, NumberFormat cur_format = NumberFormat.getCurrencyInstance(); s = cur_format.format(x); System.out.println("Currency format is \t" + s);//$3, NumberFormat per_format = NumberFormat.getPercentInstance(); s = per_format.format(x); System.out.println("Percent format is \t" + s); // 333,333% }

Other NumberFormat methods setMaximumIntegerDigits(int digits) setMaximumFractionDigits(int digits) setMinimumIntegerDigits(int digits) setMinimumFractionDigits(int digits)