- Learning C# 7 By Developing Games with Unity 2017(Third Edition)
- Micael DaGra?a Greg Lukosek
- 311字
- 2021-07-02 19:59:39
Making decisions in code
The fundamental mechanism of programming is making decisions. In everyday life, we make hundreds and possibly thousands of decisions a day. They might be the results of simple questions such as "Do I need an umbrella today?" or "Should I drive at the maximum highway speed at the moment?" Let's first take a question and draw a single graph, as follows:

This is a fairly easy question. If it will be raining, I will need an umbrella; otherwise, I won't. In programming, we call it an if statement. It's a way we describe to the computer what code should be executed under what conditions. The question "Will it be raining?" is the condition. When planning your code, you should always break decision–making down into simple questions that can be answered only by a "yes" or a "no."
In C# syntax, we use true or false instead of yes or no.
We now know how the simplest if statements work. Let's see how this question will look in code. Let's create a new script, name it LearningStatements, and add it to a GameObject in the scene:

Look at the code on line 10 and its description:
if (willItBeRainingToday)
An if statement is used to test whether the condition between the parentheses is true or false. The willItBeRainingToday variable stores a value true. Therefore, the code block in line 11 will be executed. Go ahead and hit Play in the Editor tab. The Console will print outline 11.
Line 12 contains the else keyword. Everything within the brackets after the else keyword is executed only if the previous conditions aren't met. To test how it works, we press Stop in the editor, and on the GameObject containing our LearningStatements script, we change our variable value by ticking the checkbox in the Inspector panel. Then press Play again.
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- Vue 3移動Web開發與性能調優實戰
- Python編程自學手冊
- C#編程入門指南(上下冊)
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- Designing Hyper-V Solutions
- HTML5游戲開發案例教程
- 匯編語言程序設計(第3版)
- Windows內核編程
- Extending Puppet(Second Edition)
- Nginx Lua開發實戰
- 區塊鏈技術與應用
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- SQL Server實用教程(SQL Server 2008版)
- Developing SSRS Reports for Dynamics AX