软件开发新手入门五大核心技能之基础编程能力(二)

简介: 教程来源 http://yvyus.cn/ 本章详解运算符与表达式、控制流程核心机制:涵盖算术(含整除陷阱)、关系、逻辑、位运算及优先级规则;深入if-else嵌套、增强switch、while/do-while、多形态for循环及break/continue高级用法,夯实编程逻辑基础。

第二章 运算符与表达式 —— 让数据产生交互

2.1 算术运算符的细节与陷阱

// Java算术运算符
int a = 17;
int b = 5;

System.out.println(a + b);   // 22
System.out.println(a - b);   // 12
System.out.println(a * b);   // 85
System.out.println(a / b);   // 3(整数除法,截断小数)
System.out.println(a % b);   // 2(取余数)

// 整数除法的陷阱
double result = 17 / 5;      // 结果是3.0,不是3.4!
// 原因:两边都是整数,先执行整数除法得3,再转double
double correct = 17.0 / 5;   // 3.4,浮点数除法
double correct2 = (double)17 / 5;  // 显式转换

// 负数取余
System.out.println(-17 % 5);   // -2
System.out.println(17 % -5);   // 2
// 规则:结果的符号与被除数相同

// 复合赋值运算符
a += 5;   // 等价于 a = a + 5
a -= 3;   // a = a - 3
a *= 2;   // a = a * 2
a /= 4;   // a = a / 4
a %= 3;   // a = a % 3

2.2 关系运算符与逻辑运算符

# Python关系运算符
x = 10
y = 20

print(x < y)   # True
print(x <= y)  # True
print(x > y)   # False
print(x >= y)  # False
print(x == y)  # False
print(x != y)  # True

# 链式比较(Python特色)
print(5 < x < 15)     # True,等价于 5 < x and x < 15
print(5 < x > 8)      # True,注意这不是数学表达式

# 逻辑运算符
a = True
b = False

print(a and b)   # False(与)
print(a or b)    # True(或)
print(not a)     # False(非)

# 短路求值(Short-circuit evaluation)
def risky_operation():
    print("This won't be called")
    return 1 / 0  # 不会执行

False and risky_operation()  # 左侧False,短路,右侧不执行
True or risky_operation()    # 左侧True,短路,右侧不执行

# 利用短路实现默认值
name = input("Enter name: ") or "Anonymous"  # 如果输入为空字符串,使用默认值

2.3 位运算符(底层优化利器)

// C语言位运算(同样适用于Java、C++等)
unsigned int a = 0b1100;  // 12
unsigned int b = 0b1010;  // 10

// 按位与:两个都为1才为1
printf("%d\n", a & b);     // 0b1000 = 8

// 按位或:至少一个为1则为1
printf("%d\n", a | b);     // 0b1110 = 14

// 按位异或:不同为1,相同为0
printf("%d\n", a ^ b);     // 0b0110 = 6

// 按位取反
printf("%u\n", ~a);        // 0b...11110011(高位全变1)

// 左移:乘以2的n次方
printf("%d\n", a << 1);    // 0b11000 = 24
printf("%d\n", a << 2);    // 0b110000 = 48

// 右移:除以2的n次方(整数除法)
printf("%d\n", a >> 1);    // 0b0110 = 6

// 实际应用:权限系统
#define READ   0b100  // 4
#define WRITE  0b010  // 2
#define EXEC   0b001  // 1

int permission = READ | WRITE;  // 0b110 = 6
if (permission & READ) {
    printf("Can read\n");  // 执行
}
if (permission & EXEC) {
    printf("Can exec\n");  // 不执行
}

// 应用:快速判断奇偶
int isOdd = (x & 1);  // 最后一位为1则是奇数

// 应用:交换两个数(不借助临时变量)
int x = 5, y = 7;
x = x ^ y;
y = x ^ y;
x = x ^ y;
// 现在 x=7, y=5

2.4 运算符优先级(复杂表达式的求值顺序)

// 运算符优先级表(从高到低)
// 1. 括号  ()
// 2. 后缀  ++ --  
// 3. 一元  ++ -- + - ! ~
// 4. 乘除  * / %
// 5. 加减  + -
// 6. 移位  << >> >>>
// 7. 关系  < <= > >= instanceof
// 8. 相等  == !=
// 9. 逻辑与 &
// 10. 逻辑异或 ^
// 11. 逻辑或 |
// 12. 条件与 &&
// 13. 条件或 ||
// 14. 三元   ?:
// 15. 赋值  = += -= *= ...

