Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.

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

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.
Java Basics (continued)
Numeric Values and Computations Today, we extend our programming capabilities by adding numeric values and computations –As introduced last time, we can.
Lecture 2: Static Methods, if statements, homework uploader.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 4 Introduction to Control Statements (Branching Statements) Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else.
Chapter 2: Using Data.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Fundamentals of Java Text by: Lambert and Osborne Slides by: Cestroni.
4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the arithmetic and concatenation operators to provide.
1 Operations Making Things Happen (Chap. 3) Expressions.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 4 Selection: the if-else and switch-case instructions.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
The Math Class Methods Utilizing the Important Math Operations of Java!
Lesson 4: Introduction to Control Statements
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
A: A: double “4” A: “34” 4.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Java Basics (continued) Ms. Pack AP Computer Science A.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Assignment and Arithmetic expressions
Factoring if/else code
SELECTION STATEMENTS (1)
Fundamentals of Java Lesson 4: Introduction to Control Statements
Chapter 2: Basic Elements of Java
Programming 2 Decision-Making.
Associativity and Prescedence
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Using C++ Arithmetic Operators and Control Structures
Fundamentals of Java Lesson 4: Introduction to Control Statements
Presentation transcript:

Lesson 4: Introduction to Control Statements

4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the arithmetic and concatenation operators to provide extended assignment operators. For example int a = 17; String s = “hi”; a += 3; // Equivalent to a = a + 3; a -= 3;// Equivalent to a = a – 3; a *= 3;// Equivalent to a = a * 3; a /= 3;// Equivalent to a = a / 3; a %= 3;// Equivalent to a = a % 3; s += “ there”; // Equivalent to s = s + “ there”;

4.1 Additional Operators –Extended assignment operators can have the following format. variable op= expression; which is equivalent to variable = variable op expression; –Note that there is no space between op and =. –The extended assignment operators and the standard assignment have the same precedence.

4.1 Additional Operators Increment and Decrement –Java includes increment (++) and decrement (--) operators that increase or decrease a variables value by one: int m = 7; double x = 6.4; m++; // Equivalent to m = m + 1; x--; // Equivalent to x = x – 1.0; –The precedence of the increment and decrement operators is the same as unary plus, unary minus, and cast.

4.2 Standard Classes and Methods Seven methods in the Math Class

4.2 Standard Classes and Methods –There are two methods called abs. They are distinguished from each other by the fact that one takes an integer and the other takes a double parameter. –Using the same name for two different methods is called overloading

4.2 Standard Classes and Methods The sqrt Method –This code segment illustrates the use of the sqrt method: // Given the area of a circle, compute its radius // Use the formula a =  r 2 radius = Math.sqrt (area / Math.PI); –Messages are usually sent to objects; however, if a method’s signature is labeled static, the message instead is sent to the method’s class.

4.2 Standard Classes and Methods The sqrt Method –Thus, to invoke the sqrt method, we send the sqrt message to the Math class. –In addition to methods, the Math class includes good approximations to several important constants. – Math.PI is an approximation for  accurate to about 17 decimal places.

for ( int i = 1; i<= 6; i++){ System.out.println(Math.random()); } The code above prints 6 random doubles in the range 0 <= number <1 for ( int i= 1; i<= 6; i++){ System.out.println((int) (Math.random( )*3)+1); } The code above prints 6 random integer numbers in the range 1-3. Use Math.random( ) to create random numbers. Math.random( ) returns a double value with a positive sign, greater than or equal to 0.0 and less than

Assignment #1 and 2 page 92 #1 and 2 page 95

4.3 A Shortcut for Inputting data –Remember prompts can be passed as parameters to read messages. For Example: fahrenheit = reader.readDouble (“Enter degrees Fahrenheit: ”);

4.4 Control Statements –While and if-else are called control statements. For example: while (some condition) { do stuff; Means do the stuff repeatedly as long as the condition holds true o do stuff 1; }else { do stuff 2; Means if some condition is true, do stuff 1, and if it is false, do stuff 2. while (some condition){ do stuff; } if (some condition){ do stuff 1; }else{ do stuff 2; }

The condition in an if or while must be a Boolean expression, which means it has a value of true or false. In Java there is a data type boolean. boolean found; boolean pos = true; while ( pos == true) { n= reader.readInt( “Enter a number”); if (n<0) { pos=false; } while ( pos ) { n= reader.readInt(“Enter a number”); if (n<0) { pos=false; }

4.5 The if and if-else Statements Principal Forms –In Java, the if and if-else statements allow for the conditional execution of statements. if (condition){ statement;//Execute these statements if the statement;//condition is true. } if (condition){ statement;//Execute these statements if the statement;//condition is true. }else{ statement;//Execute these statements if the statement;//condition is false. }

4.5 The if and if-else Statements –There is no semicolon immediately following a closing brace.

// Increase a salesman’s commission by 10% if his sales are over $5000 if (sales > 5000){ commission *= 1.1; } // Pay a worker $14.5 per hour plus time and a half for overtime pay = hoursWorked * 14.5; if (hoursWorked > 40){ overtime = hoursWorked - 40; pay += overtime * 7.25; } // Let c equal the larger of a and b if (a > b) { c = a; } else{ c = b; } EXAMPLES:

4.5 The if and if-else Statements Relational Operators Table 4-3 shows the complete list of relational operators available for use in Java.

4.5 The if and if-else Statements –The double equal signs (==) distinguish the equal to operator from the assignment operator (=). –In the not equal to operator, the exclamation mark (!) is read as not.

Assignment #4 page 100 #6 page 101