Operators, expressions and statements

A base content
substantially the same as the expression 1 operators and C
to add here is that previously did not understand the bitwise operators:

. a bitwise AND operator & binocular operator, calculates two bit integer data, a result of the operation is integer data, which the algorithm is: if the data corresponding to the two bits are 1, then the results of the bit is 1, otherwise 0
example:
m: 00000000 00000000 00000000 00000111
n-: 10000001 10100101 11110011 10101011
result y: 00000000 00000000 00000000 00000011

b bitwise OR operator |. it is a binary operator, algorithms and the like &

c. Press ~ operator is non-bit unary operators, one calculates a integer data bit

. D bitwise exclusive OR operator ^ is a binary operator, the algorithm: if two bits corresponding to the same data, the result of the bit is 0, otherwise 1

2. Add operator: instanceof Operator
The operator is binary operator, the left operand is an object, a class is right. When the left to the right is the object class or subclass created when the result of the operator is ture, false otherwise

3. Basic statements
here and C are identical. But java provides a loop through the array: for (loop variable declaration: name of the array) {...}

II. Practical experience

In fact, I think the focus of this chapter is to give us the learning algorithm design and lay a foundation, we have to learn the characteristics of the transfer algorithm will learn here, or use java to design new algorithms, in fact, programming, algorithms are most key areas, but also the most difficult to break place.

Example: to write an application seeking all primes less than 100

import java.util.Scanner;
public class Part3_4_2
 {
    public static void main(String args[])
      {
         int start , end ;
         Scanner reader  = new Scanner(System.in);
         end = reader.nextInt();
         int n = 0;
         int i = 0;
          for (start = 1; start < end; start += 2) 
          {
               int k = (int) Math.sqrt(start);
               for (i = 2; i <= k; i++) {
                   if (start % i == 0)
                       break;
              }
              if (i >= k + 1) 
               {
                  System.out.println(start);
                  n++;
               }
          }
     }
}
Published 35 original articles · won praise 0 · Views 1321

Guess you like

Origin blog.csdn.net/c1776167012/article/details/100628017