à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday 8 October 2017

Basic JAVA 6


Basic JAVA 6

Java If-else Statement

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


1) if statement

The if statement tests the condition. It executes the if statement if condition is true.

Syntax:

if(condition){ 
        //code to be executed 

Example:

public class IfExample {  
public static void main(String[] args) {  
    int age=20;  
    if(age>18){  
        System.out.print("Age is greater than 18");  
    }  
}  
}  
Output:

Age is greater than 18

2) if else statement

The if-else statement also tests the condition. It executes the if block if condition is true otherwise else block.

Syntax:

if(condition){
        //code if condition is true
}else{
        //code if condition is false

Example:

public class IfElseExample {  
public static void main(String[] args) {  
    int number=13;  
    if(number%2==0){  
        System.out.println("even number");  
    }else{  
        System.out.println("odd number");  
    }  
  }  
}  
Output:

odd number

3) if else if ladder statement

The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

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

Example:

public class IfElseIfExample {  
public static void main(String[] args) {  
    int marks=65;  
      
    if(marks<50 span="" style="color: #7f0055; font-weight: bold;">System
.out.println("fail"); } else if(marks>=50 && marks<60 span="" style="color: #7f0055; font-weight: bold;">System.out.println("D grade"); } else if(marks>=60 && marks<70 span="" style="color: #7f0055; font-weight: bold;">System.out.println("C grade"); } else if(marks>=70 && marks<80 span="" style="color: #7f0055; font-weight: bold;">System.out.println("B grade"); } else if(marks>=80 && marks<90 span="" style="color: #7f0055; font-weight: bold;">System.out.println("A grade"); }else if(marks>=90 && marks<100 span="" style="color: #7f0055; font-weight: bold;">System.out.println("A+ grade"); }else{ System.out.println("Invalid!"); } } } Output: C grade 

Java Switch Statement

The Java switch statement is executes one statement from multiple conditions. It is like if-else-if ladder statement.

Syntax:

switch(expression){
          case value1:
                           //code to be executed;
          break; //optional
          case value2:
                          //code to be executed;
          break; //optional
          ......

          default:
                    code to be executed if all cases are not matched;

Example:

public class SwitchExample {  
public static void main(String[] args) {  
    int number=20;  
    switch(number){  
    case 10: System.out.println("10");break;  
    case 20: System.out.println("20");break;  
    case 30: System.out.println("30");break;  
    default:System.out.println("Not in 10, 20 or 30");  
    }  
}  
}  
Output:

20

Switch statement if fall-through

The java switch statement is fall-through. It means it executes all statement after first match if break statement is not used with switch cases.

Example:

public class SwitchExample2 {  
public static void main(String[] args) {  
    int number=20;  
    switch(number){  
    case 10: System.out.println("10");  
    case 20: System.out.println("20");  
    case 30: System.out.println("30");  
    default:System.out.println("Not in 10, 20 or 30");  
    }  
}  
}  
Output:

20
30
Not in 10, 20 or 30

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website