Data Type Valid Operators Example Literals 42 159 0 double 1. 5 2. 9+8 -17. 5 3. 3334 Boolean true false String . 18 A statement that “declares” one or more variable, giving each a name and associating each with a data type. String circumscription; Assigning an initial value is optional, but generally a good idea. Boolean effortlessness’s = true; Declaring multiple variables in one statement is allowed but discoursed. Into unobtrusiveness = 174, numbering’s = 6; Expression A small piece of program code that represents or computes a value of a particular hype.
Variables and Expressions After a variable is declared and initialized, it can then be used in expressions Into infectiousness’s =4; double delectation = infectiousness’s * 2. 5; double telescope = 100 – delectation; Statements A small piece of a program code that causes some side effects Two kinds seen so Print statement: System. Out. Print(“Hi”); Variable declaration: double radius = 7. 8; If we will need to set the value of a variable after its declaration, we must use an assignment statement. ; Method A sequence of instructions for carrying out a certain task, grouped together and given a name.
That name can be used elsewhere in the program as a stand-in for the sequence of instructions. Public static in god(Into a, Into b) { // instructions for Euclid algorithm Method Definition Public static return-type method name (type parameter 1){ Where a method returns an integer, return type is “Into” When a method does not return a value, return type is void. The rules for method names are the same as for variables. A method can have O or more formal parameters These are the input(s) to the method Each must be specified with a data type and name Method Call A statement that causes execution of the method’s body of instructions.
This is the “stand-in” for the assurance of instructions inside the method definition This is also where we specify the value of the formal parameters (inputs) Method Calls Nearly all examples we have seen so far simply call a method and print the returned result. Into kilometers = MITOCW(105); System. Out. Print(kilometers); Method calls are expressions and can be used in other expressions. Task(105) + Amtrak(433) – Amtrak(89) Relational and Logical Operators: Operators Operand(s) Value Relational Two numerical values Into, double, and other (true or false) Logical One (! Or two(&&, I l) Boolean values Boolean Expression An expression that includes relational operators and/or logical operators and evaluates to true or false. Count // Postposition: XA = returned value public static double sort(double x) {… } Together these condition form a method contract. If the preconditions are met, the method body MUST fulfill the post conditions. Method Contracts If the precondition(s) is not met, the method must still behave in a reasonable way; I. E. , not crash or give an incorrect result.
How a method handles all possible parameter values should be described in the method comment For Java built-in methods, this is described in the API (Applications Programming Interface) Scope The region in a program where the declaration of an intensifier is valid. It is not valid to use the parameters of a method outside of that methods body. Public static Into sum(Into a, Into b){ return a + b; public static double sum(double c){ turn b + c; // error: b is out of scope Local Variable A variable declared inside a method It is also not valid to use the local variables of a method outside of that methods body.
Public static Into sum(Into a, Into b){ Into result-?a + b; Return result; public static double sum(double c, double d){ result = c + d; // error: result out of scope return result; Scope (2) Each new structured statement defines a new scope If statements, loops, or even Just blocks via 0 Static Member Variable A variable declared inside a class with the keyword static and accessible by all Scope (3) If a parameter or local variable with the same name is used in a method, the (inner) parameter or local variable hides the (outer) static member variable.
Loop Patterns Counting Loop Accumulation loop Searching loop Optimization loop Sentinel Value A data value used to mark the end of real data. The value itself is not part of the collection of data Priming the Loop Setting things up so that the Boolean expression in a loop “makes sense” the first time it is executed Off-by-one Error An error that occurs because a loop is iterated one time too many or one time too few Loop control variable Used to regulate the number of times the body of a loop is executed.
It is incremented (or decremented) each time the loop body is executed Arranging Loops Nested Loops: Consecutive Loops: Break Statement A mechanism for breaking out a loop. When executed, control immediately leaves the loop and continues on to whatever follows the loop in the program. Continue statement A mechanism for skipping the rest of the current iteration of a loop, but not breaking out of the loop. When executed, control immediately goes to evaluation of the Boolean expression that controls the loop.