{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 02

# The code reads in a number and then writes the result

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 7. #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } The first line of the body #include <iostream> declares two containers to hold using namespace std; numbers. The identifiers of these int main ( ) containers will be x and y. { It is important to declare every double x, y; cin >> x; identifier before it is used. y = x * 7; cout << y; return 0; Reading A C++ Program: Declaration } Simple declarations are of the #include <iostream> form: using namespace std; simple_type identifiers; int main ( ) { In this case the identifiers x double x, y; and y are declared to be of the cin >> x; type double. y = x * 7; For now we will consider two cout << y; simple types: double and int. return 0; } Reading A C++ Program: Declaration Reading A C++ Program: Declaration #include <iostream> A double is a number with a using namespace std; fractional part that can take on a very large range of magnitudes. An int is an integer number between about 2 billion to 2 billion. Always choose the one closest to your meaning. int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Expressions The next 3 lines of the program are expressions. Expressions change and exchange data. The basic form of an expression is: expression; #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Expressions The first expression, cin >> x; will obtain input from the caller. int main ( ) The second expression, y { double x, y; = x * 7; multiples x by 7 cin >> x; and assigns the result to y. y = x * 7; The third expression, cout cout << y; << y; will output y to the return 0; caller. } #include <iostream> using namespace std; Reading A C++ Program: Semicolons int main ( ) { Denote the end of a double x, y; declaration or expression. cin >> x; Critical for determining y = x * 7; order of execution. cout << y; return 0; The most common errors are } omitted semicolons. ; #include <iostream> using namespace std; Next Lecture C++ Basics fun!...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online