C++ course (third section 1)
Assignment Statement
•In C++, = is called the assignment operator
•For example, if miles is a double variable
cin >> miles;
−Causes computer to get a value of type double Places it in the variable miles
1)
x = 5;
y = ++x;
2)
x = 5;
y = x++;
The first one you increment x by 1 (which become 6) then keep it and print it if you want,
•The stream insertion operator is <<
•Expression evaluated and its value is printed at the current cursor position on the screen
−May appear anywhere in the string
cout << "Hello there.";
cout << "My name is James.";
•Output:
Hello there.My name is James.
cout << "Hello there.\n";
cout << "My name is James.";
•Output :
Hello there.
My name is James.
C++ course
third section(write your first C++ program 1)
First you must know some main important things about the program:
•The assignment statement takes the form:
variable=expression
•Expression is evaluated and its value is assigned to the variable on the left side •In C++, = is called the assignment operator
i.e int num1,num2;
double sale;
char first;
string str;
num1=4;
num2=4*3-7;
sale=0.02;
first='A';
str="have a nice day";
OR
int num1=3;
int num2=6;
double sale=2.5;
.
.
.
.
Input (Read) Statement
•cin is used with >> to gather input
cin>>variable>>variable;
•The stream extraction operator is >> •For example, if miles is a double variable
cin >> miles;
−Causes computer to get a value of type double Places it in the variable miles
•Using more than one variable in cin allows more than one value to be read at a time
•For example, if feet and inches are variables of type int, a statement such as:
cin >> feet >> inches;
−Inputs two integers from the keyboard
−Places them in variables feet and inches respectively
Increment & Decrement Operators•For example, if feet and inches are variables of type int, a statement such as:
cin >> feet >> inches;
−Inputs two integers from the keyboard
−Places them in variables feet and inches respectively
•Increment operator: increment variable by 1
−Pre-increment: ++variable
−Post-increment: variable++
•Decrement operator: decrement variable by 1
−Pre-decrement: --variable
−Post-decrement: variable—
•What is the difference between the following?
−Pre-increment: ++variable
−Post-increment: variable++
•Decrement operator: decrement variable by 1
−Pre-decrement: --variable
−Post-decrement: variable—
•What is the difference between the following?
1)
x = 5;
y = ++x;
2)
x = 5;
y = x++;
The first one you increment x by 1 (which become 6) then keep it and print it if you want,
The second one you keep the value of x which is 5 and print it if you want then you increment it by one.
Output(write) statement
•The syntax of cout and << is:
cout<<expression or manipulator<<expression;
−Called an output statement •The stream insertion operator is <<
•Expression evaluated and its value is printed at the current cursor position on the screen
•A manipulator is used to format the output
−Example: endl causes insertion point to move to beginning of next line
−Example: endl causes insertion point to move to beginning of next line
i.e cout<<x<<endl;
•The new line character is '\n' −May appear anywhere in the string
cout << "Hello there.";
cout << "My name is James.";
•Output:
Hello there.My name is James.
cout << "My name is James.";
•Output :
Hello there.
My name is James.
0 comments:
Post a Comment