Java syntax (5): condition Control

Source: Internet
Author: User
Java syntax introduction (5): Conditional Control-general Linux technology-Linux programming and kernel information. The following is a detailed description. Conditional Control: if and switch

1: Condition Statement

A condition statement is the most commonly used statement in programming. It is used to select the execution process of the program. The basic condition judgment statement in Java is an if... else... statement. The organization is:

If (conditional expression)
{
Statement 1
}
Else
{
Statement 2

}
Statement 3

If the value of "conditional expression" is true, execute "Statement 1", then execute Statement 3; otherwise, execute "Statement 2" and then execute Statement 3.

The following is an example of using the if -- else statement to construct a multi-branch program:

If (a> 5) B = 1
Else if (a> 4) B = 2
Else if (a> 3) B = 3
...
Else B =-1;

2: switch branch structure (switch statement)

The switch branch structure is used for multi-Condition Selection. You can use if... else... structure, but the use of the switch statement will make the program more refined and clear. The format of the switch statement is:

Switch (conditional expression)
{
Case constant expression 1:
Statement 1;
Break;

Case constant expression 2:
Statement 2;
Break;
//......
Case constant expression n:
Statement n;
Break;
Default:
//
Break
}

The switch expression first calculates the value of the condition expression. If the value is equal to a constant expression, the statement after the constant expression is executed. If the value is not equal to the value of all constant expressions, the statement after defualt is executed.

In a switch statement, you usually use the break statement in each case. Otherwise, all the statements after the first case of equality will be executed. This is called failure. You can add and remove the break statement to execute the following example:

Class Switch Test

Public static void main (String args [])
Throws java. io. IOException

Char;
System. out. println ("Enter a number from 1--3 :");
A = (char) System. in. read ();
Switch ()

Case '1': System. out. println ("win a Car! "); Break;
Case '2': System. out. println ("picked the goat"); break
Case '3': System. out. println ("get to keep your 100 ");
Break;
Default: System. out. println ("entry ");

After adding the break statement in the code, you should be clear about the changes that will occur in the program and confirm that the program is not transferred to the code you do not want to execute.
After you have mastered these basic statements, you can write a program that is more practical. The program below calculates the numbers and characters entered from the terminal.

Class SwitchTest

Public static void main (String args [])
Throws java. io. IOException

Int numberOfDigits = 0, numberOfSpaces = 0, numberOfOthers = 0;
Char c;
While (c = (char) System. in. read ())! = '')

Switch (c)

Case '0 ':
Case '1 ':
...
...
Case '8 ':
Case '9': numberOfDigits ++; break;
Case '': numberOfSpaces ++; break;
Default: numberOfOthers ++; break;


System. out. println (":");
System. out. println ("Number of digits =" + numberOfDigits + "");
System. out. println ("Number of spaces =" + numberOfSpaces + "");
System. out. println ("Number of others =" + numberOfOthers + "");

The while loop of the Code reads characters from the keyboard until a carriage return is input. The switch statement inside the loop first compares it with a number. When it is found to be equal, it adds the value of the corresponding statistical variable to 1, then the break statement ends the switch statement, and the program returns to the status waiting for keyboard input. In the program, the break in the default statement is unnecessary, but adding it can make the program style consistent.
Related Article

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.