We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 91
CHAPTER-2
BASIC CONCEPTS OF C++
PROGRAMMING A SAMPLE C++ PROGRAM OUTPUT // my first program in C++ #include <iostream.h> Hello World! int main () { cout << "Hello World!"; return 0; }
This program includes the basic components that
every C++ program has Chapter 2: Basic Concepts of C++ Programming 2 COMMENTS
Chapter 2: Basic Concepts of C++ Programming 3
COMMENTS Comments are pieces of source code discarded from the code by the compiler. They are used to insert notes or descriptions. C++ supports two ways to insert comments: // line comment /* block comment */ The Line comment, discards everything from where the double slash signs (//) is found up to the end of that same line. The Block comment, discards everything between the /* characters and the next appearance of the */ characters, with the possibility of including several lines. Chapter 2: Basic Concepts of C++ Programming 4 Example of Comments /* my second program in C++ with more comments */ #include <iostream.h> int main () { cout << "Hello World! "; // shows Hello World! cout << “A C++ program";// shows A C++ program return 0; }
Chapter 2: Basic Concepts of C++ Programming 5
DATA TYPES
Chapter 2: Basic Concepts of C++ Programming 6
DATA TYPES There are many different types of data. In the realm of numeric information, for example, there are whole numbers and fractional numbers. There are negative numbers and positive numbers. And there are numbers so large, and others so small, that they don’t even have a name. Then there is textual information. Names and addresses, for instance, are stored as groups of characters. Chapter 2: Basic Concepts of C++ Programming 7 Data Types C++ offers two very broadest data types: numeric and character. Numeric data types are broken into two additional categories: integer and floating-point. Integers are whole numbers like 12, 157, –34, and 2. Floating-point numbers have a decimal point, like 23.7, 189.0231, and 0.987. Additionally, the integer and floating point data types are broken into even more classifications. Chapter 2: Basic Concepts of C++ Programming 8 Integer Data Types Includes short unsigned short Int unsigned int Long unsigned long
data types
Chapter 2: Basic Concepts of C++ Programming 9
Floating-Point Data Types Are data type that allows fractional values. If you are writing a program that works with dollar amounts or precise measurements you need floating point data types In C++ there are three data types that can represent floating-point numbers. They are float double long double Chapter 2: Basic Concepts of C++ Programming 10 Floating-Point Data Types Internally, floating-point numbers are stored in a manner similar to scientific notation. In scientific notation the number 47,281.97 is 4.728197x104. Computers typically use E notation to represent floating- point values. In E notation, the number 47,281.97 would be 4.728197E4. The part of the number before the E is the mantissa, and the part after the E is the power of 10. The following table shows other numbers represented in scientific and E notation.
Chapter 2: Basic Concepts of C++ Programming 11
Char Data Type A char variable (used to hold characters) is most often one byte long.
Chapter 2: Basic Concepts of C++ Programming 12
Size of Data Types The size of a variable is the number of bytes of memory it uses. Our computer's memory is organized in bytes. A byte is the minimum amount of memory that we can manage. A byte can store an integer between 0 and 255 or one single character. But in addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or numbers with decimals.
Chapter 2: Basic Concepts of C++ Programming 13
List of the existing fundamental data types in C++ Name Bytes* Description Range*
signed: -128 to 127
char 1 character or integer 8 bits length. unsigned: 0 to 255
signed: -32768 to 32767
short 2 integer 16 bits length. unsigned: 0 to 65535 signed:-2147483648 to long 4 integer 32 bits length. 2147483647 unsigned: 0 to 4294967295 Integer. Its length traditionally int * depends on the length of the See short, long system's Word type, 2 (4) bytes. float 4 floating point number. 3.4e + / - 38 (7 digits) double precision floating point double 8 1.7e + / - 308 (15 digits) number. long double precision floating point long double 10 1.2e + / - 4932 (19 digits) number. Chapter 2: Basic Concepts of C++ Programming 14 VARIABLES
Chapter 2: Basic Concepts of C++ Programming 15
Variables You can use variables to store values and you can perform any mathematical operation on those variables This can be expressed in C++ with the following instruction set: a = 5; b = 2; a = a + 1; result = a - b;
Chapter 2: Basic Concepts of C++ Programming 16
Declaration of variables In order to use a variable in C++, we must first declare it The syntax to declare a new variable is: data type specifier (like int, short, float...) followed by a valid variable identifier. For example: int a; float mynumber; // Are valid declarations of variables.
Chapter 2: Basic Concepts of C++ Programming 17
….Declaration of variables You can declare all of the variables of the same data type in the same line separating the identifiers with commas. For example: int a, b, c; declares three variables (a, b and c) of type int , and has exactly the same meaning as if we had written: int a; int b; int c;
Chapter 2: Basic Concepts of C++ Programming 18
Examples on operating with variables Example-1 // operating with variables Output int main () 4 { int a, b, result; // declaring variables // process: a = 5; b = 2; a = a + 1; result = a - b; cout << result; // print out the result return 0; // terminate the program } Chapter 2: Basic Concepts of C++ Programming 19 Example-2 // a C++ program with a variable int main() { int number; number = 5; cout << "The value of number is " << "number" << endl; cout << "The value of number is " << number << endl; number = 7; cout << "Now the value of number is " << number << endl; return 0; } Output The value of number is number The value of number is 5 Now the value of number is 7 Chapter 2: Basic Concepts of C++ Programming 20 Initialization of variables To make a variable to store a concrete value the moment that it is declared, append an equal sign followed by the value. type identifier = initial_value ; For example: int number = 5; Here is the other way: int number; number = 5; // this line is an assignment. The = sign is an operator that copies the value on its right (5) into the variable named on its left (number).
Chapter 2: Basic Concepts of C++ Programming 21
CONSTANTS
Chapter 2: Basic Concepts of C++ Programming 22
CONSTANTS A constant is any expression that has a fixed value. You must initialize a constant when you create it, and you cannot assign a new value later. C++ has two types of constants: literal and symbolic.
Chapter 2: Basic Concepts of C++ Programming 23
1. Literal Constants A literal constant is a value typed directly into your program. For example int myAge = 39; myAge is a variable of type int; 39 is a literal constant. You can't assign a value to 39, and its value can't be changed. They can be divided in Integer Numbers, Floating-Point Numbers, Characters and Strings.
Chapter 2: Basic Concepts of C++ Programming 24
Integer and Floating Point Constants Values 1776, 707, -273 are numerical constants that identify integer numbers. Floating Point constants express numbers with decimals and/or exponents. They can include a decimal point: 3.14159 // 3.14159 6.02e23 // 6.02 x 1023 1.6e-19 // 1.6 x 10-19 3.0 // 3.0
Chapter 2: Basic Concepts of C++ Programming 25
Characters and strings There also exist non-numerical constants, like: 'z', // single character 'p', // single character "Hello world", // strings of several characters "How do you do?". // strings of several characters Notice that: to represent a single character we enclose it between single quotes (') and to express a string of more than one character we enclose them between double quotes ("). Notice this: x and 'x'. Here, x refers to variable x, whereas 'x' refers to the character constant 'x'. Chapter 2: Basic Concepts of C++ Programming 26 2. Symbolic Constants A symbolic constant is a constant that is represented by a name. There are two ways to declare a symbolic constant in C++. Defining Constants with #define E.g. #define studentsPerClass 15
Defining Constants with const
E.g. const unsigned short int studentsPerClass = 15;
Chapter 2: Basic Concepts of C++ Programming 27
…Symbolic Constants For example: If your program has two integer variables named students and classes, you could compute how many students you have, given number of classes, if you know there were 15 students per class: students = classes * 15; In this example, 15 is a literal constant. If you substituted a symbolic constant for this value: students = classes * studentsPerClass Chapter 2: Basic Concepts of C++ Programming 28 IDENTIFIERS
Chapter 2: Basic Concepts of C++ Programming 29
IDENTIFIERS An identifier is a programmer-defined name that represents some element of a program. Variable names are examples of identifiers. A valid identifier is a sequence of one or more letters, digits or underscore( _ ). Neither spaces nor special characters can be part of an identifier. Variable identifiers should always begin with a letter and underscore( _ ). They cannot begin with a digit. They cannot match any key word of the C++ language nor your compiler's specific ones. Chapter 2: Basic Concepts of C++ Programming 30 Legal Identifiers Here are some specific rules that must be followed with all identifiers. The first character must be one of the letters a through z, A through Z, or an underscore (_). After the first character you may use the letters a through z or A through Z, the digits 0 through 9, or underscores. Uppercase and lowercase characters are distinct. For e.g. ItemsOrdered is not the same as itemsordered.
Chapter 2: Basic Concepts of C++ Programming 31
….Legal Identifiers The following table lists variable names and indicates whether each is legal or illegal in C++.
Chapter 2: Basic Concepts of C++ Programming 32
Keywords Some words are reserved by C++, and you may not use them as variable names. Keywords used by the compiler to control your program. Keywords include if, while, for, and main. The key words make up the “core” of the language and have specific purposes. They are words that have a special meaning. They may only be used for their intended purpose. They are also known as reserved words. Chapter 2: Basic Concepts of C++ Programming 33 Key words according to the ANSI- C++ standard
Chapter 2: Basic Concepts of C++ Programming 34
SPECIAL CHARACTERS AND ESCAPE SEQUENCES
Chapter 2: Basic Concepts of C++ Programming 35
SPECIAL CHARACTERS There are several sets of special characters. The following table provides a short summary of how they were used.
Chapter 2: Basic Concepts of C++ Programming 36
Escape Sequences Escape sequences are written as a backslash character (\) followed by one or more control characters They are used to control the way output is displayed. They give you the ability to exercise greater control over the way information is output by your program. There are many escape sequences in C++. The newline escape sequence (\n) is just one of them. When cout encounters \n in a string, interprets it as a special command to advance the output cursor to the next line. Chapter 2: Basic Concepts of C++ Programming 37 ….Escape Sequences
Do not confuse the backslash (\) with the forward slash (/). Do not put a space between the backslash and the control character.
Chapter 2: Basic Concepts of C++ Programming 38
BASIC INPUT/OUTPUT
Chapter 2: Basic Concepts of C++ Programming 39
BASIC INPUT/OUTPUT In the iostream C++ library, standard input and output operations are supported by two data streams: cin for input and cout for output. Therefore: cout (the standard output stream) is normally directed to the Monitor and cin (the standard input stream) is normally assigned to the Keyboard. By handling these two streams you can show messages on the screen and receive input from the keyboard. Chapter 2: Basic Concepts of C++ Programming 40 Output (cout) The cout stream is used in conjunction with the overloaded operator << (a pair of "less than" signs). cout << "Output sentence"; // prints Output sentence on screen cout << 120; // prints number 120 on screen cout << x; // prints the content of variable x on screen When the << symbol is used this way, it is called the stream-insertion operator, since it inserts the data that follows it into the stream that precedes it. The information immediately to the right of the operator is sent to cout and then displayed on the screen. In the examples above it inserted the constant string Output sentence, the numerical constant 120 and the variable x into the output stream cout.
Chapter 2: Basic Concepts of C++ Programming 41
…. Output (cout) To use constant strings of characters we must enclose them between double quotes (") so that they can be clearly distinguished from variables. The output message is enclosed between double quotes (") because it is a string of characters. For example, these two sentences are very different: cout << "Hello"; // prints Hello on screen cout << Hello; // prints the content of Hello variable on screen The insertion operator (<<) may be used more than once in a same sentence: cout << "Hello, " << "I am " << "a C++ sentence"; this would print the message Hello, I am a C++ sentence on the screen. Chapter 2: Basic Concepts of C++ Programming 42 Input (cin) Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. This must be followed by the variable that will store the data that is going to be read. For example: int age; cin >> age; cin can only process the input from the keyboard once the ENTER key has been pressed. Chapter 2: Basic Concepts of C++ Programming 43 …. Input (cin) You can also use cin to request more than one datum input from the user: cin >> a >> b; is equivalent to: cin >> a; cin >> b; In both cases the user must give two data, one for variable a and another for variable b that may be separated by any valid blank separator : a space, a tab character or a newline. Chapter 2: Basic Concepts of C++ Programming 44 Example on I/O // i/o example #include <iostream.h> int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << “\nThe value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; } OUTPUT Please enter an integer value: 702 The value you entered is 702 and its double is 1404. Chapter 2: Basic Concepts of C++ Programming 45 OPERATORS
Chapter 2: Basic Concepts of C++ Programming 46
Operators An operand is usually a piece of data, like a number. There are three types of operators: unary, binary, and ternary. Unary operators only require a single operand. For example, consider the following expression: -5 The minus sign, when used this way, is called the negation operator. Binary operators work with two operands. The assignment operator is in this category. E.g. a=1 Ternary operators require three operands. C++ only has one ternary operator (? – conditional operator). E.g. a>b ? a : b
Chapter 2: Basic Concepts of C++ Programming 47
Operators C++ provides operators to operate on variables and constants Operators are a set of keywords and signs.. Includes Assignment operator(=) Arithmetic operators ( +, -, *, /, % ) Compound assignment operators (+=, -=, *=, /=, %=) Relational operators ( ==, !=, >, <, >=, <= ) Logic operators ( !, &&, || ) Conditional operator ( ? ) The Increment and Decrement Operators(++, --) Chapter 2: Basic Concepts of C++ Programming 48 Assignment operator(=) It serves to assign a value to a variable. a = 5; assigns the integer value 5 to variable a. lvalue must always be a variable whereas rvalue can be either a constant, a variable, the result of an operation or any combination of them. For e.g. a = b; assigns to variable a (lvalue) the value that contains variable b (rvalue) independently of the value that was stored in a at that moment.
Chapter 2: Basic Concepts of C++ Programming 49
…. Assignment operator(=) For example, if we take this code : int a, b; // a:? b:? a = 10; // a:10 b:? b = 4; // a:10 b:4 a = b; // a:4 b:4 b = 7; // a:4 b:7 will give us the result that the value contained in a is 4 and the one contained in b is 7. The final modification of b has not affected a, (right-to-left rule).
Chapter 2: Basic Concepts of C++ Programming 50
Arithmetic operators ( +, -, *, /, %) The following table shows the common arithmetic operators in C++.
Chapter 2: Basic Concepts of C++ Programming 51
…. Arithmetic operators ( +, -, *, /, %) The addition operator returns the sum of its two operands. Here, the variable amount will be assigned the value 12: amount = 4 + 8; The subtraction operator returns the value of its right operand subtracted from its left operand. This statement will assign the value 98 to temperature: temperature = 112 - 14; The multiplication operator returns the product of its two operands. In the following statement, markUp is assigned the value 3: markUp = 12 * 0.25;
Chapter 2: Basic Concepts of C++ Programming 52
…. Arithmetic operators ( +, -, *, /, %) The division operator returns the quotient of its left operand divided by its right operand. In the next statement, points is assigned the value 5: points = 100 / 20; The modulus operator, which only works with integer operands, returns the remainder of an integer division. The following statement assigns 2 to leftOver: leftOver = 17 % 3;
Chapter 2: Basic Concepts of C++ Programming 53
Compound assignment operators (+=, -=, *=, /=, %=) The following table shows the combined assignment operators, also known as compound operators or arithmetic assignment operators.
Chapter 2: Basic Concepts of C++ Programming 54
….. Compound assignment operators (+=, -=, *=, /=, %=) Programs may have assignment statements of the following form: number = number + 1; On the right-hand side of the assignment operator, 1 is added to number. The result is then assigned to number, replacing the value that was previously stored there. Effectively, this statement adds 1 to number. In a similar fashion, the following statement subtracts 5 from number. number = number – 5; Chapter 2: Basic Concepts of C++ Programming 55 ….. Compound assignment operators (+=, -=, *=, /=, %=) Examples of statements: (Assume x = 6)
Chapter 2: Basic Concepts of C++ Programming 56
Relational operators ( ==, !=, >, <, >=, <= ) They allow you to compare numeric values and determine if one is greater than, less than, equal to, or not equal to another. For example, the greater-than operator (>) determines if a value is greater than another. The equality operator (==) determines if two values are equal.
Chapter 2: Basic Concepts of C++ Programming 57
…. Relational operators ( ==, !=, >, <, >=, <= ) The following table lists all of C++’s relational operators.
Chapter 2: Basic Concepts of C++ Programming 58
…. Relational operators ( ==, !=, >, <, >=, <= ) All the relational operators are binary operators They use two operands. Here is an example of an expression using the greater-than operator: x>y This expression is called a relational expression. It is used to determine if x is greater than y. The following expression determines if x is less than y: x<y Chapter 2: Basic Concepts of C++ Programming 59 …. Relational operators ( ==, !=, >, <, >=, <= ) Relational expressions are Boolean expressions which means their value can only be true or false. If x is greater than y, the expression x>y will be true, while the expression y==x will be false. The == operator determines if the operand on its left is equal to the operand on its right. If both operands have the same value, the expression is true. Assuming that a is 4, the following expression is true: a == 4 But the following is false: a == 2 Chapter 2: Basic Concepts of C++ Programming 60 …. Relational operators ( ==, !=, >, <, >=, <= ) The >= operator determines if the operand on its left is greater than or equal to the operand on the right. Assuming that a is 4, b is 6, and c is 4, both of the following expressions are true: b >= a a >= c But the following is false: a >= 5 The <= operator determines if the operand on its left is less than or equal to the operand on its right. Assuming that a is 4, b is 6, and c is 4, both of the following expressions are true: a <= c b <= 10 But the following is false: b <= a Chapter 2: Basic Concepts of C++ Programming 61 …. Relational operators ( ==, !=, >, <, >=, <= ) The != operator is the not-equal operator. It determines if the operand on its left is not equal to the operand on its right . Is the opposite of the == operator. As before, assuming a is 4, b is 6, and c is 4, both of the following expressions are true because a is not equal to b and b is not equal to c: a != b b != c But the following expression is false because a is equal to c: a != c Chapter 2: Basic Concepts of C++ Programming 62 …. Relational operators ( ==, !=, >, <, >=, <= ) Examples of relational expressions and their true or false values. (Assume x is 10 and y is 7.)
Chapter 2: Basic Concepts of C++ Programming 63
Logic operators (&&, ||, ! ) They connect two or more relational expressions into one or reverse the logic of an expression. The following table lists C++’s logical operators.
Chapter 2: Basic Concepts of C++ Programming 64
… Logic operators (&&, ||, ! ) The && operator is known as the logical AND operator. It takes two expressions as operands and creates an expression that is true only when both sub-expressions are true.
Chapter 2: Basic Concepts of C++ Programming 65
…. Logic operators (&&, ||, ! ) The || operator is known as the logical OR operator. It takes two expressions as operands and creates an expression that is true when either of the sub- expressions are true.
Chapter 2: Basic Concepts of C++ Programming 66
…. Logic operators (&&, ||, ! ) The ! operator performs a logical NOT operation. It takes an operand and reverses its truth or falsehood. In other words, if the expression is true, the ! operator returns false, and if the expression is false, it returns true.
returns false because the expression at its right (5 == 5) would
!(5 == 5) be true. !(6 <= 4) returns true because (6 <= 4) would be false. !true returns false. !false returns true. •For example: ( (5 == 5) && (3 > 6) ) returns false ( true && false ). ( (5 == 5) || (3 > 6))Chapter returns true ( true || false ). 2: Basic Concepts of C++ Programming 68 Conditional operator ( ?: ) This evaluates an expression and returns a different value according to the evaluated expression, depending on whether it is true or false. Its format is:
condition ? result1 : result2
if condition is true the expression will return result1, if not it will return result2.
Chapter 2: Basic Concepts of C++ Programming 69
…. Conditional operator ( ? ) •Example: 7==5 ? 4 : 3 returns 3 since 7 is not equal to 5. 7==5+2 ? 4 : 3 returns 4 since 7 is equal to 5+2. 5>3 ? a : b returns a, since 5 is greater than 3. a>b ? a : b returns the greater one, a or b.
Chapter 2: Basic Concepts of C++ Programming 70
The Increment and Decrement Operators (++ and -- ) They are designed just for incrementing and decrementing variables. The increment operator is ++ . The decrement operator is - -. They are unary operators E.g. The following statement uses the ++ operator to increment num: num++; And the following statement decrements num: num--; The expression num++ is pronounced “num plus plus,” and num--is pronounced “num minus minus.” Chapter 3: Basic Concepts of C++ Programming 71 …. The Increment and Decrement Operators (++ and -- ) ++ and -- are operators that add and subtract 1 from their operands. To increment a value means to increase it by one, and to decrement a value means to decrease it by one. Both of the following statements increment the variable num: num = num + 1; num += 1; And num is decremented in both of the following statements: num = num - 1; num -= 1;
Chapter 2: Basic Concepts of C++ Programming 72
…. The Increment and Decrement Operators (++ and -- ) Increment and decrement operators are used in: postfix mode, where means the operator is placed after the variable. E.g. num ++; num --; prefix mode, where the operator is placed before the variable name. E.g. ++num; --num;
Chapter 2: Basic Concepts of C++ Programming 73
OPERATOR PRECEDENCE
Chapter 2: Basic Concepts of C++ Programming 74
Operator Precedence Deals about which operand is evaluated first and which later. For example, in this expression: a=5+7%2 we may doubt if it really means: a = 5 + (7 % 2) with result 6, or a = (5 + 7) % 2 with result 0 The correct answer is the first of the two expressions, with a result of 6. Chapter 2: Basic Concepts of C++ Programming 75 … Operator Precedence There is an established order for all the operators which can appear in C++. Priority Operator Description Associativity 1 ( ) [ ] -> . sizeof Left ++ -- increment/decrement ! unary NOT 2 &* Reference and Dereference (pointers) Right (type) Type casting +- Unary less sign 3 */% arithmetical operations Left 4 +- arithmetical operations Left 5 < <= > >= Relational operators Left 6 == != Relational operators Left 7 && || Logic operators Left 8 ?: Conditional Right 9 = += -= *= /= %= Chapter Assignment 2: Basic Concepts of C++ Programming Right 76 … Operator Precedence The following table shows some expressions with their values.
Chapter 2: Basic Concepts of C++ Programming 77
… Operator Precedence Grouping with Parentheses can force some operations to be performed before others. All these precedence levels can be manipulated using parenthesis signs ( and ), as in this example: a = 5 + 7 % 2; might be written as: a = 5 + (7 % 2); or a = (5 + 7) % 2; For example: consider the following statement average = (a + b + c + d) / 4; Here the sum of a, b, c, and d is divided by 4. Without the parentheses, however, d would be divided by 4 and the result added to a, b, and c. Chapter 2: Basic Concepts of C++ Programming 78 … Operator Precedence The following table shows more expressions and their values with high- precedence operations enclosed between parentheses.
Chapter 2: Basic Concepts of C++ Programming 79
EXPRESSIONS
Chapter 2: Basic Concepts of C++ Programming 80
EXPRESSIONS In algebra it is not always necessary to use an operator for multiplication. C++ requires an operator for any mathematical operation. The following table shows some algebraic expressions that perform multiplication and the equivalent C++ expressions.
Chapter 2: Basic Concepts of C++ Programming 81
… EXPRESSIONS When converting some algebraic expressions to C++, you may have to insert parentheses in the algebraic expression. For example, look at the following expression:
To convert this to a C++ statement, a + b
will have to be enclosed in parentheses: x = (a + b) / c; Chapter 2: Basic Concepts of C++ Programming 82 … EXPRESSIONS The following table shows more algebraic expressions and their C++ equivalents.
Chapter 2: Basic Concepts of C++ Programming 83
PROGRAMMING ERRORS
Chapter 2: Basic Concepts of C++ Programming 84
PROGRAMMING ERRORS Debugging is the process of finding and correcting errors in computer programs. Most programs you write will contain errors. Either they won't compile or they won't execute properly. Program debugging is another form of problem solving There are three types of programming errors: Design errors Syntax errors Run-time errors
Chapter 2: Basic Concepts of C++ Programming 85
Design Errors They occur during the analysis, design, and implementation phases. They occur due to: Choosing an incorrect method of solution for the problem to be solved, Making mistakes in translating an algorithm into a program, or Designing erroneous data for the program. Design errors are usually difficult to detect. Debugging them requires careful review of problem analysis, algorithm design, translation, and test data. Chapter 2: Basic Concepts of C++ Programming 86 Syntax Errors Syntax Errors are violations of syntax rules, which define how the elements of a programming language must be written. They occur during the implementation phase They are detected by the compiler during the compilation process. Another name for syntax errors is compilation errors. If your program contains syntax errors, the compiler issues diagnostic messages. Depending on how serious the violation is, the diagnostic message may be a warning message or an error message. Chapter 2: Basic Concepts of C++ Programming 87 … Syntax Errors A warning message indicates a minor error that may lead to a problem during program execution. These errors do not cause the termination of the compilation process and may or may not be important. It is good practice to take warning message seriously and eliminate their causes in the program. If the syntax violation is serious, the compiler produces error messages, telling you about the nature of the errors and where they are in the program. Because of the explicit help we get from compilers, debugging syntax errors is relatively easy.
Chapter 2: Basic Concepts of C++ Programming 88
Run-time Errors Run-time Errors are detected by the computer while your program is being executed. They are caused by program instructions that require the computer to do something illegal, such as attempting to store inappropriate data or dividing a number by zero. When a run-time error is encountered, the computer produces an error message and terminates the program execution. You can use these error messages to debug run-time errors Chapter 2: Basic Concepts of C++ Programming 89 Review Question Write C++ expressions for the following algebraic expressions