C# study notes–logical statements (branch and loop)
Logical statement
Conditional branch statement
Conditional branch statements can branch the sequentially executed code logic and execute the corresponding code logic when the corresponding conditions are met.
IF statement
//IF statement block
int a=5;
if(a>0&&a= 10)
{
Console.WriteLine("a3 is greater than or equal to 10");
}
else if( a3 > 5 && a3 = 0 && a3 <= 5 )
{
Console.WriteLine("a3 is between 0 and 5");
}
else
{
Console.WriteLine("a is less than 0");
}
//For beginners, the code logic should be neat and well-proportioned to facilitate comparison of the pairing of nested logic statement blocks.
A little exercise in if statements – distinguishing odd and even numbers
try
{
console.writeline("Please enter an integer");
int num = int.parse(console.readline());
//A number that is divisible by 2 is called an even number
if (num % 2 == 0)
{
console.writeline("your input is even");
}
else
{
console.writeline("your input is odd");
}
}
catch
{
console.writeline("Please enter a number");
}
Knowledge of statement blocks
The logical statement enclosed by {} is a code block. Pay attention to the life cycle of the variables in the code block
//Understanding of statement blocks
//The life cycle of variables caused by statement blocks
//Variables declared in the statement block can only be used in the current statement block
//Understand the meaning of the error reported by the current code in the compiler!
int a = 1;
int b = 2;
{
int b = 3;
Console.WriteLine(a);
Console.WriteLine(b);
}
Console.WriteLine(b);
int a = 5;
if (a > 3)
{
int b = 0;
++b;
b += a;
}
Console.WriteLine(b);
Switch statement
When there are too many judgment conditions, when using if elseif to make a judgment, you need to write multiple elseifs, which is lengthy and cumbersome. This reflects the advantage of the switch branch statement – clarity
//switch statement
int a=2;
switch(a)
{
//This condition must be a constant
case 1:
Console.WriteLine("a equals 1");
break;//Each condition is separated by break
case 2:
Console.WriteLine("a equals 2");
break;
case 3:
Console.WriteLine("a equals 3");
break;
default://can be omitted, default selection condition
Console.WriteLine("No conditions are met, execute the content in default");
break;
}
string str = "123";
switch (str)
{
case "123":
Console.WriteLine("equal to 123");
break;
case "234":
Console.WriteLine("equal to 234");
break;
}
//Use throughout
//When a variable satisfies multiple conditions at the same time, it can perform "merging" judgment of multiple conditions.
//Find a home for the variable number - if a relevant and acceptable one is found, it will be matched directly.
//Otherwise, it will continue to match the next case condition
string name="Changzhi";
switch(name)
{
//As long as it meets one of the three conditions
case "changzhi":
case "TonyChang":
case "Xiao Ming":
Console.WriteLine("He's a handsome guy!");
break;//break has a blocking effect
case "小玉":
case "Lily":
Console.WriteLine("She is a beauty!");
break;
default:
break;
}
Exercises on using switch: Binning of student scores
//Enter the student’s test scores, if
//Grade >= 90: A
//90 > Score >= 80:B
//80 > Score >= 70:C
//70 > Score >= 60:D
//score < 60:E
//(done using switch syntax)
//Finally output the student's test grade
try
{
Console.WriteLine("Please enter student grades");
int cj = int.Parse(Console.ReadLine());
// Get its tens digit
// 100 / 10 = 10
// 99 / 10 = 9
// 84 / 10 = 8
// 74 / 10 = 7
// cj = cj / 10;
cj /= 10;
switch (cj)
{
case 10:
case 9:
Console.WriteLine("Your grade is A");
break;
case 8:
Console.WriteLine("Your grade is B");
break;
case 7:
Console.WriteLine("Your grade is C");
break;
case 6:
Console.WriteLine("Your grade is D");
break;
default:
Console.WriteLine("Your grade is E");
break;
}
}
catch
{
Console.WriteLine("Please enter a number");
}
Loop statement
Loops can repeatedly execute logic that satisfies the loop execution conditions.Be careful not to write infinite loops casually.
while loop
//while loop
int a=1;
while(a<10)//Loop condition
{
++a;
}
Console.WriteLine(i);
// Nested use of loops
int a1=1;
int b=0;
while (a1 < 10)
{
++a1;
while (b < 10)
{
++b;
}
}
//Use of break
//break can be the execution logic point to jump out of the while statement block
while(true)
{
Console.WriteLine("code before break");
break;
Console.WriteLine("code after break");
}
Console.WriteLine("Code outside the loop");
//Use of continue
//Make the execution logic point jump out of the current loop content
//Directly enter the next cycle judgment execution
//Print odd numbers between 1 and 20
int index = 0;
while(index %
if (index % 2 == 0)
{
continue;//Skip even cases
}
Console.WriteLine(index);
}
Exercise–find all prime numbers within 100 and print them
//Find all prime numbers within 100 and print them.
int num = 2;
while(num < 100)
{
//Use the number you want to judge to be a prime number. Start from 2 and take the remainder. If it is divisible halfway, it proves that it is not a prime number.
// If it accumulates to the same number as itself, it proves that it is a prime number.
int i = 2;
while( i < num )
{
//Determine whether to divide
if( num % i == 0 )
{
break;
}
++i;
}
if( i == num )
{
Console.WriteLine(num);
}
++num;
}
doWhile loop
The do…while statement is similar to the while loop, except that this guy is too reckless. He cuts it first and plays it later. No matter what, the code block is executed first and then the condition is judged
//do while loop simple application
string userName = "";
string passWord = "";
bool isShow = false;
do
{
//This code will definitely not be executed for the first time
if(isShow)
{
Console.WriteLine("Username or password is wrong, please re-enter");
}
//loop input
Console.WriteLine("Please enter username");
userName = Console.ReadLine();
Console.WriteLine("Please enter your password");
passWord = Console.ReadLine();
isShow = true;
} while (userName != "Changzhi" || passWord != "666");
for loop
The for loop is the most commonly used loop statement,
//for loop
for( int i = 10; i >= 0; i-- )
{
Console.WriteLine(i);
}
//Each space can be written according to the rules
//Note: The semicolon cannot be omitted, even if there is no variable declaration!
//The first empty space declares variables. Multiple declarations can be made at the same time.
//The second vacancy judgment condition return value is bool
//The third vacancy operates on variables
for( int i = 0, j = 0; i < 10 && j < 0; ++i, j = j + 1)
{
}
//Special use of for loop
// for loop can write an infinite loop
//for( ; ; )
//{
// Console.WriteLine("for loop infinite loop");
//}
int k = 0;
for(; k = 10 )
// {
// break;
// }
//}
Classic exercises for for loops:
Generally, we are looking for the correspondence between the execution result of the logic block to be executed and the loop condition variable
//Output the following 10 * 10 hollow star square matrix on the console
//************
//* *
//* *
//* *
//* *
//* *
//* *
//* *
//* *
//************
//OK
for (int j = 0; j < 10; j++)
{
//List
for (int i = 0; i 2i - 1 9 10 - i
// *** 2 3 -> 2i - 1 8 10 - i
// ***** 3 5 7 10 - i
// ******* 4 7 6 10 - i
// ********* 5 9 5
//********** 6 11 4
//********** 7 13 3
// *************** 8 15 2
//**************** 9 17 1
//********************** 10 19 0 10 - i
//OK
for (int i = 1; i <= 10; i++)
{
//Print columns with spaces
for (int k = 1; k <= 10 - i; k++)
{
Console.Write(" ");
}
//Print the column of asterisks
for (int j = 1; j <= 2*i-1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
//Output the multiplication table on the console
for (int i = 1; i <= 9; i++)
{
//1 1 X 1 = 1 blank line
//2 1 X 2 = 2 2 X 2 = 4 blank lines
//3 1 X 3 = 3 2 X 3 = 6 3 X 3 = 9 blank line
for (int j = 1; j <= i; j++)
{
Console.Write("{0}X{1}={2} ", j, i, i * j);
}
Console.WriteLine();
}
//Find the sum of all even numbers between 1 and 100
int sum = 0;
for (int i = 1; i <= 100; i++)
{
//Determine whether it is an even number and whether it can be divided by 2
if( i % 2 == 0 )
{
sum += i;
}
}
for (int i = 2; i <= 100; i += 2)
{
sum += i;
}
Console.WriteLine(sum);
//2 1 X 2 = 2 2 X 2 = 4 blank lines
//3 1 X 3 = 3 2 X 3 = 6 3 X 3 = 9 blank line
for (int j = 1; j <= i; j++)
{
Console.Write("{0}X{1}={2} ", j, i, i * j);
}
Console.WriteLine();
}
//Find the sum of all even numbers between 1 and 100
int sum = 0;
for (int i = 1; i <= 100; i++)
{
//Determine whether it is an even number and whether it can be divided by 2
if( i % 2 == 0 )
{
sum += i;
}
}
for (int i = 2; i <= 100; i += 2)
{
sum += i;
}
Console.WriteLine(sum);