catalogue
Characteristics of cyclic structure
Cyclic structure
Cycle in life
Characteristics of cyclic structure
while Loop
while(Conditional expression) { //Cyclic content }
If the conditional expression is true, execute the loop body. Once the conditional expression is false, the loop stops.
practice
1. Write a program to realize the function and calculate the number between 100 and 10000. The sum of each number is 7 (e.g. 241:2 + 4 + 1 = 7)
//count int count = 0; int number = 100; while (number <= 10000) { int allDigits /*Wan Wei*/ = number / 10000; int thousandsDigits /*Thousand bit*/ = number / 1000 % 10; int hundredsDigits /*Hundredth*/ = number / 100 % 10; int tenDigits /*Ten*/ = number / 10 % 10; int singleDigits /*Bit*/ = number % 10; if (allDigits + thousandsDigits + hundredsDigits + tenDigits + singleDigits == 7) { count++; } number++; } Console.WriteLine("Altogether " + count + " individual");
2. Print out all even numbers between 1-100
//Declare a variable, print even numbers, and start with 2 int number = 2; while (number <= 100) { if (number%2==0) { Console.WriteLine(number+" Even number"); } number++; }
3. Print 1 + 2 + 3 ++ Value of 100
//Declare a variable store and int sum = 0; //Declare an initial value for the condition of the loop int num = 1; while (num <=100) { sum += num; num++; } Console.WriteLine("After adding: "+sum);
break
In the switch statement
Function: jump out of switch statement
Circulating body
Function: jump out of this layer loop (usually used with if)
Practice
It is realized by while loop and break, and the number is input in the terminal loop,
Once the input number is less than 0, stop the input
while (true) { Console.WriteLine("please enter a number:"); int num = int.Parse(Console.ReadLine()); if (num < 0) { Console.WriteLine("Program stop"); break; } }
continue
Role in the cycle:
End this cycle (continue. The following code will no longer be executed),
Enter the next cycle. (usually used with f)
practice
Use the continue keyword to print 1-100, which is not a multiple of 3
//Declare the initial value of the cycle int num = 1; while (num < 100) { num++; if (num % 3 == 0) { continue; } Console.WriteLine(num); }
task
1. Input an arithmetic expression from the keyboard, use the if statement to realize the correct arithmetic operation, and output the operation result
- for example
- please enter the first number: 4
- please enter operator:+
- please enter the first number: 2
- the calculation result is: 6
Console.Write("Please enter the first number:\t"); int num = int.Parse(Console.ReadLine()); Console.Write("Please enter an operator:\t"); string str /*operator*/ = Console.ReadLine(); Console.Write("Please enter the second number:\t"); int number = int.Parse(Console.ReadLine()); switch (str) { case "+": Console.WriteLine(num + "+" + number + "=" + (num + number)); break; case "-": Console.WriteLine(num + "-" + number + "=" + (num - number)); break; case "*": Console.WriteLine(num + "*" + number + "=" + (num * number)); break; case "/": Console.WriteLine(num + "/" + number + "=" + (num / number)); break; default: Console.WriteLine("Failed to read operator"); break; }
2. Input a birthday date and output its constellation
ARIES: 3.21-4.19, Taurus: 4.20-5.20, Gemini: 5.21-6.21, cancer: 6.22-7.22
Teacher: 7.23-8.22, Virgo: 8.23-9.22, Libra: 9.23-10.23, Scorpio: 10.24-11.22
Sagittarius: 11.23-12.21, Capricorn: 12.22-1.19, Aquarius: 1.20-2.18, Pisces: 2.19-3.20
- for example
- please enter month: 12
- please enter day: 21
- you're a Sagittarius
Console.WriteLine("Please enter month: "); int month /*month*/ = int.Parse(Console.ReadLine()); Console.WriteLine("Please enter the day: "); byte day /*date*/ = byte.Parse(Console.ReadLine()); switch (month) { case 1: if (day < 20) { Console.WriteLine("You are a Capricorn"); } else { Console.WriteLine("You are an Aquarius"); } break; case 2: if (day < 19) { Console.WriteLine("You are an Aquarius"); } else { Console.WriteLine("You are Pisces"); } break; case 3: if (day < 21) { Console.WriteLine("You are Pisces"); } else { Console.WriteLine("You are an Aries"); } break; case 4: if (day < 20) { Console.WriteLine("You are an Aries"); } else { Console.WriteLine("You are Taurus"); } break; case 5: if (day < 21) { Console.WriteLine("You are Taurus"); } else { Console.WriteLine("You are a Gemini"); } break; case 6: if (day < 22) { Console.WriteLine("You are a Gemini"); } else { Console.WriteLine("You are cancer"); } break; case 7: if (day < 23) { Console.WriteLine("You are cancer"); } else { Console.WriteLine("You are a Leo"); } break; case 8: if (day < 23) { Console.WriteLine("You are a Leo"); } else { Console.WriteLine("You are Virgo"); } break; case 9: if (day < 23) { Console.WriteLine("You are Virgo"); } else { Console.WriteLine("You are a Libra"); } break; case 10: if (day < 24) { Console.WriteLine("You are a Libra"); } else { Console.WriteLine("You are a Scorpio"); } break; case 11: if (day < 23) { Console.WriteLine("You are a Scorpio"); } else { Console.WriteLine("You are a Sagittarius"); } break; case 12: if (day < 22) { Console.WriteLine("You are a Sagittarius"); } else { Console.WriteLine("You are a Capricorn"); } break; default: Console.WriteLine("Out of range!"); break; }
3. Print out all even numbers between 1-100
//initial value int number = 2; while (number <= 100) { Console.WriteLine(number+" It's an even number"); number += 2; }
4. Program to print out all "daffodils" and print their total number.
-"Daffodils number" is the sum of the cubes of each bit, which is equal to the three digits of the integer
//initial value int number = 100; //Counter int count = 0; while (number < 1000) { // Hundredth int hundreds = number / 100; // Ten int tensDigit = number / 10 % 10; // Bit int singleDigit = number % 10; if (hundreds * hundreds * hundreds + tensDigit * tensDigit * tensDigit + singleDigit * singleDigit * singleDigit == number) { //Counter increase count++; Console.WriteLine("Number:\t" + number); } number++; } Console.WriteLine("The total number is: "+count);
Do..While loop
do { //Cyclic content } while(Conditional expression);
Execute the loop body first, and then judge the loop conditions. The loop ends when the conditions are not met.
practice
sum=1+2+3+..+N; Find the maximum integer value and term value n with sum not exceeding 10000.
//Storage and int sum = 0; //initial value int number = 1; do { //Sum sum += number; //Digital increment number++; } while (sum < 10001); //Back off number--; sum -= number; Console.WriteLine("Sum:\t" + sum); Console.WriteLine("Number:\t" + number);
for loop
for(Loop variable initialization; Cycle conditions; Cyclic increment) { //Circulatory body }
If the loop condition is true, execute the loop body. If the cycle condition is false, jump out of the cycle.
practice
Print out the number between 1 and 100 that is not a multiple of 7 and does not contain 7 with fo.
for (int i = 1; i <= 100; i++) { //Condition: it is not a multiple of 7, and the ten bits do not contain seven if (i % 7 != 0 && i / 10 % 10 != 7 && i % 10 != 7) { Console.WriteLine("Number:\t" + i); } }
Loop nesting
for (int i = 1; i <= 3; i++) { for (int j = 1; j <=i; j++) { Console.Write(j); } //Wrap every run Console.WriteLine(); }
Practice
Print multiplication formula table
for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { Console.Write(j + "*" + i + "=" + i * j + "\t"); } Console.WriteLine(); }
summary
1. for is most commonly used. It is usually used to know the number of cycles.
2. while is also commonly used. It is usually used for loops that do not know the number of cycles.
3. do.while is not particularly common and is usually used for loops that need to be executed once first.
4. break jumps out of the loop of this layer. continue: ends the loop. It is usually used with if.