天天看點

c++初學者指南_2020年面向初學者的終極C ++指南。 入門 (Getting Started) 句法 (Syntax) 輸出文字 (Output text) 注釋 (Comments) 變數 (Variables) 數組 (Arrays) 經營者 (Operators) 數學 (Math) 條件語句 (Conditional Statements) 循環 (Loops) 結論 (Conclusion)

c++初學者指南

C++ is a high-level programming language developed by Bjarne Stroustrup. C++ is one of the most popular programming languages for graphical applications, such as those that run in Windows and Macintosh environments.

C ++是由Bjarne Stroustrup開發的進階程式設計語言。 C ++是圖形應用程式(例如在Windows和Macintosh環境中運作的圖形應用程式)最受歡迎的程式設計語言之一。

入門 (Getting Started)

For developing in C++, you will need two things:

對于用C ++開發,您将需要兩件事:

  • A text editor, like Notepad, to write C++ code

    文本編輯器(如記事本)可編寫C ++代碼

  • A compiler, like GCC, to translate the C++ code into a language that the computer will understand

    像GCC這樣的編譯器,用于将C ++代碼翻譯成計算機可以了解的語言

The best way to achieve an editor and compiler in one is Codeblocks. If you have installed Codeblocks, we can start writing code.

一個實作編輯器和編譯器的最佳方法是Codeblocks 。 如果您已安裝Codeblock,我們可以開始編寫代碼。

Let’s create our first C++ file

讓我們建立第一個C ++檔案

my_program.cpp:

my_program.cpp:

#include <iostream>
using namespace std;int main() {
  cout << "Hello Medium!";
  return 0;
}
           

If you run this code by clicking build > Build and Run, it will look like this:

如果通過單擊生成>生成并運作來運作此代碼,則它将如下所示:

Hello World!
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.
           

句法 (Syntax)

  1. ‘#include <iostream>’ is a header file library that lets us work with input and output objects.

    “ #include <iostream>”是一個頭檔案庫,可讓我們使用輸入和輸出對象。

  2. ‘using namespace std’ means that we can use names for objects and variables from the standard library.

    “使用命名空間标準”意味着我們可以使用标準庫中對象和變量的名稱。

  3. Blank space, C++ ignores white space.

    空格,C ++忽略空格。

  4. ‘int main() {‘ is called a function. Any code inside its curly brackets will be executed.

    'int main(){'被稱為函數。 大括号内的所有代碼都将執行。

  5. ‘cout << “Hello Medium”’ is an object used together with the insertion operator to print text.

    'cout <<“ Hello Medium”是與插入運算符一起用于列印文本的對象。

  6. ‘return 0’ ends the main function.

    “傳回0”結束主要功能。

  7. ‘}’ Always close with a curly bracket as well.

    '}'始終也用大括号括起來。

輸出文字 (Output text)

The ‘cout’ object is used to print or display text.

'cout'對象用于列印或顯示文本。

cout << "Hello Medium!";
           

To add a new line to your console add:

要将新行添加到控制台,請添加:

cout << "Hello Medium! \n";
           

注釋 (Comments)

Comments can be used to explain C++ code, and to make it more readable. To add a comment in C++, we do this:

注釋可用于解釋C ++代碼,并使其更具可讀性。 要在C ++中添加注釋,我們可以這樣做:

// This is a comment
cout << "Hello Medium!";
           

變數 (Variables)

As we have in all other programming languages, we have variables. In C++, there are different types of variables:

就像所有其他程式設計語言一樣,我們也有變量。 在C ++中,有不同類型的變量:

  • INT (stores integers)

    INT(存儲整數)

  • DOUBLE (stores floating-point numbers)

    DOUBLE(存儲浮點數)

  • CHAR (stores single characters)

    CHAR(存儲單個字元)

  • STRING (stores text)

    STRING(存儲文字)

  • BOOL (stores values with two states: true or false)

    BOOL(存儲具有兩種狀态的值:true或false)

Declaring a variable

聲明一個變量

To create a variable, you must specify the type and assign it a value:

要建立變量,必須指定類型并為其配置設定值:

type variable = value; //Pseudocode
           

To store a number in a variable:

要将數字存儲在變量中:

int num = 16;
           

This is called initializing a variable.

這稱為初始化變量。

數組 (Arrays)

Arrays are used to store multiple values in a single variable, instead of declaring separate variables. To create an array of 3 integers, you could do:

數組用于将多個值存儲在單個變量中,而不是聲明單獨的變量。 要建立一個由3個整數組成的數組,您可以執行以下操作:

int nums[3] = {10, 50, 130};
           

Displaying the elements

顯示元素

int nums[3] = {10, 50, 130};
cout << nums[0];
// Outputs 10
           

Change an Element

更改元素

nums[2] = 1000;
cout << nums[2];
// Outputs 1000
           

經營者 (Operators)

  • + (Addition)

    +(加法)

  • - (Subtraction)

    -(減法)

  • * (Multiplication)

    *(乘法)

  • / (Division)

    /(部門)

  • % (Modulus)

    %(模量)

數學 (Math)

C++ has many functions that allow you to perform mathematical tasks on numbers.

C ++具有許多功能,可讓您對數字執行數學任務。

cout << max(500, 120);
           

Output:

輸出:

500
           

條件語句 (Conditional Statements)

C++ has the following conditional statements:

C ++具有以下條件語句:

  • If

    如果

  • Else

    其他

  • If Else

    如果别的

  • Switch

    開關

If Statement

如果聲明

To execute a block of code if a specific condition is true.

如果特定條件為真,則執行代碼塊。

if (condition) {
  // block of code to be executed if the condition is true
}
           

Else Statement

其他聲明

To execute a block of code if a specific condition is false.

如果特定條件為假,則執行代碼塊。

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}
           

If Else Statement

否則聲明

To specify a new condition if the first condition is false.

如果第一個條件為假,則指定一個新條件。

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}
           

Switch

開關

The switch statement is used to select one of many code blocks to be executed.

switch語句用于選擇要執行的許多代碼塊之一。

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
           

循環 (Loops)

Loops can execute a block of code as long as a specified condition is reached.

隻要達到指定條件,循環就可以執行代碼塊。

While Loop

While循環

The while loop loops through as long as a specific condition is true.

隻要滿足特定條件,while循環就會循環通過。

while (condition) {  // code block to be executed
}
           

For Loop

對于循環

When you know exactly how many times you want to loop through a block of code, you can use the for loop.

當您确切知道要周遊一段代碼的次數時,可以使用for循環。

for (statement 1; statement 2; statement 3) {
  // code block to be executed
}
           

結論 (Conclusion)

C++ is a very fast programming language to work with, very easy to learn as well, and I hope that you have learned the fundamentals of C++ from this guide. With this knowledge, you should be able to explore the world of C++ yourself.

C ++是一種非常快速的程式設計語言,也非常易于學習,我希望您從本指南中學到了C ++的基礎知識。 有了這些知識,您就應該能夠自己探索C ++的世界。

翻譯自: https://levelup.gitconnected.com/the-ultimate-c-guide-for-beginners-in-2020-a4331948ff43

c++初學者指南