// 易错示例
int result = 5 + 3 * 2;     // 11,不是16
int result2 = (5 + 3) * 2;  // 16

boolean flag = true || false && false;  // true
// 原因:&&优先级高于||,等价于 true || (false && false)

// 赋值运算符从右向左结合
int a, b, c;
a = b = c = 10;  // 等价于 a = (b = (c = 10))

// 建议:复杂表达式使用括号增加可读性
int result3 = ((a + b) * (c - d)) / (e + f);

第三章 控制流程 —— 程序的决策与循环

3.1 条件分支的精髓
if-else 家族的完整形态

// Java条件语句完整示例
int score = 85;

if (score >= 90) {
    System.out.println("优秀");
} else if (score >= 80) {
    System.out.println("良好");
} else if (score >= 70) {
    System.out.println("中等");
} else if (score >= 60) {
    System.out.println("及格");
} else {
    System.out.println("不及格");
}

// 嵌套if
boolean hasTicket = true;
boolean isVip = false;

if (hasTicket) {
    if (isVip) {
        System.out.println("VIP通道");
    } else {
        System.out.println("普通通道");
    }
} else {
    System.out.println("请先购票");
}

// if-else 的代码块注意事项
int x = 10;
if (x > 5)
    System.out.println("大于5");
    System.out.println("这行永远执行");  // 缩进误导!实际不属于if

// 正确写法:始终使用大括号
if (x > 5) {
    System.out.println("大于5");
    System.out.println("属于if块");
}

switch 语句的进化

// Java传统switch(支持int, char, String, enum)
int day = 3;
String dayName;

switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    default:
        dayName = "Unknown";
}

// 穿透现象(如果没有break)
switch (day) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        System.out.println("工作日");
        break;  // 所有1-5都执行这里
    case 6:
    case 7:
        System.out.println("周末");
        break;
}

// Java 14+ 增强switch(表达式形式)
String result = switch (day) {
    case 1, 2, 3, 4, 5 -> "工作日";
    case 6, 7 -> "周末";
    default -> "无效";
};

// 带yield的复杂逻辑
String grade = "A";
int points = switch (grade) {
    case "A" -> {
        System.out.println("优秀");
        yield 95;  // 产生返回值
    }
    case "B" -> {
        System.out.println("良好");
        yield 85;
    }
    default -> 0;
};

3.2 循环结构的深度剖析
while 和 do-while

# Python while循环
# 基本形式
count = 0
while count < 5:
    print(f"Count: {count}")
    count += 1

# 无限循环与break
while True:
    user_input = input("输入exit退出: ")
    if user_input == "exit":
        break
    print(f"你输入了: {user_input}")

# continue跳过本次循环
num = 0
while num < 10:
    num += 1
    if num % 2 == 0:
        continue  # 跳过偶数
    print(num)   # 打印奇数

# while-else(Python特色)
search = [1, 3, 5, 7, 9]
target = 6
i = 0
while i < len(search):
    if search[i] == target:
        print("找到了!")
        break
    i += 1
else:
    print("没找到")  # 循环正常结束(没有break)时执行
// Java do-while循环(至少执行一次)
int attempts = 0;
int password = 1234;
int input;

do {
    System.out.print("请输入密码: ");
    input = scanner.nextInt();
    attempts++;
    if (attempts >= 3) {
        System.out.println("超过尝试次数");
        break;
    }
} while (input != password);

for 循环的多种形态
C语言风格for循环:

// 基本形式
for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

// 多个变量
for (int i = 0, j = 10; i < j; i++, j--) {
    System.out.println("i=" + i + ", j=" + j);
}

// 省略部分表达式
int i = 0;
for (; i < 10; ) {
    System.out.println(i);
    i++;
}

// 死循环
for (;;) {
    // 等价于 while(true)
}

增强for循环(foreach):

// 遍历数组
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
    System.out.println(num);
}

// 遍历集合
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
for (String name : names) {
    System.out.println(name);
}

Python的for循环(迭代器风格):

# 遍历范围
for i in range(5):      # 0,1,2,3,4
    print(i)

for i in range(2, 8):   # 2,3,4,5,6,7
    print(i)

for i in range(1, 10, 2):  # 1,3,5,7,9(步长2)
    print(i)

# 反向遍历
for i in range(10, 0, -1):  # 10,9,8,...,1
    print(i)

# 遍历列表
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
    print(fruit)

# 同时获取索引和值
for index, fruit in enumerate(fruits):
    print(f"{index}: {fruit}")

