Chapter 2
Chapter 2
Chapter 2
} Closing Brace.
• The braces are also known as delimiters.
• These statements are collectively called as body of the program.
• Each statement in C++ is terminated by semicolon.
Example .cpp File Extension
Preprocessor Directive
Main Function
C++ instruction
C++ Output - Using cout
• The cout object is used to print a message on the screen.
• The message may consist of strings and values.
• The cout object is used with insertion operator <<.
• Anything written after the insertion operator is displayed on the
screen.
• Example:
cout<<"Hello World!";
cout<<3+5;
C++ Program Development and Execution
• Editing: The process of writing C++ program is known as editing.
• It includes writing , modifying and deleting program statements.
• Saving: The process of storing the program on disk is known as saving.
• A program should be saved on disk to be used repeatedly.
• C++ Programs are savedwith .cpp extension. E.g. hello.cpp
• Compiling: Process of converting source program to object program.
• Compiler do this conversion.
• Linking: Process of linking library files with object program.
• Linker combines the object program with the library files.
• Executing: Process of running an executable file.
• The program is loaded into the memory to execute it.
• The program that places the executable file in the memory is known as loader.
Steps for C++ Program Development and Execution
Debugging
• An error in a computer program is known as bug.
• The process of finding and removing bugs is known as debugging.
• Type of errors
• Syntax Errors
• Logical Errors
• Run-time errors
Syntax Errors
• Syntax : a collection of rules for writing programs in a programming
language.
• All statements in a program are written according to these rules.
• Syntax error is a type of error that occurs when an invalid statement is
written in program.
• The compiler detects syntax errors and display error message to describe
the cause of error.
• A program containing syntax errors cannot be compiled successfully.
• There can be many causes of syntax errors. E.g.
• The statement terminator is missing at the end of statement.
• A misspelled keyword.
• Any of the delimiters is missing.
Logical Errors
• A typical error that occurs due to poor logic of the programmer is
called logical error.
• A statement with logical error may produce unexpected and wrong
results in the program.
• Logical errors are difficult to find because translator cannot detect
these errors.
• These errros can be deected by examing the program thoroughly.
• Different causes of logical errors are as follows:
• Wrong conditions in program such as writing a <5 instead of >5.
• Wrong formula e.g. Aerage = Total * 5 instead of total/5.
Run-Time Errors
• A runtime error in a program is an error that occurs while the
program is running/executing after being successfully compiled.
• The run-time error are detected and displayed by the computer
during execution.
• Caused due to wrong input by the user. E.g. dividing a number by
zero.
• Example
• The user may ask the program to open a file that does not
exist.
• The user may enter wrong type of data.
Example
• #include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
Example
• #include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
New Lines
• To insert a new line, you can use the \n character:
New Lines
• To insert a new line, you can use the \n character:
New Lines
• Two \n characters after each other will create a blank line:
New Lines
• Another way to insert a new line, is with the endl manipulator:
Escape Sequence
• Both \n and endl are used to break lines. However, \n is most used.
• The newline character (\n) is called an escape sequence, and it forces
the cursor to change its position to the beginning of the next line on
the screen. This results in a new line.
• Examples of other valid escape sequences are:
Escape Sequence \t
• Creates a horizontal tab
Escape Sequence \\
• Inserts a backslash character (\)
Escape Sequence \"
• Inserts a double quote character
C++ Comments
• Comments can be used to explain C++ code, and to make it more
readable.
• It can also be used to prevent execution when testing alternative
code.
• Comments can be singled-lined or multi-lined.
Single-line Comments
• Single-line comments start with two forward slashes (//).
• Any text between // and the end of the line is ignored by the compiler
(will not be executed).
• This example uses a single-line comment before a line of code:
C++ Multi-line Comments
• Multi-line comments start with /* and ends with */.
• Any text between /* and */ will be ignored by the compiler: