Download presentation
Presentation is loading. Please wait.
1
Nahla Abuel-ola / WIT
2
Your First Program in Java: Printing a Line of Text (Cont.)
System.out.println method Displays (or prints) a line of text in the command window. The string in the parentheses the argument to the method. Positions the output cursor at the beginning of the next line in the command window. Most statements end with a semicolon. Nahla Abuel-ola / WIT
3
Obtaining Variables as input From the User
1. Import java.util.Scanner; 2. Create an object ( a copy from the Scanner class): Scanner obj = new Scanner (system.in); 3. Print your question: System.out.println(“ please enter the number); 4. Store the number in the obj variable: Obj = number.nextInt(); Nahla Abuel-ola / WIT
4
Nahla Abuel-ola / WIT
5
Arithmetic Operations
Nahla Abuel-ola / WIT
6
Arithmetic Operations
Nahla Abuel-ola / WIT
7
Relational and conditional operators
Nahla Abuel-ola / WIT
8
Arithmetic (Cont.) Rules of operator precedence
Multiplication, division and remainder operations are applied first. If an expression contains several such operations, they are applied from left to right. Multiplication, division and remainder operators have the same level of precedence. Addition and subtraction operations are applied next. If an expression contains several such operations, the operators are applied from left to right. Addition and subtraction operators have the same level of precedence. When we say that operators are applied from left to right, we are referring to their associativity. Some operators associate from right to left. Complete precedence chart is included in Appendix A. Nahla Abuel-ola / WIT
9
Nahla Abuel-ola / WIT
10
Nahla Abuel-ola / WIT
11
Java Basics: Expressions
Examples: 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division) Remainder is very useful in programming. an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd Nahla Abuel-ola / WIT
12
Nahla Abuel-ola / WIT
13
Type Casting Two types Implicit—also called "Automatic" Done FOR you, automatically 17 / 5.5 Explicit type conversion Programmer specifies conversion with cast operator (double)17 / (double)myInt / myDouble Explicitly "casts" or "converts" intVar to double type Result of conversion is then used Can add ".0" to literals to force precision arithmetic, but what about variables? We can’t use "myInt.0"! Nahla Abuel-ola / WIT
14
Compound Assignment Operations
Nahla Abuel-ola / WIT
15
Increment and Decrement Operators
Unary increment operator, ++, adds one to its operand Unary decrement operator, --, subtracts one from its operand An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator, respectively. An increment or decrement operator that is postfixed to (placed after) a variable is referred to as the postfix increment or postfix decrement operator, respectively. Nahla Abuel-ola / WIT
16
Increment and Decrement Operators
Nahla Abuel-ola / WIT
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.