Upcoming SlideShare
×

# Comp102 lec 5.1

288 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
288
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Comp102 lec 5.1

1. 1.  Take the value from right hand side (rvalue) and copy it into left hand side (lvalue) Rvalue  Constant , variable or expression Lvalue  Distinct, named variable
2. 2. Operator Example Equivalent+= i += 8 i = i + 8-= f -= 8.0 f = f - 8.0*= i *= 8 i = i * 8/= i /= 8 i = i / 8%= i %= 8 i = i % 8 3
3. 3.  Syntax Errors  Detected by the compiler Runtime Errors  Causes the program to abort Logic Errors  Produces incorrect result 4
4. 4. A pair of braces in a program forms a block thatgroups components of a program. public class Test { Class block public static void main(String[] args) { System.out.println("Welcome to Java!"); Method block } } 5
5. 5. Eihter next-line or end-of-line style for braces. 6
6. 6. Specifier Output Example%b a boolean value true or false%c a character a%d a decimal integer 200%f a floating-point number 45.460000%e a number in standard scientific notation 4.556000e+01%s a string "Java is cool" int count = 5; items double amount = 45.56; System.out.printf("count is %d and amount is %f", count, amount); display count is 5 and amount is 45.560000 7
7. 7. Description Escape SequenceBackspace bTab tLinefeed nCarriage return rBackslash Single Quote Double Quote " 8
8. 8. Java in two semesters by Quentin Charatan & Aaron Kans
9. 9.  The order in which the instructions were executed was not under your control Program starts by executing the first instruction in main method and then all executed instructions were in sequence This order of execution is restrictive and one as programmer need more control over the order in which the instructions are executed
10. 10. Generates boolean resultEvaluates relationship between values of the operands  Produces TRUE if relationship is true  Produces FALSE is relationship is untrue
11. 11. Operator Meaning== Equal to!= Not equal to< Less than> Greater than>= Greater than or equal to<= Less than or equal to
12. 12.  Whenever we need to make choice among different courses of action Example  A program processing requests for airline tickets could have following choices to make  Display the price os seats requested  Display a list of alternative flights  Display a message saying that no flights are available to that destinationA program that can make choices can behave differently each time it is run, whereas programs run in sequence behave the same way each time they are run Unless otherwise mentioned, program instructions are executed in sequence
13. 13.  Some of the instruction need a guard so that they are executed only when appropriate  JAVA if statement Syntaxif(/* a test goes here*/){ // instruction (s) to be guarded go here} The braces indicate the body of if statement If statement must follow the round brackets and condition is placed inside these brackets Condition/expression gives a boolean result of true or false
14. 14. import java.util.Scanner;public class Assignment1 { public static void main(String[] args) { int x = 0; Scanner scan = new Scanner(System.in); x = scan.nextInt(); if(x%2 == 0) { System.out.println("number is even"); } }}
15. 15. import java.util.Scanner;public class Assignment1 { public static void main(String[] args) { int temperature = 0; Scanner scan = new Scanner(System.in); temperature = scan.nextInt(); if(temperature<0) { System.out.println("its freezing"); System.out.println("temperature is: "+temperature); System.out.println("Wear appropriate clothes"); } }}
16. 16.  Single – branched instructions The if statement provides two choices  Execute the conditional instructions  Condition is true  Do not execute the conditional instructions  Condition is false  Do nothing
17. 17.  Double branched selection Alternative course of action Choices  Some instructions are executed if condition is true  Some other instructions are executed if condition is false
18. 18. import java.util.Scanner;public class Assignment1 { public static void main(String[] args) { int x = 0; Scanner scan = new Scanner(System.in); x = scan.nextInt(); if(x%2 == 0) { System.out.println("number is even"); } else { System.out.println("number is odd"); } System.out.println(“good work"); }}
19. 19.  int n1 = 15; int n2 = 15; System.out.println(n1==n2); System.out.println(n1!=n2);
20. 20.  Often it is necessary to join two or more tests together to create a complicated test Consider a program that checks the temperature in laboratory. To have a successful experiment it is required that the temperature remain between 5 and 12 The test need to check  Temperature is greater than or equal to 5  Temperature>=5  Temperature is less than or equal to 12  Temperature <=12  Both of these tests need to evaluate true in order to provide the right environment
21. 21. Logical Operator Java CounterpartAND &&OR ||NOT !Produces a boolean value of true or false based on logical relationship of argumentsAllowed in between of boolean values onlyConditions generate boolean result
22. 22. A = Result A = Result A && B of 1st of 2ndExpression ExpressionTrue True TrueTrue False FalseFalse True FalseFalse False False
23. 23. A = Result A = Result A || B of 1st of 2ndExpression ExpressionTrue True TrueTrue False TrueFalse True TrueFalse False False
24. 24. X !XTRUE FALSEFALSE TRUE
25. 25. import java.util.Scanner;public class Assignment1 { public static void main(String[] args) { int temperature = 0; Scanner scan = new Scanner(System.in); temperature = scan.nextInt(); if(temperature>=5 && temperature <=12) { System.out.println("environment is safe"); } else { System.out.println("environment is not safe"); } }}
26. 26. In JavaIf a value is zero, it can be used as the logical value false. If a value is not zero, it can be used as the logical value true. Zero <===> False Nonzero <===> True
27. 27.  Expression will be evaluated only until the truth or falsehood of the entire expression can be unambiguously determined Latter parts of the logical expression might not be evaluated
28. 28. Expression Result Explanation10>5 && 10>7 True Both results are true10>5 && 10>20 False The second test is false10>15 && 10>20 False Both tests are false10>5 || 10>7 True At least one test is true10>5 || 10>20 True At least one test is true10>15 || 10>20 False Both tests are false!(10>5) False Original test is true!(10>15) True Original test is false
29. 29.  Instructions within if and if…else statements can themselves be any legal java commands It is also allowed that if statements contain other if/if-else statements  Nesting  Allows multiple choices to be processed
30. 30. public static void main(String[] args) { int marks = 0; Scanner scan = new Scanner(System.in); marks = scan.nextInt(); if(marks <100) { if(marks>=80) { System.out.println("Grade = A"); } else { if(marks>=60) { System.out.println("Grade = B"); } else { System.out.println("Grade = F"); } } }}
31. 31. public static void main(String[] args) { int marks = 0; Scanner scan = new Scanner(System.in); marks = scan.nextInt(); if(marks <100) { if(marks>=80) { System.out.println("Grade = A"); } else if(marks>=60) { System.out.println("Grade = B"); } else { System.out.println("Grade = F"); } }}
32. 32. else is always pairedwith the most recent, unpaired if
33. 33. Adding a semicolon at the end of an if clause is acommon mistake.if (radius >= 0);{ area = radius*radius*PI; System.out.println( "The area for the circle of radius " + radius + " is " + area);}This mistake is hard to find, because it is not acompilation error or a runtime error, it is a logic error.This error often occurs when you use the next-lineblock style. 40
34. 34.  Conditionaloperator  Has three operands  Boolean-exp? value0 : value1import java.util.Scanner;public class Assignment1{ public static void main(String[] args) { int marks = 0; Scanner scan = new Scanner(System.in); marks = scan.nextInt(); System.out.println(marks>=50? "Pass":"Fail"); }}
35. 35. public static void main(String[] args) { int value = 0; Scanner scan = new Scanner(System.in); value = scan.nextInt(); switch(value) { case 1: System.out.println("We are in case 1"); break; case 2: System.out.println("We are in case 2"); break; case 3: System.out.println("We are in case 3"); break; default: System.out.println("We are in case default"); //break; }}
36. 36.  Only one variable is being checked in each condition Check involves only specific values of that variable and not ranges (>= or <= are not allowed) Switch statement condition carries the name of the variable only The variable is usually of type int or char but can also be of type long, byte or short break is optional command that forces the program to skip the rest of switch statement Default is optional (last case) that can be thought of as an otherwise statement.  Deal with the possibility if none of the cases above is true
37. 37. Consider the following statements:byte i = 100;long k = i * 3 + 4;double d = i * 3.1 + k / 2; 47
38. 38. When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules:1. If one of the operands is double, the other is converted into double.2. Otherwise, if one of the operands is float, the other is converted into float.3. Otherwise, if one of the operands is long, the other is converted into long.4. Otherwise, both operands are converted into int. 48
39. 39. Implicit casting double d = 3; (type widening)Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated)What is wrong? int x = 5 / 2.0; range increases byte, short, int, long, float, double 49
40. 40. int i = a; // Same as int i = (int)a;char c = 97; // Same as char c = (char)97; 50