第四章 流程控制
本章内容:
- 布尔逻辑的含义及其用法
- 如何控制代码的分支
- 如何编写循环代码
分支是有条件的执行代码,条件根据计算结果来定。
循环重复执行相同的语句。
4.1 布尔逻辑
关系运算符
运算符 | == | != | < | > | <= | >= |
类别 | 二元 | 二元 | 二元 | 二元 | 二元 | 二元 |
运算符 | ! | & | | | ^ | && | || |
类别 | 一元 | 二元 | 二元 | 二元 | 二元 | 二元 |
4.1.1 布尔赋值运算符
布尔赋值运算符把布尔比较与赋值组合起来。
运算符 | &= | |= | ^= |
类别 | 二元 | 二元 | 二元 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter an integer:");
int myInt = Convert.ToInt32(Console.ReadLine());
bool isLessThan10 = myInt < 10;
bool isBetween0And5 = (0 <= myInt) && (myInt <= 5);
Console.WriteLine("Integer less than 10?{0}",isLessThan10);
Console.WriteLine("Integer between 0 and 5?{0}",isBetween0And5);
Console.WriteLine("Exactly one of the above is true?{0}",isLessThan10^isBetween0And5);
Console.ReadKey();
}
}
}
4.1.2 按位运算符
取反运算符:~(一元)
左移运算符:<<(二元);右移运算符:>>(二元)