Review Part I
Identifiers
Identifiers are used as names for variables and other items in a C++
program. An identifier must start with either a letter or the underscore
symbol, and the remaining characters must all be letters, digits, or the
underscore symbol.
Variable Declarations
All variables must be declared before they are used. The syntax for variable
declarations is as follows:
SYNTAX Type_Name Variable_Name_1, Variable_Name_2, . . .;
EXAMPLES:
int count, number_of_dragons, number_of_trolls;
double distance;
Syntax
The syntax for a programming language (or any other kind of language) is
the set of grammar rules for that language. For example, when we talk
about the syntax for a variable declaration (as in the box labeled “Variable
Declarations”), we are talking about the rules for writing down a well-formed
variable declaration. If you follow all the syntax rules for C++, then
the compiler will accept your program. Of course, this only guarantees that
what you write is legal. It guarantees that your program will do something,
but it does not guarantee that your program will do what you want it to do.
Assignment Statements
In an assignment statement, first the expression on the right-hand side of
the equal sign is evaluated, and then the variable on the left-hand side of
the equal sign is set equal to this value.
SYNTAX Variable = Expression;
EXAMPLES distance = rate * time;
count = count + 2;
Initializing Variables in Declarations
You can initialize a variable (that is, give it a value) at the time that you
declare the variable.
SYNTAX
Type_Name Variable_Name_1 = Expression_ for_Value_1,
Variable_Name_2 = Expresssion_ for_Value_2, . . .;
EXAMPLES
int count = 0, limit = 10, fudge_factor = 2;
double distance = 999.99;
Alternative Syntax for Initializing in Declarations
Type_Name Variable_Name_1 (Expression_ for_Value_1),
Variable_Name_2 (Expression_ for_Value_2), . . .;
Navid Plus is a Tech blog focusing on Science material that I learn day through my College and job. Most likely, blog posts contain C++ Programming, Security, Networking, and Math. Let's have some fun.
Showing posts with label Initialization. Show all posts
Showing posts with label Initialization. Show all posts
Wednesday, March 22, 2017
Sunday, March 12, 2017
C++ Part 16
Review to C++ Basics
• C++ is case sensitive. For example, count and COUNT are two different identifiers.
• Use meaningful names for variables.
• Variables must be declared before they are used. Other than following this rule, a
variable declaration may appear anyplace.
• Be sure that variables are initialized before the program attempts to use their value.
This can be done when the variable is declared or with an assignment statement before
the variable is first used.
• You can assign a value of an integer type, like int, to a variable of a floating-point
type, like double, but not vice versa.
• Almost all number constants in a program should be given meaningful names that
can be used in place of the numbers. This can be done by using the modifier constin a variable declaration.
• Use enough parentheses in arithmetic expressions to make the order of operations clear.
• The object cout is used for console output.
• A \n in a quoted string or an endl sent to console output starts a new line of output.
• The object cerr is used for error messages. In a typical environment, cerr behaves
the same as cout.• The object cin is used for console input.
• In order to use cin, cout, or cerr, you should place the following directives near
the beginning of the file with your program:#include <iostream>using namespace std;
• There are two forms of comments in C++: Everything following // on the same line
is a comment, and anything enclosed in/* and */ is a comment.
• Do not over comment.
• Use meaningful names for variables.
• Variables must be declared before they are used. Other than following this rule, a
variable declaration may appear anyplace.
• Be sure that variables are initialized before the program attempts to use their value.
This can be done when the variable is declared or with an assignment statement before
the variable is first used.
• You can assign a value of an integer type, like int, to a variable of a floating-point
type, like double, but not vice versa.
• Almost all number constants in a program should be given meaningful names that
can be used in place of the numbers. This can be done by using the modifier constin a variable declaration.
• Use enough parentheses in arithmetic expressions to make the order of operations clear.
• The object cout is used for console output.
• A \n in a quoted string or an endl sent to console output starts a new line of output.
• The object cerr is used for error messages. In a typical environment, cerr behaves
the same as cout.• The object cin is used for console input.
• In order to use cin, cout, or cerr, you should place the following directives near
the beginning of the file with your program:#include <iostream>using namespace std;
• There are two forms of comments in C++: Everything following // on the same line
is a comment, and anything enclosed in/* and */ is a comment.
• Do not over comment.
__________________
Source: Absolute C++ by Walter Savitch , Kenrick Mock
Subscribe to:
Posts (Atom)
Digital Design Part 3
4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two diļ¬erent implementations of the same instru...
-
An array is used to process a collection of data all of which is of the same type, such as a list of numbers or a list of names. An array...
-
One way to think about execution time is that it equals the number of instructions executed multiplied by the average time per instruc...