C + + Tutorial Chapter-hello World

Source: Internet
Author: User

Reference book: C + + Primer 5th

Code: Https://github.com/alivebao/StudyofCPlusPlus

1. Hello World

The first program to learn to write code-hello World

Code:

int _tmain (int argc, _tchar* argv[]) {     std::cout<< "Hello World" <<std::endl;     Return0;}

Pic:


A C + + program consists of one or more functions, one of which must be named Main,main function is the main function, is the entry point of the program. In other words, the execution of any program starts with the main function.

What is a function?

A function can be thought of as a collection of statement blocks that perform some function. The function consists of four parts: The return type, the function name, the input parameter list (enclosed in parentheses), and the function body (enclosed in curly braces). In the Hello World program, the return type is Int,int, which is a C + + built-in type that represents an integer number. The function name is main, the input parameter list is empty, the first sentence in the function body outputs Hello world to the screen, and the second sentence returns that the function is completed and the number 0 is returned.

2. Input and output

Input and output can be performed through CIN and cout in C + +. In Hello world, words are printed on the screen through cout. Let's look at an input and output using instance-add2num.

Code:

void Add2num (int a,int b) {     std::cout<< "the sum of" <<a<< "and" <<b<< "is:" <<a+b <<std::endl;     return;} int _tmain (int argc, _tchar* argv[]) {     inti,j;     std::cout<< "Please input both num:" <<std::endl;     std::cin>>i>>j;     Add2num (i,j);     Return0;}
Pic:


The main function first defines two integer variables I and J, then prints a sentence with cout, and then uses CIN to assign I and J values. Finally, call the Add2num function and print out the sum of two numbers.

What is a variable?

A variable is a piece of memory space used to store data, and the definition of a variable consists of a variable type and a variable name. The program operates on the data through the variable name.

What is a function call?

This program defines a function named Add2num, which, according to the previous section, has a function return type of void, a function named Add2num, and a parameter list of a and b two integer variables. Output of two integer variables in the function body. The function is called in main: Add2num (i,j). The statement assigns I and J to A and B.

What is input and output

As long as you remember that you can enter variables through CIN, you can print the content you want to output to the screen by cout.

3. Control Flow

If,for,while

Code:

int _tmain (int argc, _tchar* argv[]) {     std::cout<< "IF Test:" <<std::endl;     if (1>3)         std::cout<< "1>3" <<std::endl;     else         std::cout<< "1<3" <<std::endl;      std::cout<< "for Test:" <<std::endl;     for (int i = 0; i < 3; ++i) {         std::cout<< "i =" <<i<<std::endl;     }      std::cout<< "while Test:" <<std::endl;     INTJ = 3;     while (j!= 0) {         std::cout<< "j =" <<j<<std::endl;         --j;     }     Return0;}

Pic:


The order of execution of the program executes from top to bottom, but in the control flow, it is selectively executed according to the logical judgment result. The IF, for, and while in this program are logical judgments. In the parentheses of these judgment logic, the condition is judged, and if the condition is true, then the statement in curly braces is executed, and the sentence is skipped. As in If, 1>3 is false, so the content in else is executed directly.

For syntax: For parentheses, there are typically three expressions in the order of execution:

1. Definition: int i = 0

2. Judging: I is less than 3

3. I is less than 3, judging the condition is true, execute the contents of the curly braces

4. The execution of ++i,++i means I increases by 1. At this point I becomes 1.

5. Again determine if I is less than 3

6 .....

7. I is not less than 3 o'clock, the judgment statement i<3 to False, no longer executes the contents in the for curly brace, the For loop ends

While syntax:

1. Judge J!=0, that is, j is not equal to 0 if this logic is true, the statement in parentheses is executed as true

2. The value of j is output in parentheses and the value of J is-j by 1. Again, judging by the judgment in the while parenthesis, it is found that J is not equal to 0 and therefore continues to execute the contents of the curly braces until the end of the 0,while loop.

C + + Tutorial Chapter-hello World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.