The third day, the branch judgment loop
1. Switch ()
(1) The parentheses must be of type int Char
(2) Break is optional, its role is to exit switch this whole, if not added, after the break of the case, the default after the statement will be used directly.
(3) default appears only once, the position is generally at the end, actually can appear freely.
(4) Switch after the parentheses fill in the value is less than or equal to the value of the int type, you can also fill in the string, must all case is a string type.
(5) Switch and if. Elseif comparison:
If....else If the comparative diversity is higher. And switch just does the equivalent comparison.
2. Cyclic structure:
for (with more)
while (second)
Do....while (less used)
for (initialization part; compare part; state change part)
{ loop statement ;}
comparison part: Refers to the conditions to continue to run, non-conforming to jump out for .
Feature: Executes a piece of code repeatedly until a state arrives and stops execution.
The name of the variable should have a certain meaning, at least such a name is not recommended, such as a,b,a1,b1.
The expression that controls the number of cycles is not written correctly and can easily lead to an infinite loop (or a dead loop). In most cases, infinite loops are meaningless, but as a server programming, an infinite loop is needed.
for (int i = ten; i>0;i--) This I is only valid in for. At the end of the for loop I also need to be redefined.
For,while: Is the first judgment after the execution of Do....while: first execution after judgment
Such as:
int i = 1;
do{
System.out.print (i+ ",");
i++;
}while (i<0);
will be output.
Break: In the loop the meaning is, interrupt, interrupt, (current) loop flow. When you want to break a non-current loop: (plus a logical name).
For example:
Public classTest1 { Public Static voidMain (string[] args) {out: for(inti = 0; I < 4; i++) { for(intj = 0; J < 4; J + +) {System.out.print ("[" + i + "," + j + "]"); if(i = = 2 && j = = 2) { BreakOut ; }} System.out.println (""); } }}
After adding the out logical name, it exits directly to the logical name.
Continue: The next time the current loop continues. This means that when the program executes to continue, it executes the next loop directly, ignoring the continue of the loop after the change.
For example
for (int j = 10;j>0; j-- ) { if (J ==8) { continue; } J--; System.out.print (J+ ","); }
j--and system will jump directly to j=7 after J==8 is no longer executed.
3. Arrays and Strings:
Both arrays and strings are types, both of which are object types. (separate from the base data type).
(1) Array:
What is an array: a bunch of variables of the same type? They are concentrated together and managed uniformly.
The Main learning grammar:
Defining Arrays (3 types)
Int[] Ages = new int[34];//is called the ages array. [] is the length of the array
Int[] Ages1 = {18,19,20};//It is not recommended to use this definition. Ages1 = {1,2,3,4}; This is a syntax error. Re-assignment is error-prone.
int[] Ages2 = new int[]{18,19,20};
Some terms for arrays
length : Is the number of values in the array
Elemental element: is each specific value, the professional argument is called the element (the element of the array).
Subscript (Index)index: is the position of an element in an array. The position is counted starting from 0 .
an array of primitive types, if not assigned, the default value is 0 on the upper descriptor. The boolean corresponds to False.
an array of object types, the default value is null.
For example:
Public class Test1 { publicstaticvoid main (string[] args) { int New int [a]; New String[3]; System.out.println ("[" +ages[0]+ "]"); System.out.println ("[" +s[0]+ "]");} }
The output is: [0]
[NULL]
Use it
Int[] Ages = new int[3];//is called the ages array.
AGES[3] = 20;
compile no problem but run an error ArrayIndexOutOfBoundsException. Since the array length is 3, it is counted starting from 0
Advanced use (traversal, remove all information from the array)
public class Test1 { public static void main (string[] args) { int [] ages = new int [3 0] = 10; ages[ 1] = 20; ages[ 2] = 30; for (int Age:ages) {System.out.println (age + ","
The Foreach Loop method is used: for (array type definition name: Array ontology) simple traversal method. Pros: Simple writing, Cons: only incremental traversal lacks flexibility.
In general:
Public class Test1 { publicstaticvoid main (string[] args) { int New int [3]; ages[0] = ten;//Assignment ages[1] =; ages[2] =; for (int i =0;i<ages.length; i++ ) { System.out.print ("[" +ages[i]+ "],");}} }
Collocation: Loop
For,while,do...while
foreach Loop, a new feature added after jdk1.5
Advanced some
4. String
String
Definition method: Defines a variable of type string (more precisely the definition of a string object)
String str1 = "abc";
There is also a method of defining a variable (object) that is unique to the object type, and the keyword is new.
String str2 = new String ("BBB");
Construction Method:
String (String)
String (char[])
Common methods:
char charAt (int)
int Length ()
Char[] ToCharArray ()
Object types have more or less built-in methods (functions) that provide some logic. Example: Number of strings (length)
int len = Str1.length ();
Gets the character of a position (subscript) in a string
Char first = Str1.charat (0);//0 character of a string
Char last = Str1.charat (len-1);//FINAL character
String variable character array
char[] ch = str1.tochararray ();
Java 100 days from entry to the ground, third day