C + + Getting Started applet practice __ Database

Source: Internet
Author: User

First knowledge of C + +, to introduce a few I used to do exercises to write a few small procedures, are c++primer the above exercises, to share with you.

EG1: Write programs that use the decrement operator to print out integers from 10 to 1 in descending order in the loop.

The code is as follows:

#include <iostream>
#include <cstdlib>

using namespace std;
int main ()
{
	int i=10;
	while (i)
	{
		cout<<i;
		I.
	}
	System ("pause");
	return 0;
}

Run Result: 10 9 8 7 6 5 4 3 2 1

Childe Tips: Because I added a using namespace STD in front of the main function, it was used directly to cout. Before you add, remember to write the std::cout<< "i=" <<endl;. Because the prefix std:: Indicates that cout and Endl are defined in the namespace named Std (namespace) . Namespaces can help us avoid inadvertently conflicting names, and conflicts caused by using the same name in the library. All the names defined by the standard library are in the namespace Std. This is not in the C language. Using a standard library through namespaces has a side effect, and when we use it, we have to explicitly state that we want to use names from namespaces, such as std::cout.using after a namespace declaration can no longer be explicitly described. Both methods can be, but it is recommended that people declare, because this can write a lot of things less oh.

EG2: Write a program that prompts the user to enter two integers and print out all the integers in the range specified by the two integers.

The code is as follows:

#include <iostream>
#include <cstdlib>

using namespace std;
int main ()
{
	int num1=0;
	int num2=0;
	int ret=0;
	cin>>num1>>num2;
	while (num1<=num2)
	{
		cout<<num1<< ';
		num1++;
	}
	System ("pause");
	return 0;
}

Run Result: 3 8

3 4 5 6 7 8

Childe Tip: We use the output operator (<<) to print the information on standard output in the above program. << operator to receive two Operation objects: the Operation object on the left must be a Ostream object, and the right-hand operand is the value to be printed. In this example, we use the output statement two times <<. Because this operator returns the Operation object on the left of it, the result of the first operator becomes the Operation object on the left side of the second operator, so that we can connect the output request.

EG3: Write a program that reads a set of numbers from CIN, outputting its and.

The code is as follows:

#include <iostream>
#include <cstdlib>

using namespace std;
int main ()
{
	int sum=0;
	int val=0;
	while (Cin>>val)
	{
		sum+=val
	}
	cout<< "sum=" <<sum;
	cout<<endl;
	System ("pause");
	return 0;
}

Run Result: 1 2 3 4 5 6 7 8 9 10

Sum=55

Although the above example is very simple, most of us learn C language has mastered, but can play a good practice, so that we are familiar with the C + + operator, operators and input and output stream, the difference between the two.


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.