response.getWriter().append("Served at: ").append(request.getContextPath()doPost(request, response);

response.getWriter().append("Served at: ").append(request.getContextPath());
doPost(request, response);

Control statements in java with examples

  • Programs in java will be executed in sequential manner means line by line execution of statements.
  • By using control statements we can control the flow of execution.
  • We have three types of control statements in java.
  1. Decision making Statements.
  2. Iteration statements.
  3. Branching statements. 




1.Decision making statements in java: 

  • In some cases we will have a situation like need to execution set of statements based on a condition
  • In this scenario we need to use decision making statements in java.
  1. if
  2. if-else
  3. if-else-if
  4. switch

Simple if 

  • Based on some condition if we want to execute some set of statements then we need to use if condition in java.
  • We need to keep all the required statements inside if block .
  • If the condition is true then control executes the whole block otherwise it skips the if block of statements.

  1. if( condition ){
  2. // code
  3. }

Java example program on if control statement:

  1. package controlstatementsinjava;
  2. import java.util.Scanner;
  3.  
  4. public class ControlStatementsInjava {
  5.  
  6.     /**
  7.      * @website: www.instanceofjava.com
  8.      */
  9. public static void main(String[] args) {
  10.         
  11.   Scanner in= new Scanner(System.in);
  12.  
  13.   System.out.println("Please enter a number");
  14.   int n=in.nextInt();
  15.         
  16. if(n%2==0){
  17.    System.out.println(n+" is even number");
  18.         
  19.  }
  20.   
  21. if(n%2!=0){
  22.      System.out.println(n+" is odd number");
  23. }
  24.  
  25. }
  26. }

Output:

  1. Please enter a number
  2. 5
  3. 5 is odd number

If else condition:

  • If the condition is true then control executes the whole block otherwise it skips the if block of statements
  • if condition is false  if we need to execute another set of statement we can write another if and keep all the statements in that block.
  • instead of checking multiple times we can simply use else block .
  • More about in else statement in java

    1. if( condition ){
    2. // code
    3. }else{
    4. //code
    5. }

     if else control statement with example programs:

    1. package controlstatementsinjava;
    2. import java.util.Scanner;
    3.  
    4. public class ControlStatementsInjava {
    5.  
    6.     /**
    7.      * @website: www.instanceofjava.com
    8.      */
    9. public static void main(String[] args) {
    10.         
    11.   Scanner in= new Scanner(System.in);
    12.  
    13.   System.out.println("Please enter a number");
    14.   int n=in.nextInt();
    15.         
    16. if(n%2==0){
    17.    System.out.println(n+" is even number");
    18.         
    19.  }else{
    20.      System.out.println(n+" is odd number");
    21. }

    22.  
    23. }
    24. }

    Output:

    1. Please enter a number
    2. 5
    3. 5 is odd number


    if else ladder :

    • In some cases we may get requirement with multiple conditions in such cases we need to use if else ladder.(if -else-if-else if -else...)

    1. if( condition ){
    2. // code
    3. }else if{
    4. //code
    5. }else {
    6.  //code
    7. }

     if else if control statement with example programs:


    1. package controlstatementsinjava;
    2. import java.util.Scanner;
    3.  
    4. public class ControlStatementsInjava {
    5.  
    6.     /**
    7.      * @website: www.instanceofjava.com
    8.      */
    9. public static void main(String[] args) {
    10.         
    11.   Scanner in= new Scanner(System.in);
    12.  
    13.   System.out.println("Please enter your age");
    14.   int age=in.nextInt();
    15.         
    16. if(age<=18){
    17.    System.out.println("Your age is between 1-18");
    18.         
    19.  }else if ((age>=18) && (age<=35)){
    20.      System.out.println(" Your age is between 18-35");
    21. }
    22. else if ((age>=35) && (age<=50)){
    23.      System.out.println(" Your age is between 35-50");
    24. }else{
    25.  
    26.   System.out.println(" Your age is above 50");
    27.  
    28. }
    29.  
    30. }
    31. }

    Output:

    1. Please enter your age
    2. 20
    3. Your age is between 18-35

    control%2Bstatements%2Bin%2Bjava

    Switch statement in java:

    • Instead if writing number of if conditions we can use single switch statement


    1. switch(value)
    2.  case n1: statements;
    3. break;
    4. case n2 : statements;
    5. break;
    6. default: statements;
    7. }

    Java example program on switch statement in java:


    switch%2Bstatement%2Bin%2Bjava

    Data types in java with example programs

    • The data type is something which gives information about
    • Size of the memory location and range of the data that can be accommodated inside that location.
    • Possible legal operations those can be performed on that location.
    • What type of result come out from an expression when we use these types inside that expression.
    • Whichever the keyword gives these semantics is treated as "data type".
    • The java language provides different categories of data types. 

    1.Primitives Data types:

    • The primitive data types are the predefined data types given by the programming language and they are meant for storing a single value.
    • Based on the type and range of data, primitive types are divided into 8 types.

    1.Integer category: 

    • This category can be used for storing numbers which can be either positive value  or negative value without decimal points.
    • In this category we have 4 primitive data types whose memory sizes are different. 
    • All the 4 types under this category are used for storing same data. But there ranges are different. The java language is providing 4 types under Integer category. So that the memory is utilized efficiently. 
    1. byte  
    2. short
    3. int
    4. long 
    intdatatypes


    byte data type in java:

    • Size 1 byte. i.e 8 bits
    • Minimum value is -128 (-2^7)
    • Maximum value is 127 (2^7 -1)
    • Default value is 0
    Java example program on byte data type:

    1. package com.instanceofjava;
    2. Class ByteDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6.  byte x=10;
    7.  byte y=20;
    8.  
    9.   byte z= (byte) (x+y);
    10.   System.out.println(z);
    11.  
    12. }

    13. }

    Output:

    1. 30 

     short data type in java:

    • Size 2 bytes.
    • Minimum value is -32,768 (-2^15)
    • Maximum value is 32,767 (2^15-1)
    • Default value is 0

    Java example program on short data type:

    1. package com.instanceofjava;
    2. Class ShortDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6.  short x=10;
    7.  short y=20;
    8.  
    9.   short z= (short) (x+y);
    10.   System.out.println(z);
    11.  
    12. }

    13. }

    Output:

    1. 30

    int data type in java:

    • Size 4 bytes.
    • Minimum value is - 2,147,483,648.(-2^31)
    • Maximum value is 2,147,483,647 (2^31 -1)
    • Default value is 0

    Java example program on int data type:

    1. package com.instanceofjava;
    2. Class IntDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6.  int x=10;
    7.  int y=20;
    8.  
    9.   int z=x+y;
    10.   System.out.println(z);
    11.  
    12. }

    13. }

    Output:

    1. 30

    long data type in java:
    • Size 8 bytes.
    • Minimum value is -9,223,372,036,854,775,808.(-2^63)
    • Maximum value is 9,223,372,036,854,775,807 (2^63 -1)
    • Default value is 0

    Java example program on long data type:

    1. package com.instanceofjava;
    2. Class LongDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6.  long x=109876677777l;
    7.  long y=20666677766l;
    8.  
    9.  long z=x+y;
    10.  
    11.  System.out.println(z);
    12.  
    13. }

    14. }



    Output:

    1. 130543355543

    2.Floating point category: 

    • This category can be used for storing numbers which can be either +VE or –VE with decimal point. In the floating point category we have two types whose size is different. The two data types are float and double.
    • Both the data types under the floating point category are used for storing same data but there range is different. The java language provide as two data types under floating point category so that memory is utilized efficiently. 
    • Default value of float is 0
    • Default value of double is 0.0 
    1. float
    2. double


    floatdatatypes

    float data type in java:

    • Size 4 bytes.
    • Minimum value is 1.4e-45
    • Maximum value is 3.4e38
    • Default value is 0.0f

    Java example program on float data type:

    1. package com.instanceofjava;
    2. Class FloatDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6.  float x=231231.0031f;
    7.  float y= 53423423434.43231f;

    8.  float z= x+y; 

    9.  System.out.println(z);
    10.  
    11. }

    12. }

    Output:

    1. 5.3423653E10

    double data type in java:
    • Size 8 bytes.
    • Minimum value is 4.9e-324
    • Maximum value is 1.8e308
    • Default value is 0.0d

    Java example program on double data type:

    1. package com.instanceofjava;
    2. Class DoubleDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6.  double x=231231.0031f;
    7.  double y= 53423423434.43231f;

    8.  double z= x+y; 

    9.  System.out.println(z);
    10.  
    11. }

    12. }

    Output:

    1. 5.342365466543541E10

    3.Character category: 

    • This category can be used for storing a single character. A character can be represented by alphabets, a digit and special symbols. 
    • This category contains only one data type an it is char.
    • Default value of char is "one space"
    1. char

    char data type in java:
    • Size 2  bytes.
    • Minimum value is 0
    • Maximum value is 65,535
    • Default value is ' '

    Java example program on char data type:

    1. package com.instanceofjava;
    2. Class CharDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6.  char a='a';
    7.  System.out.println(a);
    8.  
    9. }

    10. }


    Output:

    1. a

    4.Boolean category: 

    • This category can be used for storing either true or false. Under the Boolean category only one data type an it is Boolean. The size is dependent on JVM to JVM.

    boolean data type in java:

    • boolean data type represents one bit of information.
    • There are only two possible values: true and false.
    • This data type is used for simple flags that track true/false conditions.
    • Default value is false. '

    Java example program on boolean data type:

    1. package com.instanceofjava;
    2. Class BooleanDemo {
    3.  
    4. public static void main(String[] args) {
    5.  
    6. boolean x = false;
    7. System.out.println(x);
    8.  
    9. }

    10. }

    Output:

    1. false