Java Basic Syntax & Conditional statements.

Ad Code

Java Basic Syntax & Conditional statements.

Java basic syntax

You will understand the basic syntax of Java programming language with following example.
public class Myprg {
}

This code will create a class called Myprg. Class name must start with a capital letter.
public static void main(String[] args)
{
.....................
}

This is the main block of code. The program will start from the main method. The word public means it is accessible by other classes in the program. The word static keyword will make a method to become a static method. The static method is not belonged to any object of the class. You can access a static method without creating an instance of the class. The word void means it returns nothing.
String [] args is called argument which is used to pass value (s) to the program.


Java User Input (Scanner)

The Scanner class is used to get user input from standard input device, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In this example, we will use the nextLine() method, which is used to read Strings:

Java Input types

MethodDescription
nextBoolean()Reads abooleanvalue from the user
nextByte()Reads abytevalue from the user
nextDouble()Reads adoublevalue from the user
nextFloat()Reads afloatvalue from the user
nextInt()Reads aintvalue from the user
nextLine()Reads aStringvalue from the user
nextLong()Reads alongvalue from the user
nextShort()Reads ashortvalue from the user



Example1

import java.util.Scanner; // Import the Scanner class
class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in); // Create a Scanner object
     System.out.println("Enter username");
     String userName = myObj.nextLine(); // Read user input
     System.out.println("Username is: " + userName); // Output user input
   }
}


Output
Enter username
Rakesh
Username is: Rakesh



System.out.println in Java

Java System.out.println() is used to print an argument on standard output device. The statement can be broken into 3 parts which can be understood separately as:

  1. System: It is a final class defined in the java.lang package.
  2. out: This is an instance of PrintStream type, which is a public and static member field of the System class.
  3. println(): As all instances of PrintStream class have a public method println()


Java Conditional statements

A conditional statement is a statement that used to decide which code has to be run when the condition is true or which code has not to be run when the condition is false.

The if statement

The if statement checks a condition and if it is true then it runs the line of code that follows it.
Example1:

public class Ifstatement
{
   public static void main(String[] args)
  {
    int i = 4;
    if (i%2 == 0)
     System.out.println("i is an even number");
  }
}


Example2:

public class Ifstatement
{
   public static void main(String[] args)
   {
    int x = 20;
    int y = 18;
    if (x > y)
     System.out.println("x is greater than y");
   }
}


Example3

import java.util.Scanner;
class MyClass {
   public static void main(String[] args) {
    Scanner Obj = new Scanner(System.in);
    System.out.println("Enter Number");
    int n = Obj.nextInt();
    if(n%2==0)
    {
     System.out.println(n+" is Even");
   }
  }
}


The if.....else Statement

Use the if....else statement to specify a block of code to be executed if the condition is false.
Example

import java.util.Scanner;
class MyClass {
   public static void main(String[] args) {
    Scanner Obj = new Scanner(System.in);
    System.out.println("Enter Number");
    int n = Obj.nextInt();
    if(n%2==0)
    {
     System.out.println(n+" is Even");
    }
    else{
     System.out.println(n+" is Odd");
   }
  }
}


Output
Enter Number
5
5 is Odd

In the example above, value of n is 5, so the condition is false. Because remainder of 5 is not 0, so move on to the else condition and print to the screen 5 is Odd

The Nested if Statement

Use the nested if statement to specify a new condition if the first condition is false.
Syntax
if (condition1) {
   // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else if (condition...n) {
  // block of code to be executed if the condition1& 2 is false and condition...n is true
} else {
// block of code to be executed if all condition is false
}

Example

import java.util.Scanner;
class MyClass {
   public static void main(String[] args) {
    Scanner Obj = new Scanner(System.in);
    System.out.println("Enter Number");
    int n = Obj.nextInt();
    if(n>0 && n<10)
    {
     System.out.println(n+" is Single digited");
    }
    else if(n>9 && n<100){
     System.out.println(n+" is Double digited");
   }
    else if(n>99 && n<1000){
     System.out.println(n+" is Triple digited");
   } else{
     System.out.println(n+" is Multiple digited");
   }
  }
}



Ternary Operator

It is a part of Java's conditional statements and consists of a condition that evaluates to either true or false. It can be used to replace multiple lines of code with a single line.
Syntax
variable = (condition) ? expressionTrue : expressionFalse;
Example

import java.util.Scanner;
class MyClass {
   public static void main(String[] args) {
    Scanner Obj = new Scanner(System.in);
    System.out.println("Enter Two Numbers");
    int n = Obj.nextInt();
    int p = Obj.nextInt();
    int r =(n>p) ? n:p;     {
     System.out.println("Max="+r);
   }
  }
}



Java Switch Statements

It is used to select one of many code blocks to be executed.
Syntaxswitch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

  • The break Keyword: This will stop the execution of more code and case testing inside the block.
  • The default Keyword:This statement is used as the last statement in a switch block, it does not need a break. This statement is optional and can appear anywhere inside the switch block.
import java.util.Scanner;
public class SwitchExample {
 public static void main(String[] args) {
  Scanner Obj = new Scanner(System.in);
   int number=Obj.nextInt();
   switch(number){
    case 10: System.out.println("Ten");break;
     case 20: System.out.println("Twenty");break;
    case 30: System.out.println("Thirty");break;
     default:System.out.println("Non of these");
  }
 }
}


 




Post a Comment

0 Comments

Ad Code