[Cpp]
# Include <iostream>
Int main ()
{
Std: cout <"Enter two numbers:" <std: endl;
Int v1, v2;
Std: cin> v1> v2;
Std: cout <"The sum of" <v1 <"and" <
V2 <"is" <v1 + v2 <std: endl;
System ("pause ");
Return 0;
}
Main function. The returned value must be of the int type.
This value can be regarded as a status indicator. If the return value is 0, the execution is successful. If the return value is not 0, a specific error occurs.
IO standard library, iostream library, defines four IO objects: cin, cout, cerr, clog.
Std: endl, with the output line break effect, and will refresh the buffer, so that you can immediately see the output written to the stream.
Std: endl, which contains two colons. This is a scope operator that specifies the namespace to which the service belongs.
While statement:
[Cpp]
# Include <iostream>
Int main ()
{
Int sum = 0, val = 1;
While (val <= 10 ){
Sum + = val;
++ Val;
}
Std: cout <"Sum of 1 to 10 random sive is"
<Sum <std: endl;
System ("pause ");
Return 0;
}
For statement:
[Cpp]
# Include <iostream>
Int main ()
{
Int sum = 0;
For (int val = 1; val <= 10; ++ val)
Sum + = val;
Std: cout <"Sum of 1 to 10 random sive is"
<Sum <std: endl;
System ("pause ");
Return 0;
}
Read the unknown target input:
[Cpp]
# Include <iostream>
Int main ()
{
Int sum = 0, value;
Std: cout <"Please Enter:" <std: endl;
While (std: cin> value ){
Sum + = value;
}
Std: cout <"Sum is"
<Sum <std: endl;
System ("pause ");
Return 0;
}
When a non-integer is read, or the file terminator (Ctrl + Z in windows; Ctrl + D in Unix) is input, the while loop ends.