Java Basics: Data type, switch statement, branch structure __java

Source: Internet
Author: User

casts of data types

public class example2{

int i=97;

Char c= (char) i; Cast of type

System.out.println ("c=" +c);

}

Hundred Record Entry dialog box

Import javax.swing.joptionpane;//dialog box to eject the method in the required package

public class testcondinitionaloperator{

public static void Main (String args[]) {

int score;

Score=integer.parseint (Joptionpane.showinputdialog ("Please enter hundred score:"); the//integer.parseint () method converts the string identity of a number to its equivalent signed integer.

String result= (score>=60)? " Pass ":" Fail ";

SYSTEM.OUT.PRINTLN (result);

}

}

range of data types: .....

Process Control Statements

No matter which programming language, there are 3 basic process Control structures-sequential structure, branching structure, and loop structure. Where the branching structure is used to implement a certain piece of code selectively according to the condition, the loop structure is used to repeat a piece of code based on the loop condition.

Sequential structure: the most common program structure in any programming language is the sequential structure. The sequential structure is executed from top to bottom, and the preceding code executes first, followed by the following code. This means that if there is no process control, the language room in the Java method executes the stream sequentially, executing each statement from top to bottom (that is, sequentially according to the order in which the statement appears).

Branching structure: Select one of the two branches or branches to perform according to the criteria. First determine the conditions in the diamond box, when the condition is set up, execution < statement 1>, when the condition is not tenable, execution < statement 2>.

Loop structure: repeat execution when a condition is met, knowing that the condition is not satisfied. First of all, determine the conditions in the diamond box, when the condition is set up, repeated execution of the < statement, until the condition does not set the exit loop, execution of the next basic structure.

Java also provides statements for implementing branching and looping structures.

Spoke statements: if and switch

Loop statements: While, do-while, for, and foreach;

Where the Foreach loop is a new circular statement provided by JDK5.0, it can traverse the elements of the collection and the array in a simple way.

public class testiferror{

public static void Main (String args[]) {

int score=85;

if (score>=90) {

System.out.println ("excellent");

}else if (score>=80) {

System.out.println ("good");/note the order of if, else fi, and if reversed, only pass the statement

}else if (score>=60) {

SYSTEM.OUT.PRINTLN ("Pass");

}

}

}

if the communication of the IF condition and the else condition is calculated every time, in order to avoid the above error, there is a basic rule when using the If////else statement--always limited to handle the condition that contains the small scope. such as score>=80 and score>=60 conditions, obviously score>=80 range is smaller, so should first deal with the situation of score>=80.

Switch Branch Statement

A switch statement consists of a control expression and multiple case labels. And if statements do not make sense, the data type of the control expression following the Swirch statement can only be a shape or character, not a Boolean. Case label followed by a slap code block, case label as the code block identifier, the switch statement syntax format is as follows:

Switch (expression) {

Case condition1:{

Statement (s)

Break ;

 

}

Case condition2:{

Statement (s)

Break ;

}

... ..

Case condition:{

Statement (s)

Break ;

}

default:{

state,emt (s)

}

The execution of this branch statement is to search for the expression first, then to match the condition1,condition2...conditionn equivalent, and to meet the matching value that executes the corresponding execution body. If the value after all case labels is not equal to the value of the expression expression, the code block for the default label is executed.

Public class testswitch{

Public static void Main (String args[]) {

//Declare the variable score and assign it a value of ' C '

Char grade= ' C ';

//EXECUTE Switch Branch statement

switch (grade) {

Case ' A ':

System.out.println ("excellent");

Break ;

Case ' B ':

System.out.println ("good");

Break ;

Case ' C ':

System.out.println ("Zhong");

Break ;

Case ' D ':

System.out.println ("Pass");

Break ;

Case ' E ':

System.out.println ("fail");

Break ;

Case ' F ':

System.out.println ("excellent");

Break ;

Default:

System.out.println ("Score input error");

              }

       }

run the full program and the output is "medium". This result is completely normal, the character expression score the value is C, the corresponding result is ' medium '.

It is worth noting that the type of control expression in a switch statement can only be byte, short, char, and int, not a string, which is not in C #.

After each block of code after the case label there is a break; The statement has a very important meaning. Java switch statements allow you to omit the break of a code block after a case, but this omission may introduce a trap. Spit out the break in the above program, the statements are commented out, the results will be different. The results seem odd, but that's exactly how the SWITVH statement runs: The switch statement first gets the value of the expression expression and then compares the expression with the value after the case tag, and once it encounters an equal value, The program changes to start the code after the case label, and no longer judge with the case of cotton, default label conditions when the Buddha-even match, unless encountered break;

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.