4.1 Boolean logic
Boolean comparison operators
= = = < > <= >=
Boolean operator that handles Boolean values
! & | ^ (XOR)
Conditional Boolean operators
&& | | Better performance than & and | && just judge the previous Boolean value to be false, the overall value is false, and you don't have to calculate the following Boolean value
1. Boolean Assignment operators
&= |= ^=
2. Bitwise operators
& | ^ ~
Displacement Operators >> <<
Displacement assignment operator >>= <<=
3. Operator precedence (UPDATE)
++,--(used as a prefix), (), +,-(unary),!, ~
*,/,%
+,-
<<,>>
<,>,<=,>=
==,!=
&
^
|
&&
||
=,*=,/=,%=,+=,-=,>>=,<<=,&=,^=,|= assignment operator
++,--(used as a suffix)
4.2 Goto Statement
Goto <labelName>
4.3 branches
1. Ternary operators
? :
2.if statements
if () {}else{}
3.switch statements
Switch ()
{
Case VAL1: ...; Break
Case VAL2: ...; Break
......
Default: If there is no matching Val value, have default, execute the code in default
}
Declare constants: Specify the variable type and keyword const, and you must assign values to them.
4.4. Looping: Repeating statement execution
1.do Cycle
Do
{Execute once to determine the value within while (), true to execute again, false to exit the loop
}while ();
2.while Cycle
while () {} first determines the value within while (), and true starts execution
3.for Cycle
for (int i=0;i<4,i++) {}
4. Cyclic interrupts
Break: Terminate Loop immediately
Continue: Immediately terminates the current loop and enters the next loop
Goto: Jumps out of the loop to the specified marker position
Return: Jump out of the loop and the function that contains the loop
5. Infinite loops
while (true) {} Exits with break, etc.
Mandelbrot Collection Example (the sample code given in the book is in C #)
classProgram {//each location in the Mandelbrot image corresponds to a complex number in the formula N=x+y*i. In fact, the number of parts is x, the imaginary part is y,i-1 square root. The x and Y coordinates of each position in the image correspond to the x and y parts of the imaginary numbers//each position in the image is represented by the parameter n, which is the square root of the x*x+y*y. If the value is greater than or equal to 2, the position value for this number is 0. If the value of parameter n is less than 2, change the value of N to N*n-n (n= (x* x-y* y-x) + (2*x* y-y) *i) and test the new N value again. If the value is greater than or equal to 2, the position value for this number is 1. This process continues until we assign a value to the position in the image, or the iteration executes more times than the specified number of times. " Static voidMain (string[] args) { //the real and imaginary part of N DoubleRealcoord, Imagcoord; //temporary information stored in the calculation process Doublerealtemp, Imagtemp, REALTEMP2, ARG; //record number of iterations before parameter N (ARG) is equal to or greater than 2 intiterations; //Select the appropriate boundary value to display the main part of the Mandelbrot image, and if you want to enlarge the image, you can enlarge the boundary values. for(Imagcoord =1.2; Imagcoord >=-1.2; Imagcoord-=0.05) { //Two for loop processes a point in the image and assigns a value to N. for(Realcoord =-0.6; Realcoord <=1.77; Realcoord + =0.03) { //Initialize Variablesiterations =0; Realtemp=Realcoord; Imagtemp=Imagcoord; Arg= (Realcoord * realcoord) + (Imagcoord *Imagcoord); //2 is the square root of 4, so only the value of x^2+y^2 is computed, while loop executes the iteration, while(Arg <4) && (Iterations < +)) { //the real part of the N*n-nREALTEMP2 = (realtemp * realtemp)-(IMAGTEMP *imagtemp)-Realcoord; //n*n-n part of the imaginary numberImagtemp = (2* Realtemp * imagtemp)-Imagcoord; Realtemp=REALTEMP2; Arg= (Realtemp * realtemp) + (IMAGTEMP *imagtemp); //The value of the current point is stored in iterationsIterations + =1; } //Select the characters to output Switch(Iterations%4) { Case 0: Console.Write ("."); Break; Case 1: Console.Write ("o"); Break; Case 2: Console.Write ("O"); Break; Case 3: Console.Write ("@"); Break; } } //An end line is required after the inner loop ends, so the output line break. Console.Write ("\ n"); } console.readkey (); } }
The demo results are:
Chapter exercises require the user to enter the bounds of the image and display the selected portion of the image. The word Fu Ching of the current code output can be placed exactly on one line of the console application, considering how to make each selected image occupy exactly
Space of the same size to maximize the viewable area.
classProgram {//each location in the Mandelbrot image corresponds to a complex number in the formula N=x+y*i. In fact, the number of parts is x, the imaginary part is y,i-1 square root. The x and Y coordinates of each position in the image correspond to the x and y parts of the imaginary numbers//each position in the image is represented by the parameter n, which is the square root of the x*x+y*y. If the value is greater than or equal to 2, the position value for this number is 0. If the value of parameter n is less than 2, change the value of N to N*n-n (n= (x* x-y* y-x) + (2*x* y-y) *i) and test the new N value again. If the value is greater than or equal to 2, the position value for this number is 1. This process continues until we assign a value to the position in the image, or the iteration executes more times than the specified number of times. " Static voidMain (string[] args) { //the real and imaginary part of N DoubleRealcoord, Imagcoord; DoubleRealmax =1.77; DoubleRealmin =-0.6; DoubleImagmax =-1.2; DoubleImagmin =1.2; DoubleRealstep; DoubleImagstep; //temporary information stored in the calculation process Doublerealtemp, Imagtemp, REALTEMP2, ARG; //record number of iterations before parameter N (ARG) is equal to or greater than 2 intiterations; //Select the appropriate boundary value to display the main part of the Mandelbrot image, and if you want to enlarge the image, you can enlarge (actually reduce) these boundary values. while(true) { //set the span to ensure that each selected image occupies exactly the same size of space to maximize the viewable area. realstep = (realmax-realmin)/79; Imagstep = (imagmax-imagmin)/48; for(Imagcoord = imagmin; Imagcoord >= imagmax; Imagcoord + =imagstep) { //Two for loop processes a point in the image and assigns a value to N. for(Realcoord = realmin; Realcoord <= realmax; Realcoord + =realstep) { //Initialize Variablesiterations =0; Realtemp=Realcoord; Imagtemp=Imagcoord; Arg= (Realcoord * realcoord) + (Imagcoord *Imagcoord); //2 is the square root of 4, so only the value of x^2+y^2 is computed, while loop executes the iteration, while(Arg <4) && (Iterations < +)) { //the real part of the N*n-nREALTEMP2 = (realtemp * realtemp)-(IMAGTEMP *imagtemp)-Realcoord; //n*n-n part of the imaginary numberImagtemp = (2* Realtemp * imagtemp)-Imagcoord; Realtemp=REALTEMP2; Arg= (Realtemp * realtemp) + (IMAGTEMP *imagtemp); //The value of the current point is stored in iterationsIterations + =1; } //Select the characters to output Switch(Iterations%4) { Case 0: Console.Write ("."); Break; Case 1: Console.Write ("o"); Break; Case 2: Console.Write ("O"); Break; Case 3: Console.Write ("@"); Break; } } //An end line is required after the inner loop ends, so the output line break. Console.Write ("\ n"); } //Current boundary valueConsole.WriteLine ("Current Limits:"); Console.WriteLine ("realcoord:from {0} to {1}", Realmin, Realmax); Console.WriteLine ("imagcoord:from {0} to {1} \ n", Imagmin, Imagmax); //Enter a new boundary valueConsole.WriteLine ("Enter new limits:"); //Real numbers Console.WriteLine ("Realcoord:from:"); Realmin = Convert.todouble (Console.ReadLine ()); Console.WriteLine ("Realcoord:to:"); Realmax = Convert.todouble (Console.ReadLine ()); //Imaginary numbers Console.WriteLine ("Imagcoord:from:"); Imagmin = Convert.todouble (Console.ReadLine ()); Console.WriteLine ("Imagcoord:to:"); Imagmax = convert.todouble (Console.ReadLine ()); } } }
Original boundary ( -0.6,1.2) (1.77,-1.2)
Current Frontier ( -0.6,1.2) (0,0)
The equivalent of enlarging the part of the original image: presumably this part? I can only understand that at this point.
C # Getting Started classic Chapter4 Process Control