Conditional control: IF and switch
1: Conditional statement
Conditional statement is the most commonly used statement in the program design, using him to select the execution flow of the program, the basic condition judgment statement in Java is if...else ... Statement. Its institutions are:
if (条件表达式)
{
语句1
}
Else
{
语句2
}
语句3
Execute statement 1 when the value of the conditional expression is true, and then execute Statement 3, otherwise EXECUTE statement 2, and then execute Statement 3.
Here is an example of constructing a multiple-branch program with a if--else statement:
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 multiple condition selection, although in the case of multiple conditions selection, you can use If...else ... structure, but using a switch statement makes the program more refined and clear. The format of the switch statement is:
switch(条件表达式)
{
case 常量表达式1:
语句1 ;
break ;
case 常量表达式2:
语句2 ;
break ;
//......
case 常量表达式n:
语句n ;
break ;
default:
//
break
}
The switch expression first evaluates the value of the conditional expression, and if its value equals a constant expression, executes the statement after the constant expression, if its value is not equal to the value of all the constant expressions, and executes the defualt statement.
In a switch statement, you usually use the break statement after each case situation, otherwise all statements after the first equality case are executed, which is called a failure. You can try adding and removing the break statement separately to perform the following example:
class Switch Test
public static void main(String args[])
throws java.io.IOException
char a;
System.out.println("Enter a number from 1--3:");
a=(char)System.in.read();
switch(a)
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 you add a break statement to your code, you should know exactly what will happen to the program, and make sure that the program does not move to code that you do not want to execute.
After you have mastered these basic statements, you can write more practical programs, and the following program calculates the number of digits 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 entered, the switch statement inside the loop first compares it to the number, and when found equal, it causes the corresponding statistic variable to be 1, and then the break statement ends the switch statement, and the program returns to the state of the keyboard input. In a program, the break in the default statement is unnecessary, but plus it can keep the program style consistent.