Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction To Java Programming Lecture No. 3

1,859 views

Published on

Relation Operators in Java
Logical Operators in Java
Bitwise Operators in Java
Assignment Operators in Java
Assignment operators Program Example
Miscellaneous Operators
instanceof Operator in Java
Precedence of Operators
Java If-else Statements
Java if Statement
Java if Statement Example
Program Example of if Statement
Nested if Statement in java
Example of Nested if Statement
Java if-else Statement
Double Selection Examples
if-else Statement Example
Nested if else Statement
Switch Statement in java

Published in: Education

Introduction To Java Programming Lecture No. 3

  1. 1. Introduction To Java Programming Language Lecture No.3 Notes By Adil Aslam My Email Address is : adilaslam5959@gmail.com
  2. 2. In this Lecture we Learn • Relation operators in Java • Logical operators in Java • Bitwise Operators in Java • Assignment Operators in Java • Miscellaneous Operators in Java • Java Selection Statements • If Statement • If else Statement • If else if Statement • Switch Statement Introduction to Java Programming Language Notes By Adil Aslam
  3. 3. Relation Operators The following table shows all relation operators supported by Java. Introduction to Java Programming Language Notes By Adil Aslam Operator Description == Check if two operand are equal != Check if two operand are not equal. > Check if operand on the left is greater than operand on the right < Check operand on the left is smaller than right operand >= check left operand is greater than or equal to right operand <= Check if operand on left is smaller than or equal to right operand
  4. 4. Assume variable A holds 10 and variable B holds 20, then: Introduction to Java Programming Language Notes By Adil Aslam Operator Description Example == (equal to) Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != (not equal to) Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > (greater than) Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
  5. 5. Assume variable A holds 10 and variable B holds 20, then: Introduction to Java Programming Language Notes By Adil Aslam Operator Description Example < (less than) Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= (greater than or equal to) Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= (less than or equal to) Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.
  6. 6. Relational Operators Which can be used to check the Condition, it always return true or false. Lets suppose variable A hold 8 and B hold 3. Introduction to Java Programming Language Notes By Adil Aslam Operators Example (int A=8, B=3) Result < A<B False <= A<=10 True > A>B True >= A<=B False == A== B False != A!=(-4) True
  7. 7. Logical Operators Assume Boolean variables A holds true and variable B holds false, then Introduction to Java Programming Language Notes By Adil Aslam Operator Description Example Operator Description Example && (logical and) Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false || (logical or) Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. (A || B) is true ! (logical not) Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true
  8. 8. Logical Operators (Example) Java supports following 3 logical operator. Suppose a=1 and b=0; Introduction to Java Programming Language Notes By Adil Aslam Operator Description Example && Logical AND (a && b) is false || Logical OR (a || b) is true ! Logical NOT (!a) is false
  9. 9. Bitwise Operators Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte Introduction to Java Programming Language Notes By Adil Aslam Operator Description & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR << left shift >> right shift
  10. 10. Now lets see truth table for bitwise &, | and ^ a b a & b a | b a ^ b 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 Introduction to Java Programming Language Notes By Adil Aslam The bitwise shift operators shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted. Both operands have the same precedence. Example a = 0001000 b= 2 a << b= 0100000 a >> b= 0000010
  11. 11. Assignment Operators Operator Description Example = assigns values from right side operands to left side operand a=b += adds right operand to the left operand and assign the result to left a+=b is same as a=a+b -= subtracts right operand from the left operand and assign the result to left operand a-=b is same as a=a-b *= mutiply left operand with the right operand and assign the result to left operand a*=b is same as a=a*b /= divides left operand with the right operand and assign the result to left operand a/=b is same as a=a/b %= calculate modulus using two operands and assign the result to left operand a%=b is same as a=a%b Introduction to Java Programming Language Notes By Adil Aslam
  12. 12. Assignment operators (Example) Which can be used to assign a value to a variable. Lets suppose variable A hold 8 and B hold 3. Introduction to Java Programming Language Notes By Adil Aslam Operator Example (int A=8, B=3) Result += A+=B or A=A+B 11 -= A-=3 or A=A+3 5 *= A*=7 or A=A*7 56 /= A/=B or A=A/B 2 %= A%=5 or A=A%5 3 =a=b Value of b will be assigned to a
  13. 13. Assignment operators Program Example Introduction to Java Programming Language Notes By Adil Aslam
  14. 14. Assignment operators Program Example public class JavaApplication1 { public static void main(String[] args) { //+= Assignment Operator int value1 = 4; int value2 = 8; value2 += value1; System. out .println( "value1 is " +value1); System. out .println( "value2 is " +value2); } } Introduction to Java Programming Language Notes By Adil Aslam
  15. 15. Assignment operators Program Example public class JavaApplication1 { public static void main(String[] args) { //+= Assignment Operator int value1 = 4; int value2 = 8; value2 += value1; System. out .println( "value1 is " +value1); System. out .println( "value2 is " +value2); } } Introduction to Java Programming Language Notes By Adil Aslam
  16. 16. Assignment operators Program Example public class JavaApplication1 { public static void main(String[] args) { //+= Assignment Operator int value1 = 4; int value2 = 8; value2 += value1; System. out .println( "value1 is " +value1); System. out .println( "value2 is " +value2); } } Introduction to Java Programming Language Notes By Adil Aslam value2 += value1 value2 = value2+value1 Value2=8+4=12
  17. 17. Assignment operators Program Example public class JavaApplication1 { public static void main(String[] args) { //+= Assignment Operator int value1 = 4; int value2 = 8; value2 += value1; System. out .println( "value1 is " +value1); System. out .println( "value2 is " +value2); } } Introduction to Java Programming Language Notes By Adil Aslam value2 += value1 value2 = value2+value1 Value2=8+4=12 Output is : value1 is 4 value2 is 12
  18. 18. Assignment operators Program Example Introduction to Java Programming Language Notes By Adil Aslam Value2/=value1 equivalent Value2=value2/value1 Value2=8/4=2
  19. 19. Miscellaneous Operators • There are few other operators supported by Java Language • Conditional Operator ( ? : ) • Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide, which value should be assigned to the variable. The operator is written as : Introduction to Java Programming Language Notes By Adil Aslam variable x = (expression) ? value if true : value if false
  20. 20. Program Example of Conditional Operator ( ? : ) Introduction to Java Programming Language Notes By Adil Aslam
  21. 21. public class JavaApplication1 { public static void main(String[] args) { int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b ); } } Introduction to Java Programming Language Notes By Adil Aslam Condition .Checking if Condition is true Print 20 if false print 30
  22. 22. public class JavaApplication1 { public static void main(String[] args) { int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b ); } } Introduction to Java Programming Language Notes By Adil Aslam Condition .Checking if Condition is true Print 20 if false print 30 Here Condition is false because value of a is 10
  23. 23. public class JavaApplication1 { public static void main(String[] args) { int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b ); } } Introduction to Java Programming Language Notes By Adil Aslam Condition .Checking if Condition is true Print 20 if false print 30 Here Condition is true because value of a is 10
  24. 24. public class JavaApplication1 { public static void main(String[] args) { int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b ); } } Introduction to Java Programming Language Notes By Adil Aslam Output is : Value of b is : 30 Value of b is : 20 Condition is False Condition is true
  25. 25. Miscellaneous Operators • instanceof Operator • This operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type). instanceof operator is written as: • If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true Introduction to Java Programming Language Notes By Adil Aslam ( Object reference variable ) instanceof (class/interface type)
  26. 26. Program Example of instanceof Operator Introduction to Java Programming Language Notes By Adil Aslam
  27. 27. Program Example of instanceof Operator public class JavaApplication1 { public static void main(String[] args) { String name = "Adil"; // following will return true since name is type of String boolean result = name instanceof String; System.out.println( result ); } } Introduction to Java Programming Language Notes By Adil Aslam
  28. 28. Program Example of instanceof Operator public class JavaApplication1 { public static void main(String[] args) { String name = "Adil"; // following will return true since name is type of String boolean result = name instanceof String; System.out.println( result ); } } Introduction to Java Programming Language Notes By Adil Aslam Output is : true
  29. 29. (% = Remainder) Operator • Examples • 15 % 2 = 1 • 20 % 5 = 0 • 38 % 6 = 2 • 45 % 7 = 3 • 52 % 8 = 4 • 20 % 3 = 2 Introduction to Java Programming Language Notes By Adil Aslam
  30. 30. Precedence of Operators • All operations inside of () are evaluated first • *, /, and % are at the same level of precedence and are evaluated next • + and – have the same level of precedence and are evaluated last • When operators are on the same level • Performed from left to right (associativity) • 3 * 7 - 6 + 2 * 5 / 4 + 6 means (((3 * 7) – 6) + ((2 * 5) / 4 )) + 6 Introduction to Java Programming Language Notes By Adil Aslam
  31. 31. Precedence of Operators • Java has a large operator set. • When an expression involves two or more operators, two rules are applied to determine the meaning of the expression:- • Priority - Higher priority operators take priority over lower priority ones. • 2 + 3 * 5 is 17 not 25. Introduction to Java Programming Language Notes By Adil Aslam
  32. 32. Precedence of Operators  Association - When selecting between two operators of the same priority, they operate according to their associativity which is either:- R/L (right to left). Unary operators and assignment operators are right to left. • So x=y=z places the value z in x and y (not place y in x and z in y)  L/R (left to right). All other operators work left to right Introduction to Java Programming Language Notes By Adil Aslam
  33. 33. Precedence of Operators Introduction to Java Programming Language Notes By Adil Aslam Precedence Operators Highest ( ) Next * , / , % Lowest + , -
  34. 34. Same Precedence Example • For addition, subtraction, division and multiplication: • Left-to-right rule applies • A+B+C means (A+B)+C • For exponentiation • Right-to-left rule applies • A ^ B ^ C means A ^ ( B ^ C ) Introduction to Java Programming Language Notes By Adil Aslam
  35. 35. Expressions Introduction to Java Programming Language Notes By Adil Aslam • Combination of Operators and Operands Example 2 * y + 5 Operators Operands
  36. 36. Expressions • If all operands are integers • Expression is called an integral expression • Yields an integral result • Example: 2 + 3 * 5 • If all operands are floating-point • Expression is called a floating-point expression • Yields a floating-point result • Example: 12.8 * 17.5 - 34.50 Introduction to Java Programming Language Notes By Adil Aslam
  37. 37. Expressions • Mixed expression: • Has operands of different data types • Contains integers and floating-point • Examples of mixed expressions: 2 + 3.5 6 / 4 + 3.9 5.4 * 2 – 13.6 + 18 / 2 Introduction to Java Programming Language Notes By Adil Aslam
  38. 38. Activity • 10+2*3+5*4+12/6+5*20/2 • Here highest Precedence first is * then / then last Precedence of + • First solve * • 10+6+20+12/6+80/2 • Second Solve / • 10+6+20+2+40 • Third Solve + • Answer is 88 Introduction to Java Programming Language Notes By Adil Aslam
  39. 39. Java If-else Statements • The Java if statement is used to test the condition. It returns true or false. There are various types of if statement in java. • if statement • if-else statement • nested if statement • if-else-if ladder Introduction to Java Programming Language Notes By Adil Aslam
  40. 40. Java if Statement “Single Selection” Introduction to Java Programming Language Notes By Adil Aslam
  41. 41. Java if Statement • The if statement tests the condition. It executes the if statement if condition is true. • Syntax: • If the condition (or Boolean expression ) evaluates to true then the block of code inside the if statement will be executed. If not the first set of code after the end of the if statement (after the closing curly brace) will be executed. Introduction to Java Programming Language Notes By Adil Aslam if(condition){ //code to be executed }
  42. 42. Flow Chart of if Statement Introduction to Java Programming Language Notes By Adil Aslam condition If code After if True false
  43. 43. Java if Statement Example • if (bank balance is zero) • Borrow money • if (room is dark) • Put on lights • if (today is Sunday) • Lets play football • if (student marks is 60) • You are pass • if(code is 0) • Person is male • if (age is more than 55) • Person is retired Introduction to Java Programming Language Notes By Adil Aslam This is the action
  44. 44. Java if Statement Example • For example, suppose the passing grade on an exam is 60. The pseudo code statement • Determines whether the condition “student’s grade is greater than or equal to 60”is true or false. If the condition is true, “Passed” is printed and the next pseudo code statement in order is“ performed. • If the condition is false, the print statement is ignored and the next pseudo code statement in order is performed. Introduction to Java Programming Language Notes By Adil Aslam If student ’s grade is greater than or equal to 60 Print “Passed”
  45. 45. Java if Statement Example • The preceding pseudo code If statement can be written in java as Introduction to Java Programming Language Notes By Adil Aslam if (grade >= 60 ) System.out.println(“Passed");
  46. 46. Program Example of if Statement Introduction to Java Programming Language Notes By Adil Aslam
  47. 47. Program Example of if Statement public class MyFirstProgram { public static void main(String[] args) { int number=20; if(number>10) System.out.println("i am in if Statement"); } } Introduction to Java Programming Language Notes By Adil Aslam Declare and initialized variable with value of 20 Checking condition if number greater than 10 then condition is true
  48. 48. Program Example of if Statement public class MyFirstProgram { public static void main(String[] args) { int number=20; if(number>10) System.out.println("i am in if Statement"); } } Introduction to Java Programming Language Notes By Adil Aslam Declare and initialized variable with value of 20 Checking condition if number greater than 10 then condition is trueOutput is : i am in if statement
  49. 49. Another Example of is Statement Introduction to Java Programming Language Notes By Adil Aslam
  50. 50. Another Example of is Statement public class MyFirstProgram { public static void main(String[] args) { int a=10,b=20; if (a<b) { System.out.println("This is if statement example"); } } } Introduction to Java Programming Language Notes By Adil Aslam
  51. 51. Another Example of is Statement public class MyFirstProgram { public static void main(String[] args) { int a=10,b=20; if (a<b) { System.out.println("This is if statement example"); } } } Introduction to Java Programming Language Notes By Adil Aslam Declare two variables and both initialized
  52. 52. Another Example of is Statement public class MyFirstProgram { public static void main(String[] args) { int a=10,b=20; if (a<b) { System.out.println("This is if statement example"); } } } Introduction to Java Programming Language Notes By Adil Aslam Declare two variables and both initialized Checking condition if a is less than b then return true otherwise return false
  53. 53. Another Example of is Statement public class MyFirstProgram { public static void main(String[] args) { int a=10,b=20; if (a<b) { System.out.println("This is if statement example"); } } } Introduction to Java Programming Language Notes By Adil Aslam Declare two variables and both initialized Checking condition if a is less than b then return true otherwise return false Output is: This is if statement example
  54. 54. One more Example of if statement Introduction to Java Programming Language Notes By Adil Aslam
  55. 55. One more Example of if statement public class MyFirstProgram { public static void main(String[] args) { int a=20,b=10; if (a<b) { System.out.println("a is less than b"); } if(a>b){ System.out.println(“a is greater than b "); } } } Introduction to Java Programming Language Notes By Adil Aslam
  56. 56. One more Example of if statement public class MyFirstProgram { public static void main(String[] args) { int a=20,b=10; if (a<b) { System.out.println("a is less than b"); } if(a>b){ System.out.println(“a is greater than b"); } } } Introduction to Java Programming Language Notes By Adil Aslam If a is Less than b then print this statement
  57. 57. One more Example of if statement public class MyFirstProgram { public static void main(String[] args) { int a=20,b=10; if (a<b) { System.out.println("a is less than b"); } if(a>b){ System.out.println(“a is greater than b "); } } } Introduction to Java Programming Language Notes By Adil Aslam If a is greater than b then print this statement If a is Less than b then print this statement
  58. 58. One more Example of if statement public class MyFirstProgram { public static void main(String[] args) { int a=20,b=10; if (a<b) { System.out.println("a is less than b"); } if(a>b){ System.out.println(“a is greater than b "); } } } Introduction to Java Programming Language Notes By Adil Aslam If a is greater than b then print this statement If a is Less than b then print this statement Output is: a is greater than b
  59. 59. Your Task… Write a java program which gets the value of two integers from user and display whether first number is lower than, equal to or greater than the second number. Introduction to Java Programming Language Notes By Adil Aslam
  60. 60. Introduction to Java Programming Language Notes By Adil Aslam Solution of Previous Task
  61. 61. Nested if statement in java Introduction to Java Programming Language Notes By Adil Aslam
  62. 62. Nested if Statement in java • It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement. • Syntax • The syntax for a nested if statement is as follows Introduction to Java Programming Language Notes By Adil Aslam if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } }
  63. 63. Nested if statement in java Introduction to Java Programming Language Notes By Adil Aslam if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } } If Boolean Expression is true
  64. 64. Nested if statement in java Introduction to Java Programming Language Notes By Adil Aslam if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } } If Boolean Expression is true If Boolean Expression is False
  65. 65. Nested if statement in java Introduction to Java Programming Language Notes By Adil Aslam if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } } If Boolean Expression is true If Boolean Expression is False If Boolean Expression is true
  66. 66. Nested if statement in java Introduction to Java Programming Language Notes By Adil Aslam if(Boolean_expression 1) { // Executes when the Boolean expression 1 is true if(Boolean_expression 2) { // Executes when the Boolean expression 2 is true } } If Boolean Expression is true If Boolean Expression is FalseIf Boolean Expression is true If Boolean Expression is False
  67. 67. Example of Nested if Statement Introduction to Java Programming Language Notes By Adil Aslam
  68. 68. Example of Nested if Statement public class Test { public static void main(String[] args) { int x = 30; int y = 10; if( x == 30 ) { if( y == 10 ) { System.out.println("X = 30 and Y = 10"); } } } } Introduction to Java Programming Language Notes By Adil Aslam
  69. 69. Example of Nested if Statement public class Test { public static void main(String[] args) { int x = 30; int y = 10; if( x == 30 ) { if( y == 10 ) { System.out.println("X = 30 and Y = 10"); } } } } Introduction to Java Programming Language Notes By Adil Aslam This an outer if
  70. 70. Example of Nested if Statement public class Test { public static void main(String[] args) { int x = 30; int y = 10; if( x == 30 ) { if( y == 10 ) { System.out.println("X = 30 and Y = 10"); } } } } Introduction to Java Programming Language Notes By Adil Aslam This an outer if This an inner if
  71. 71. Example of Nested if Statement public class Test { public static void main(String[] args) { int x = 30; int y = 10; if( x == 30 ) { if( y == 10 ) { System.out.println("X = 30 and Y = 10"); } } } } Introduction to Java Programming Language Notes By Adil Aslam This an outer if This an inner if When both if condition are true then execute this statement
  72. 72. Example of Nested if Statement public class Test { public static void main(String[] args) { int x = 30; int y = 10; if( x == 30 ) { if( y == 10 ) { System.out.println("X = 30 and Y = 10"); } } } } Introduction to Java Programming Language Notes By Adil Aslam This an outer if This an inner if When both if condition are true then execute this statement Output is: X = 30 and Y = 10
  73. 73. Another Example of Nested if Statement Introduction to Java Programming Language Notes By Adil Aslam Outer if Statement
  74. 74. Another Example of Nested if Statement Introduction to Java Programming Language Notes By Adil Aslam Outer if Statement Inner if Statement
  75. 75. Another Example of Nested if Statement Introduction to Java Programming Language Notes By Adil Aslam Outer if Statement Inner if Statement In this program outer if condition is true so print ‘I am y’ but inner if condition is false
  76. 76. Another Example of Nested if Statement Introduction to Java Programming Language Notes By Adil Aslam Outer if Statement Inner if Statement In this program outer if condition is true so print ‘I am y’ but inner if condition is false Output is:
  77. 77. One More Example Introduction to Java Programming Language Notes By Adil Aslam Outer if statement
  78. 78. One More Example Introduction to Java Programming Language Notes By Adil Aslam Outer if statement Inner if statement
  79. 79. One More Example Introduction to Java Programming Language Notes By Adil Aslam Outer if statement Inner if statement In this program please note that outer if condition is false so compiler never check the inner if condition even it will be true
  80. 80. One More Example Introduction to Java Programming Language Notes By Adil Aslam Outer if statement Inner if statement In this program please note that outer if condition is false so compiler never check the inner if condition even it will be true Output is nothing
  81. 81. Java if else Statement “Double Selection” Introduction to Java Programming Language Notes By Adil Aslam
  82. 82. Java if-else Statement • The if-else statement also tests the condition. • It executes the if block if condition is true otherwise execute else block. • Syntax: Introduction to Java Programming Language Notes By Adil Aslam if(condition){ //code if condition is true } else{ //code if condition is false }
  83. 83. Flow chart of if else statement Introduction to Java Programming Language Notes By Adil Aslam
  84. 84. Double Selection Examples • if today is Thursday I have test today else we have match today • if you will run fast, you will catch the bus else you have to take a cab • if x is dividable by 2 then x is even number else x is odd Introduction to Java Programming Language Notes By Adil Aslam
  85. 85. if-else Statement Example • For example, suppose the passing grade on an exam is 60. The pseudo code statement • Prints “Passed” if the student’s grade is greater than or equal to 60, but prints “Failed” if the student’s grade is less than 60. In either case , after printing occurs , the next pseudo code statement in sequence is “performed. Introduction to Java Programming Language Notes By Adil Aslam If student ’s grade is greater than or equal to 60 Print “Passed” else Print “Failed”
  86. 86. if-else Statement Example • The preceding pseudo code If…Else statement can be written in Java as Introduction to Java Programming Language Notes By Adil Aslam if (grade >= 60 ) System.out.println(“Passed"); else System.out.println(“Failed");
  87. 87. Program Example of if else Statement Introduction to Java Programming Language Notes By Adil Aslam
  88. 88. Program Example of if else Statement public class MyFirstProgram { public static void main(String[] args) { int number=30; if (number<30) { System.out.println("This is if statement"); } else{ System.out.println("This is else statement"); } } } Introduction to Java Programming Language Notes By Adil Aslam
  89. 89. Program Example of if else Statement public class MyFirstProgram { public static void main(String[] args) { int number=30; if (number<30) { System.out.println("This is if statement"); } else{ System.out.println("This is else statement"); } } } Introduction to Java Programming Language Notes By Adil Aslam If number is less than 30 than execute this statement
  90. 90. Program Example of if else Statement public class MyFirstProgram { public static void main(String[] args) { int number=30; if (number<30) { System.out.println("This is if statement"); } else{ System.out.println("This is else statement"); } } } Introduction to Java Programming Language Notes By Adil Aslam If number is less than 30 than execute this statement Otherwise execute this statement
  91. 91. Program Example of if else Statement public class MyFirstProgram { public static void main(String[] args) { int number=30; if (number<30) { System.out.println("This is if statement"); } else{ System.out.println("This is else statement"); } } } Introduction to Java Programming Language Notes By Adil Aslam If number is less than 30 than execute this statement Otherwise execute this statement Output is: This is else statement
  92. 92. Program Example of if else Statement Introduction to Java Programming Language Notes By Adil Aslam
  93. 93. Program Example of if else Statement Introduction to Java Programming Language Notes By Adil Aslam Checking two conditions here 1.Is number is less than 30 2.Is number is equal two 30
  94. 94. Program Example of if else Statement Introduction to Java Programming Language Notes By Adil Aslam Checking two conditions here 1.Is number is less than 30 2.Is number is equal two 30 Note: Number is equal to 30
  95. 95. Program Example of if else Statement Introduction to Java Programming Language Notes By Adil Aslam Note Here
  96. 96. Program Example of if else Statement Introduction to Java Programming Language Notes By Adil Aslam Note Here Output is: Output is:
  97. 97. Problem Statement • Prompt user to enter a value. Check if the number is even then display a message that number is even. If number is odd then display message number is odd. Introduction to Java Programming Language Notes By Adil Aslam
  98. 98. Solution of the Previous Problem Introduction to Java Programming Language Notes By Adil Aslam
  99. 99. Solution of the Previous Problem Introduction to Java Programming Language Notes By Adil Aslam Output is
  100. 100. Solution of the Previous Problem Introduction to Java Programming Language Notes By Adil Aslam
  101. 101. Solution of the Previous Problem Introduction to Java Programming Language Notes By Adil Aslam Output is:
  102. 102. Explanation of Previous Program import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { int number; Scanner user_input = new Scanner(System.in); System.out.println("Please Enter a Number: "); number=user_input.nextInt(); if(number%2==0) { System.out.println((+number)+" is an Even Number"); } else { System.out.println((+number)+" is an odd Number"); } } } Introduction to Java Programming Language Notes By Adil Aslam Import Scanner Class to use its method to take input from user
  103. 103. Explanation of Previous Program import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { int number; Scanner user_input = new Scanner(System.in); System.out.println("Please Enter a Number: "); number=user_input.nextInt(); if(number%2==0) { System.out.println((+number)+" is an Even Number"); } else { System.out.println((+number)+" is an odd Number"); } } } Introduction to Java Programming Language Notes By Adil Aslam Import Scanner Class to use its method to take input from user Create an object using Scanner class
  104. 104. Explanation of Previous Program import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { int number; Scanner user_input = new Scanner(System.in); System.out.println("Please Enter a Number: "); number=user_input.nextInt(); if(number%2==0) { System.out.println((+number)+" is an Even Number"); } else { System.out.println((+number)+" is an odd Number"); } } } Introduction to Java Programming Language Notes By Adil Aslam Import Scanner Class to use its method to take input from user Create an object using Scanner class Take input from user
  105. 105. Explanation of Previous Program import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { int number; Scanner user_input = new Scanner(System.in); System.out.println("Please Enter a Number: "); number=user_input.nextInt(); if(number%2==0) { System.out.println((+number)+" is an Even Number"); } else { System.out.println((+number)+" is an odd Number"); } } } Introduction to Java Programming Language Notes By Adil Aslam Import Scanner Class to use its method to take input from user Create an object using Scanner class Take input from user Checking condition
  106. 106. Explanation of Previous Program import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { int number; Scanner user_input = new Scanner(System.in); System.out.println("Please Enter a Number: "); number=user_input.nextInt(); if(number%2==0) { System.out.println((+number)+" is an Even Number"); } else { System.out.println((+number)+" is an odd Number"); } } } Introduction to Java Programming Language Notes By Adil Aslam Import Scanner Class to use its method to take input from user Create an object using Scanner class Take input from user Checking condition Output is: Please Enter a Number: 10 10 is an Even Number
  107. 107. Explanation of Previous Program import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { int number; Scanner user_input = new Scanner(System.in); System.out.println("Please Enter a Number: "); number=user_input.nextInt(); if(number%2==0) { System.out.println((+number)+" is an Even Number"); } else { System.out.println((+number)+" is an odd Number"); } } } Introduction to Java Programming Language Notes By Adil Aslam Explain this statement in detail
  108. 108. Scanner user_input = new Scanner(System.in); • Scanner: The Scanner class is a class in java.util, which allows the user to read values of various types. • User_input: An object name defined by the user, you can use any object name like year, num, input Device and so on. • System.in: An Input Stream which is typically connected to keyboard input of console programs. • new Scanner (System.in): Creates a Scanner object that is connected to the System.in object. In other words, the created Scanner object is connected to the default input device. The keyword "new" is required by Java; you will use it whenever you create objects that are more complex than the simple datatypes. • The assignment operator in the Scanner declaration statement assigns the value of the new object--that is, its memory address--to the "console" object in the program. Introduction to Java Programming Language Notes By Adil Aslam
  109. 109. Scanner user_input = new Scanner(System.in); • Scanner: The Scanner class is a class in java.util, which allows the user to read values of various types. • User_input: An object name defined by the user, you can use any object name like year, num, input Device and so on. • System.in: An Input Stream which is typically connected to keyboard input of console programs. • new Scanner (System.in): Creates a Scanner object that is connected to the System.in object. In other words, the created Scanner object is connected to the default input device. The keyword "new" is required by Java; you will use it whenever you create objects that are more complex than the simple datatypes. • The assignment operator in the Scanner declaration statement assigns the value of the new object--that is, its memory address--to the "console" object in the program. Introduction to Java Programming Language Notes By Adil Aslam
  110. 110. Scanner user_input = new Scanner(System.in); • Scanner: The Scanner class is a class in java.util, which allows the user to read values of various types. • User_input: An object name defined by the user, you can use any object name like year, num, input Device and so on. • System.in: An Input Stream which is typically connected to keyboard input of console programs. • new Scanner (System.in): Creates a Scanner object that is connected to the System.in object. In other words, the created Scanner object is connected to the default input device. The keyword "new" is required by Java; you will use it whenever you create objects that are more complex than the simple datatypes. • The assignment operator in the Scanner declaration statement assigns the value of the new object--that is, its memory address--to the "console" object in the program. Introduction to Java Programming Language Notes By Adil Aslam
  111. 111. Scanner user_input = new Scanner(System.in); • Scanner: The Scanner class is a class in java.util, which allows the user to read values of various types. • User_input: An object name defined by the user, you can use any object name like year, num, input Device and so on. • System.in: An Input Stream which is typically connected to keyboard input of console programs. • new Scanner (System.in): Creates a Scanner object that is connected to the System.in object. In other words, the created Scanner object is connected to the default input device. The keyword "new" is required by Java; you will use it whenever you create objects that are more complex than the simple datatypes. • The assignment operator in the Scanner declaration statement assigns the value of the new object--that is, its memory address--to the "console" object in the program. Introduction to Java Programming Language Notes By Adil Aslam
  112. 112. Scanner user_input = new Scanner(System.in); • Scanner: The Scanner class is a class in java.util, which allows the user to read values of various types. • User_input: An object name defined by the user, you can use any object name like year, num, input Device and so on. • System.in: An Input Stream which is typically connected to keyboard input of console programs. • new Scanner (System.in): Creates a Scanner object that is connected to the System.in object. In other words, the created Scanner object is connected to the default input device. The keyword "new" is required by Java; you will use it whenever you create objects that are more complex than the simple datatypes. • The assignment operator in the Scanner declaration statement assigns the value of the new object--that is, its memory address--to the "console" object in the program. Introduction to Java Programming Language Notes By Adil Aslam
  113. 113. Problem Statement Write a Java program which inputs the marks obtained by student. If the marks are equal to or greater than 50 then display the message that “You are pass.” else “Sorry you are fail.” Introduction to Java Programming Language Notes By Adil Aslam
  114. 114. Solution of previous problem is : Introduction to Java Programming Language Notes By Adil Aslam
  115. 115. Your Task… • Write a Java program to check whether a triangle is valid or not, when the three angles of the triangle are entered by the user. A triangle is valid if the sum of all the three angles is equal to 180 degrees. Introduction to Java Programming Language Notes By Adil Aslam
  116. 116. Introduction to Java Programming Language Notes By Adil Aslam Solution of Previous Problem
  117. 117. Your Second Task.. Any year is input by the user. Write a program to determine whether the year is a leap year or not. Introduction to Java Programming Language Notes By Adil Aslam
  118. 118. Nested if else statement in java Introduction to Java Programming Language Notes By Adil Aslam
  119. 119. Nested if else Statement • When a if condition is included in the if block of some other if condition then those are called nested ifs. Introduction to Java Programming Language Notes By Adil Aslam if (...) { // some code if (...) { // some code } else{ // some code } } else{ // some code }
  120. 120. Introduction to Java Programming Language Notes By Adil Aslam if (Test Condition) { // some code if (Test Condition) { // some code } else{ // some code } } else{ // some code } If Test Condition is true
  121. 121. Introduction to Java Programming Language Notes By Adil Aslam if (Test Condition) { // some code if (Test Condition) { // some code } else{ // some code } } else{ // some code } If Test Condition is true If Test Condition is true
  122. 122. Introduction to Java Programming Language Notes By Adil Aslam if (Test Condition) { // some code if (Test Condition) { // some code } else{ // some code } } else{ // some code } If Test Condition is true If Test Condition is true If Test Condition is False
  123. 123. Introduction to Java Programming Language Notes By Adil Aslam if (Test Condition) { // some code if (Test Condition) { // some code } else{ // some code } } else{ // some code } If Test Condition is true If Test Condition is true If Test Condition is False If Test Condition is False
  124. 124. Program Example if Nested if else Statement Introduction to Java Programming Language Notes By Adil Aslam
  125. 125. Program Example if Nested if else Statement Introduction to Java Programming Language Notes By Adil Aslam Outer if
  126. 126. Program Example if Nested if else Statement Introduction to Java Programming Language Notes By Adil Aslam Outer if Inner if
  127. 127. Program Example if Nested if else Statement Introduction to Java Programming Language Notes By Adil Aslam Outer if Inner if In this program outer if condition is true but inner if condition is false. So outer if body execute and in inner if else, else body of inner if is execute.
  128. 128. Program Example if Nested if else Statement Introduction to Java Programming Language Notes By Adil Aslam Outer if Inner if In this program outer if condition is true but inner if condition is false. So outer if body execute and in inner if else, else body of inner if is execute. Output is:
  129. 129. Java if-else-if ladder Statement “Multiple Selection” Introduction to Java Programming Language Notes By Adil Aslam
  130. 130. The if...else if...else Statement The if-else-if ladder statement executes one condition from multiple statements. Syntax: Introduction to Java Programming Language Notes By Adil Aslam if(condition1){ //code to be executed if condition1 is true } else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false }
  131. 131. Flow Chart of if...else if...else Statement Introduction to Java Programming Language Notes By Adil Aslam
  132. 132. The if...else if...else Statement If student ’s grade is greater than or equal to 90 Print “ A ” else If student ’s grade is greater than or equal to 80 Print “ B ” else If student ’s grade is greater than or equal to 70 Print “ C ” else If student ’s grade is greater than or equal to 60 Print “ D ” else Print “ F ” Introduction to Java Programming Language Notes By Adil Aslam
  133. 133. The if...else if...else Statement • When using if , else if , else statements there are few points to keep in mind. • An if can have zero or one else's and it must come after any else if's. • An if can have zero to many else if's and they must come before the else. • Once an else if succeeds, none of the remaining else if's or else's will be tested. Introduction to Java Programming Language Notes By Adil Aslam
  134. 134. Program Example of if...else if...else Statement Introduction to Java Programming Language Notes By Adil Aslam
  135. 135. Program Example of if...else if...else Statement public static void main(String[] args) { int x = 30; if(x == 10) { System.out.println("Value of X is 10"); } else if(x == 20) { System.out.println("Value of X is 20"); } else if(x == 30) { System.out.println("Value of X is 30"); } else { System.out.println("This is else statement"); } } Introduction to Java Programming Language Notes By Adil Aslam
  136. 136. Problem Statement • Write a program which prompt the user to enter a digit between 1to 3 and display the digit entered by the user. Introduction to Java Programming Language Notes By Adil Aslam
  137. 137. Solution : Introduction to Java Programming Language Notes By Adil Aslam
  138. 138. Explanation of Previous Program import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { int number; Scanner user_input = new Scanner(System.in); System.out.println("Please Enter a Number: "); number=user_input.nextInt(); Introduction to Java Programming Language Notes By Adil Aslam Here Nothing new to Explain because already explain Everything
  139. 139. Explanation of Previous Program…. if(number==1) { System.out.println("You Pressed 1"); } else if(number==2) { System.out.println("You Pressed 2"); } else if(number==3) { System.out.println("You Pressed 3"); } else { System.out.println("You Pressed other than 1,2,3"); } } } Introduction to Java Programming Language Notes By Adil Aslam If you enter 1 then execute this statement
  140. 140. Explanation of Previous Program…. if(number==1) { System.out.println("You Pressed 1"); } else if(number==2) { System.out.println("You Pressed 2"); } else if(number==3) { System.out.println("You Pressed 3"); } else { System.out.println("You Pressed other than 1,2,3"); } } } Introduction to Java Programming Language Notes By Adil Aslam If you enter 1 then execute this statement If you enter 2 then execute this statement
  141. 141. Explanation of Previous Program…. if(number==1) { System.out.println("You Pressed 1"); } else if(number==2) { System.out.println("You Pressed 2"); } else if(number==3) { System.out.println("You Pressed 3"); } else { System.out.println("You Pressed other than 1,2,3"); } } } Introduction to Java Programming Language Notes By Adil Aslam If you enter 1 then execute this statement If you enter 2 then execute this statement If you enter 3 then execute this statement
  142. 142. Explanation of Previous Program…. if(number==1) { System.out.println("You Pressed 1"); } else if(number==2) { System.out.println("You Pressed 2"); } else if(number==3) { System.out.println("You Pressed 3"); } else { System.out.println("You Pressed other than 1,2,3"); } } } Introduction to Java Programming Language Notes By Adil Aslam If you enter 1 then execute this statement If you enter 2 then execute this statement If you enter 3 then execute this statement If you enter other than 1,2,3, then execute this statement
  143. 143. Explanation of Previous Program…. if(number==1) { System.out.println("You Pressed 1"); } else if(number==2) { System.out.println("You Pressed 2"); } else if(number==3) { System.out.println("You Pressed 3"); } else { System.out.println("You Pressed other than 1,2,3"); } } } Introduction to Java Programming Language Notes By Adil Aslam If you enter 1 then execute this statement If you enter 2 then execute this statement If you enter 3 then execute this statement If you enter other than 1,2,3, then execute this statement Output is: Please Enter a Number 3 You Pressed 3
  144. 144. Problem Statement • A bank gives loan in two situations • 1) If customer is male and his age is more than 25 and his salary is more than 25000 • 2) If customer is a female and her age is more than 30 and her income is 20000 • Program: Ask user to enter age, gender(M/F) and income. Check whether heshe is eligible to get loan. Introduction to Java Programming Language Notes By Adil Aslam
  145. 145. Problem Statement • A bank gives loan in two situations • 1) If customer is male and his age is more than 25 and his salary is more than 25000 • 2) If customer is a female and her age is more than 30 and her income is 20000 • Program: Ask user to enter age, gender(M/F) and income. Check whether heshe is eligible to get loan. Introduction to Java Programming Language Notes By Adil Aslam We solve this problem using if else if Statement with also use of logical Operators
  146. 146. Introduction to Java Programming Language Notes By Adil Aslam Solution of Previous Problem
  147. 147. Explanation of Previous Program import java.util.Scanner; public class Test { public static void main(String[] args) { int age, income; char gender; Scanner input=new Scanner(System.in); System.out.println("Please Enter Your Age: "); age=input.nextInt(); System.out.println("Please Enter Your Income: "); income=input.nextInt(); System.out.println("Please Enter Your Gender(M/F): "); gender=input.next().charAt(0); Introduction to Java Programming Language Notes By Adil Aslam
  148. 148. Explanation of Previous Program import java.util.Scanner; public class Test { public static void main(String[] args) { int age, income; char gender; Scanner input=new Scanner(System.in); System.out.println("Please Enter Your Age: "); age=input.nextInt(); System.out.println("Please Enter Your Income: "); income=input.nextInt(); System.out.println("Please Enter Your Gender(M/F): "); gender=input.next().charAt(0); Introduction to Java Programming Language Notes By Adil Aslam The next() method gets the next string of text that a user types on the keyboard.
  149. 149. Explanation of Previous Program import java.util.Scanner; public class Test { public static void main(String[] args) { int age, income; char gender; Scanner input=new Scanner(System.in); System.out.println("Please Enter Your Age: "); age=input.nextInt(); System.out.println("Please Enter Your Income: "); income=input.nextInt(); System.out.println("Please Enter Your Gender(M/F): "); gender=input.next().charAt(0); Introduction to Java Programming Language Notes By Adil Aslam The charAt(index) method returns the char at that index position in the String. The first character in the String is at index position 0, the next at index position 1, etc. The next() method gets the next string of text that a user types on the keyboard.
  150. 150. Explanation of Previous Program if(age > 25 && gender == 'M' && income > 25000){ System.out.println("Sir, You can apply for Loan"); } else if(age > 30 && gender == 'F' && income > 30000){ System.out.println("Madam, You can apply for Loan"); } else{ System.out.println("Sorry, You are not eligible for loan"); } } } Introduction to Java Programming Language Notes By Adil Aslam
  151. 151. Explanation of Previous Program if(age > 25 && gender == 'M' && income > 25000){ System.out.println("Sir, You can apply for Loan"); } else if(age > 30 && gender == 'F' && income > 30000){ System.out.println("Madam, You can apply for Loan"); } else{ System.out.println("Sorry, You are not eligible for loan"); } } } Introduction to Java Programming Language Notes By Adil Aslam If these three condition in both if’s are true then execute the body of the if statement otherwise execute the else statement
  152. 152. Explanation of Previous Program if(age > 25 && gender == 'M' && income > 25000){ System.out.println("Sir, You can apply for Loan"); } else if(age > 30 && gender == 'F' && income > 30000){ System.out.println("Madam, You can apply for Loan"); } else{ System.out.println("Sorry, You are not eligible for loan"); } } } Introduction to Java Programming Language Notes By Adil Aslam If these three condition in both if’s are true then execute the body of the if statement otherwise execute the else statement Body of if && operator
  153. 153. Explanation of Previous Program if(age > 25 && gender == 'M' && income > 25000){ System.out.println("Sir, You can apply for Loan"); } else if(age > 30 && gender == 'F' && income > 30000){ System.out.println("Madam, You can apply for Loan"); } else{ System.out.println("Sorry, You are not eligible for loan"); } } } Introduction to Java Programming Language Notes By Adil Aslam If these three condition in both if’s are true then execute the body of the if statement otherwise execute the else statement Body of if In && operator every condition must be true then output is true && operator
  154. 154. Explanation of Previous Program if(age > 25 && gender == 'M' && income > 25000){ System.out.println("Sir, You can apply for Loan"); } else if(age > 30 && gender == 'F' && income > 30000){ System.out.println("Madam, You can apply for Loan"); } else{ System.out.println("Sorry, You are not eligible for loan"); } } } Introduction to Java Programming Language Notes By Adil Aslam If these three condition in both if’s are true then execute the body of the if statement otherwise execute the else statement Body of if In && operator every condition must be true then output is true && operator Output is: Please Enter Your Age: 26 Please Enter Your Income: 26000 Please Enter Your Gender(M/F): M Sir, You can apply for Loan
  155. 155. Your Task… Write a Java program to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for up to 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls. Introduction to Java Programming Language Notes By Adil Aslam
  156. 156. switch statement in java Introduction to Java Programming Language Notes By Adil Aslam
  157. 157. Switch Statement in java • A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. • Syntax Introduction to Java Programming Language Notes By Adil Aslam switch(expression) { case value : // Statements break; // optional case value : // Statements break; // optional // You can have any number of case statements. default : // Optional // Statements }
  158. 158. The following Rules Apply to a Switch Statement • The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums. • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. • The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. Introduction to Java Programming Language Notes By Adil Aslam
  159. 159. The following Rules Apply to a Switch Statement • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. Introduction to Java Programming Language Notes By Adil Aslam
  160. 160. Flowchart of Switch Statement Introduction to Java Programming Language Notes By Adil Aslam
  161. 161. Break Statement in Java • In Java break statement is used for the termination of the loop statement and switch case statement. • Syntax of break Introduction to Java Programming Language Notes By Adil Aslam break;
  162. 162. Program Example of Switch Statement Introduction to Java Programming Language Notes By Adil Aslam
  163. 163. More Example of Switch Statement • This program is a simple calculator designed in java using switch case statement to perform some basic arithmetic operation on two integers entered by user. • It enables the user to enter a choice that corresponds to one of the four basic arithmetic operation, addition, subtraction, multiplication and division. The control structure used in this code is switch case, which works in the same manner as that in C or C++. Introduction to Java Programming Language Notes By Adil Aslam Simple Calculator Using Switch Case Statement In Java
  164. 164. Simple Calculator Using Switch Case Statement In Java….. import java.util.Scanner; public class MyFirstProgram { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("You Have Following Choices : "); System.out.println("1. Addition"); System.out.println("2. Subtraction "); System.out.println("3. Multiplication "); System.out.println("4. Division"); System.out.println("Enter Your Choice : "); int i=s.nextInt(); Introduction to Java Programming Language Notes By Adil Aslam
  165. 165. Simple Calculator Using Switch Case Statement In Java….. System.out.println("Enter First Number "); int a=s.nextInt(); System.out.println("Enter Second Number "); int b=s.nextInt(); //result' will store the result of operation double result=0; Introduction to Java Programming Language Notes By Adil Aslam
  166. 166. Simple Calculator Using Switch Case Statement In Java….. switch(i) { case 1: result=a+b; break; case 2: result=a-b; break; case 3: result=a*b; break; Introduction to Java Programming Language Notes By Adil Aslam
  167. 167. Simple Calculator Using Switch Case Statement In Java….. case 4: if(b==0) { //when denominator becomes zero System.out.println("Division Not Possible"); break; } else result=a/b; default: System.out.println("You Have Entered A Wrong Choice"); } System.out.println("Result = "+result); } } Introduction to Java Programming Language Notes By Adil Aslam
  168. 168. Outputs of Previous Program is : You Have Following Choices : 1. Addition 2. Subtraction 3. Multiplication 4. Division Enter Your Choice : 3 Enter First Number 3 Enter Second Number 4 Result = 12.0 Introduction to Java Programming Language Notes By Adil Aslam Compiler View Output
  169. 169. Switch Statement Using JOptionPane import javax.swing.JOptionPane; public class MyFirstProgram { public static void main(String[] args) { String s1, status; int code; s1 = JOptionPane.showInputDialog(“Please Enter a number between 1 or 2:"); code = Integer.parseInt(s1); Introduction to Java Programming Language Notes By Adil Aslam Take input as a String
  170. 170. Switch Statement Using JOptionPane import javax.swing.JOptionPane; public class MyFirstProgram { public static void main(String[] args) { String s1, status; int code; s1 = JOptionPane.showInputDialog(“Please Enter a number between 1 or 2:"); code = Integer.parseInt(s1); Introduction to Java Programming Language Notes By Adil Aslam parseInt convert string into integer Take input as a String
  171. 171. Switch Statement Using JOptionPane switch (code) { case 1: status = "Adil Aslam"; break; case 2: status = "Hina Ameen"; break; default: status = "An incorrect code was entered"; } JOptionPane.showMessageDialog(null, status, "Program", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } Introduction to Java Programming Language Notes By Adil Aslam When user enter 1 then execute this Statement
  172. 172. Switch Statement Using JOptionPane switch (code) { case 1: status = "Adil Aslam"; break; case 2: status = "Hina Ameen"; break; default: status = "An incorrect code was entered"; } JOptionPane.showMessageDialog(null, status, "Program", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } } Introduction to Java Programming Language Notes By Adil Aslam When user enter 1 then execute this Statement When user enter 2 then execute this Statement
  173. 173. Switch Statement Using JOptionPane Output is: Introduction to Java Programming Language Notes By Adil Aslam
  174. 174. Switch Statement Using JOptionPane Output is: Introduction to Java Programming Language Notes By Adil Aslam
  175. 175. Switch Statement Using JOptionPane Output is: Introduction to Java Programming Language Notes By Adil Aslam Please don’t Worry if you don’t understand the program of this output we lean about JOptionPane Class in detail Very Soon.!
  176. 176. Introduction to Java Programming Language Notes By Adil Aslam Thanks You 

×