# 遍历字典
person = {"name": "Alice", "age": 25, "city": "New York"}
for key in person:
    print(f"{key}: {person[key]}")

for key, value in person.items():
    print(f"{key}: {value}")

# 并行遍历多个序列
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
for name, score in zip(names, scores):
    print(f"{name} 得分: {score}")

# 列表推导式(简洁的循环)
squares = [x**2 for x in range(10)]  # [0,1,4,9,16,25,36,49,64,81]
evens = [x for x in range(20) if x % 2 == 0]
matrix = [[i*j for j in range(5)] for i in range(5)]  # 二维列表

3.3 循环控制的高级技巧

// 带标签的break和continue(Java)
outer: for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == 1 && j == 1) {
            break outer;  // 跳出外层循环
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}
// 输出:i=0,j=0 i=0,j=1 i=0,j=2 i=1,j=0
# Python的for-else和while-else
# 查找质数的例子
for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print(f"{n} = {x} * {n//x}")
            break
    else:
        print(f"{n} 是质数")

来源:
http://zlpow.cn/

相关文章
|
5月前
|
算法 IDE 搜索推荐
软件开发新手入门五大核心技能之基础编程能力(五)
教程来源 http://fndvx.cn/ 本章涵盖算法思维与调试能力:详解冒泡、快排、归并等经典排序及线性、二分查找算法;介绍打印调试、断点调试、断言验证等实用技巧;剖析空指针、索引越界等常见错误及解决方案;最后通过学生成绩管理系统实战,综合运用面向对象编程与数据处理能力。
|
5月前
|
人工智能 运维 监控
OpenClaw是什么能干什么 新版OpenClaw完整入门教程 核心功能与阿里云部署实操指南
在AI智能体全面普及的2026年,各类自动化代理工具层出不穷,OpenClaw凭借开源免费、私有化部署、多平台适配、强大任务执行能力迅速走红,成为个人办公、团队协作、开发运维、消息自动交互的热门选择。很多新手初次接触OpenClaw时,都不清楚它到底是什么、具备哪些核心能力、适合什么场景,也不知道如何在阿里云服务器上快速完成部署配置。
563 3
|
机器学习/深度学习 存储
卡方分箱、KS分箱、最优IV分箱、树结构分箱、自定义分箱
卡方分箱、KS分箱、最优IV分箱、树结构分箱、自定义分箱
4110 0
卡方分箱、KS分箱、最优IV分箱、树结构分箱、自定义分箱
|
API Android开发
截图授权和投影媒体权限
牙叔教程 简单易懂
1267 0
|
10天前
|
人工智能 运维 安全
如何从零到一高效推进企业级大模型私有化部署?
2026年,大模型私有化部署已成为企业数智化转型标配。本文详解从零落地的四大关键:精准选型与算力规划、高效推理框架搭建、RAG+智能体业务集成、安全管控与持续运维,助力企业筑牢数据安全底线,打造专属AI生产力底座。(239字)
199 0
|
2月前
|
人工智能 自然语言处理 监控
适合电商的智能客服产品推荐:从功能匹配到价格对比全解析
本文深度解析阿里云瓴羊Quick Service如何赋能电商客服升级:依托AI Agent实现退换货等闭环办事,全渠道协同统一工作台,多源知识库自动更新,并提供模块化定价。适配大促高并发,助力企业从“问答”迈向“办事”新阶段。
|
编解码 前端开发 开发者
探索无界:前端开发中的响应式设计深度实践与思考###
本文将带你领略响应式设计的精髓,一种超越传统页面布局限制的设计策略,它要求开发者以灵活多变的思维,打造能够无缝适应各种设备与屏幕尺寸的Web体验。通过深入浅出的讲解、实际案例分析以及技术实现细节的探讨,本文目的是激发读者对于响应式设计深层次的理解与兴趣,鼓励在实际应用中不断创新与优化。 ###
573 10
|
JSON 文字识别 测试技术
超顶的视觉理解模型怎么用?Qwen2.5-VL Cookbook教会你!
超顶的视觉理解模型怎么用?Qwen2.5-VL Cookbook教会你!
|
缓存 前端开发 JavaScript
常见的三种Web应用程序:SWA, PWA 和Jamstack
常见的三种Web应用程序:SWA, PWA 和Jamstack
527 0
数字信号处理实验二 IIR数字滤波器设计及软件实现
数字信号处理实验二 IIR数字滤波器设计及软件实现
数字信号处理实验二 IIR数字滤波器设计及软件实现