Java Selection Structure if

Source: Internet
Author: User

if conditional statements are divided into three syntax formats, each of which has its own characteristics

1.if statements

An If statement is a sort of processing if a condition is met

Formula:

if (conditional statement) {

Execute the statement;

......

}

 1  public  class   pd{ 2   Public  static  void   main (string[] args) { 3  int  time = 5;  4  if  (Time<7" { 5  System.out.println ("The alarm is ringing, it's time to get up!") " 6  }  7  }  8 } 

Result: The output alarm will be ringing, it's time to get up!

2.if...else statements

if (judging condition) {

EXECUTE Statement 1

......

}else{

EXECUTE Statement 2

......

}

1  Public classPd {2 3      Public Static voidMain (string[] args) {4         intA = 4,b = 7;//define two int integer variables5         intMax;6         if(a>b) {7Max =A;8}Else{9Max =b;Ten System.out.println (max); One         } A     }       -}

Result: The output will be 7

3.if...else if...else Statements

if (judging condition 1) {

EXECUTE Statement 1

} else if (judging condition 2) {

EXECUTE Statement 2

}

...

else if (judging condition N) {

EXECUTE statement N

} else {

Execute Statement n+1

}

1  Public classpd{2      Public Static voidMain (string[] args) {3         intScore = 103;4         if(Score>=90 && score<=100){5System.out.println ("excellent");6}Else if(Score>=60 && score<90){7SYSTEM.OUT.PRINTLN ("Qualified");8}Else if(Score>=0 && score<60){9System.out.println ("Failed");Ten}Else{ OneSYSTEM.OUT.PRINTLN ("Error or exception"); A         } -     } -}

Result: Error or exception is output

Java Selection Structure if

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.