Engineering Science 1036A/B Lecture Notes - Lecture 3: Entry Point, Semicolon, Standard Streams
Program Structure
Comments
Not programming statements and are ignored by the complier
•
Allows programmers to put some descriptions inside the code
•
Line comment: is preceded by two slashes // on a line
When the compiler sees //, it ignores all text after // on the same line
○
•
Paragraph comment: is enclosed between /* and */ on one or several lines
When it sees /*, it looks for the next */ and ignores any text between /*
and */
○
•
Preprocessor Directive
line begins with ‘#’•
program that modifies the source code by:
Adding other files
#include <iostream>: adds iostream library with supports
standard input and output
§
○
performing various text replacements in the existing code
#define PI 3.141593: defines PI with the constant value
3.141593 and replaces all instances of PI in a program with the
value 3.141593
§
○
•
Executes automatically before the compilation starts
Compilation follows immediately after the preprocessing, so none can
access the modifications made by the preprocessor
○
•
Using Directive
using namespace std;
It tells the complier to use the functions/objects from the standard
namespace std
○
•
Namespaces allow to group entities like variables, objects, functions and
classes, under a name
•
All the files in the C++ standard library declare all of its entities within the std
namespace
•
Graphical representation
Function: main()
Entry point of the program•
Main function header has the forms:
int main ()
○
int main (void)
Void is a keyword
§
○
int main (int argc, char* argv[])
Used for command-line arguments
§
○
•
All three headers can be used interchangeably•
All statements in the main function must be enclosed in a statement-block '{'
and '}'
•
Every statement in the block must end with a semicolon ';'•
C++ Statements
C++ instruction that ends with a semicolon
might take one or more lines
○
•
Different types of statement include:
Declaration statements
○
Assignment statements
○
Compound/Block statements
enclosed between curly braces ‘{ ‘ & ’} ’
§
○
Selection statements
○
•
Statement: return
return 0;
•
Placed at the end of every main function to exit the program•
The value 0 indicates the program has terminated successfully•
Statement is optional for the main() function•
Statement: endl
Stands for end line•
Takes the cursor to a new line•
Program Structure
Monday, September 18, 2017
10:29 AM
Program Structure
Comments
Not programming statements and are ignored by the complier•
Allows programmers to put some descriptions inside the code•
Line comment: is preceded by two slashes // on a line
When the compiler sees //, it ignores all text after // on the same line
○
•
Paragraph comment: is enclosed between /* and */ on one or several lines
When it sees /*, it looks for the next */ and ignores any text between /*
and */
○
•
Preprocessor Directive
line begins with ‘#’•
program that modifies the source code by:
Adding other files
#include <iostream>: adds iostream library with supports
standard input and output
§
○
performing various text replacements in the existing code
#define PI 3.141593: defines PI with the constant value
3.141593 and replaces all instances of PI in a program with the
value 3.141593
§
○
•
Executes automatically before the compilation starts
Compilation follows immediately after the preprocessing, so none can
access the modifications made by the preprocessor
○
•
Using Directive
using namespace std;
It tells the complier to use the functions/objects from the standard
namespace std
○
•
Namespaces allow to group entities like variables, objects, functions and
classes, under a name
•
All the files in the C++ standard library declare all of its entities within the std
namespace
•
Graphical representation
Function: main()
Entry point of the program•
Main function header has the forms:
int main ()
○
int main (void)
Void is a keyword
§
○
int main (int argc, char* argv[])
Used for command-line arguments
§
○
•
All three headers can be used interchangeably•
All statements in the main function must be enclosed in a statement-block '{'
and '}'
•
Every statement in the block must end with a semicolon ';'•
C++ Statements
C++ instruction that ends with a semicolon
might take one or more lines
○
•
Different types of statement include:
Declaration statements
○
Assignment statements
○
Compound/Block statements
enclosed between curly braces ‘{ ‘ & ’} ’
§
○
Selection statements
○
•
Statement: return
return 0;
•
Placed at the end of every main function to exit the program•
The value 0 indicates the program has terminated successfully•
Statement is optional for the main() function•
Statement: endl
Stands for end line•
Takes the cursor to a new line•
Program Structure
Monday, September 18, 2017
10:29 AM
Document Summary
Not programming statements and are ignored by the complier. Allows programmers to put some descriptions inside the code. Line comment: is preceded by two slashes // on a line. When the compiler sees //, it ignores all text after // on the same line. Paragraph comment: is enclosed between /* and */ on one or several lines. When it sees /*, it looks for the next */ and ignores any text between /* and */ Preprocessor directive line begins with #" program that modifies the source code by: #include : adds iostream library with supports standard input and output performing various text replacements in the existing code. #define pi 3. 141593: defines pi with the constant value. 3. 141593 and replaces all instances of pi in a program with the value 3. 141593. Compilation follows immediately after the preprocessing, so none can access the modifications made by the preprocessor. It tells the complier to use the functions/objects from the standard namespace std.