C # study notes 03-- cycle and one-dimensional arrays

A. Cycle (focus)

When a cycle?

Want a piece of code is executed multiple times, the code may be different, but there must be a law.

1.while cycle

format: 

while (loop condition)

{

  Code loop implementation;

}

Loop mechanism: when the loop condition is true, the code executes the loop body, and then determine the loop condition is true continues this process, knowing judged loop condition is false, and out of the termination of the entire while loop.

note: 

1. Be sure to define a loop variable in the outside world;

2. Be sure to change the loop variable inside the loop!    

These two points are indispensable, otherwise it will fall into an infinite loop!

 

Exercise:

1> a random number input from the keyboard, this number of bits is obtained

Console.WriteLine("请输入一个数:");
int num = int.Parse(Console.ReadLine());
int i = 0;
while (num != 0)
 {
       num /= 10;
        i++;
}
Console.WriteLine(i);

 

2> a random number input from the keyboard, this binary number is obtained

 int I = . 1 , C = 0 ; 
Console.WriteLine ( " Please output a number: " );
 int NUM = int .Parse (Console.ReadLine ());
 the while (! NUM = 0 ) 
{ 
        C + = (NUM% 2 ) * I; 
        I * = 10 ; 
        NUM = NUM / 2 ; 

}

 

 

2.do while loop

format:

do

{

  Cyclic code to be executed;

} While (loop condition)

 

Exercise

1. find that the sum = 1 + 2 + 3 + 4 + ... + N largest integer less than 10000 N

int SUM = 0 , n-= . 1 ;
 do 
{ 
       SUM + = n-; 
       n- ++ ; 
} the while (SUM <= 10000 ); 
n- - ; 
SUM - = n-; 
n- - ; 
Console.WriteLine ( "" + n-+ sum);
 // should be noted here n must be reduced twice, first to pay more because the sum of the values of n, resulting in value Sum of printing out more than 10000, n minus one is because critical that the sum n value should be one.

 

 

3.for cycle

format:

for (initializes loop variable ; cycling conditions ; loop variable changes itself)

{

  Code loop implementation;

 

 

Exercise

1. Print 1-100 includes neither the number is not a multiple of 7 and 7

for (int i = 1; i <= 100; i++)
{
       if (!(i % 7 == 0 || i / 10 == 7 || i % 10 == 7))
       {
             Console.WriteLine(i);
        }
}

 

 

2. Print 1-100 odd sum

int sum = 0;
for (int i = 1; i <= 100; i++)
{
        if (i % 2 != 0)
        {
               sum += i;
         }
}
Console.WriteLine(sum);

 

 

3.2continue 和 break

continue is used to terminate this cycle, that is to say if you encounter continue during program execution then the code following the code cycles continue execution would not be carried out; jump directly to what a loop, that is to "loop variable itself change";

break is used to terminate the cycle, during the execution of the program if they break, then regardless of the conditions have not reached the end of the cycle, the entire cycle (this level) will be stopped, ahead of the end of life;

 Exercise

1. Enter a number from the keyboard, it determines whether the number is a prime number;

int num = int.Parse(Console.ReadLine());
bool isB = false;
for (int i = 2; i < num / 2; i++)
{
       if (num % i == 0)
       {
             isB = true;
             break;
        }
}
if (!isB)
{
      Console.WriteLine("是质数");
}

 

2. Print Graphics 

 

 

            for (int i = 1; i <= 4; i++)
            {
                for (int j = 1; j <= 4 - i; j++)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= i; k++)
                {
                    Console.Write("* ");
                }
                Console.WriteLine();
            }
            for (int i = 3; i >= 1; i--)
            {
                for (int j = 3; j >= i; j--)
                {
                    Console.Write(" ");
                }
                for (int k = 1; k <= i; k++)
                {
                    Console.Write("* ");
                }
                Console.WriteLine();
            }        

 

 

3. Print the multiplication table

            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("{0} * {1} = {2}   ", j, i, j * i);
                }
                Console.WriteLine();
            }    

 

 

How to select these types of recycling it?

We know the number of cycles using a for loop

Know cycling conditions using a while loop

This does not necessarily have to like, but this choice makes the code more clear and concise and logical point.

 

II. One-dimensional array

 

Array definition:

1. Data Type [] = new Array name Data type [array size] [{element 1, element 2, ...}]; start may not be assigned to

2. The data array is located the same type of container;

3. The form of access array is subscripted by an array name [index]      index starting from 0 ;

4. The index array must not be out of range;

The length of the array must be specified, either directly specified (second write directly on the length of the bracket), or indirectly specify (in parentheses the second length is not written, but must be assigned, the assignment of the number of automatically determines the length of the array );

6. The array as a whole, is not directly involved in the operation, '=' except number;

 

foreach

Special traverse the collection of elements (read-only, can not be rewritten only use it)

format:

foreach (type variable in the collection name)

{

  Code;

}

 

Guess you like

Origin www.cnblogs.com/skylar-AKS/p/11945055.html