0% found this document useful (0 votes)
22 views

Programming Lecture 3

Here is a C++ program that displays the multiplication table of 5: #include <iostream> using namespace std; int main() { for(int i=1; i<=10; i++) { cout << "5 * " << i << " = " << 5*i << endl; } return 0; }

Uploaded by

fadydesoky45
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Programming Lecture 3

Here is a C++ program that displays the multiplication table of 5: #include <iostream> using namespace std; int main() { for(int i=1; i<=10; i++) { cout << "5 * " << i << " = " << 5*i << endl; } return 0; }

Uploaded by

fadydesoky45
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

1

Programming

Lecture 3

INTRODUCTION TO C++
Dr. Badria Nabil
Programming
2
and Problem Solving
 Algorithm
A sequence of precise instructions which
leads to a solution

 Program
 An algorithm expressed in a language the computer
can understand
C++ History
3

 C developed by Dennis Ritchie at AT&T


Bell Labs in the 1970s.
 Usedto maintain UNIX systems
 Many commercial applications written in c

 C++ developed by Bjarne Stroustrup at AT&T


Bell Labs in the 1980s.
 Overcame several shortcomings of C
 Incorporated object oriented programming

 C remains a subset of C++


C++ Standard Library
 C++ programs
 Built from pieces called classes and functions
 C++ Standard Library
 Rich collections of existing classes and functions
◼ Reusable in new applications

4
Typical C++ Development Environment
 C++ programs normally undergo six phases
 Edit
◼ Programmer writes program (and stores source code on disk)
 Preprocess
◼ Perform certain manipulations before compilation (#)( like calling for libraries
of functions)
 Compile
◼ Compiler translates C++ programs into machine languages
 Link
◼ Link object code with missing functions and data
 Load
◼ Transfer executable image to memory
 Execute
◼ Execute the program one instruction at a time 5
6
C++ program Development Environment
7
Garbage In Garbage Out (GIGO)
8
1.4

Testing and Debugging


 Bug
A mistake in a program
 Debugging
 Eliminating mistakes in programs

9
Types of errors in programming
10

 Syntax errors: grammatical errors in computer


language. The program can not be executed.
 Run time errors: occurring during the execution of
the program.
 Logical errors: errors in the logic of the program.
The program can be executed but the results will be
illogical (wrong).
 The compilers and interpreters are stopping
translation because of which kind of the previous
errors ???? The answer is
 Syntax error
A Sample C++ Program
11
C++ Language Elements
 Comments
 Compiler directives
 Function main
 Declaration statements
 Executable statements
Comments
 Every program should begin with a comment that
describes the purpose of the program, author, date
and time.
 Explain programs to other programmers
 Improve program readability
 Ignored by compiler

 Single-line comment
◼ Begin with //
◼ Example
◼ // This is a text-printing program.
 Multi-line comment
◼ Start with /*
◼ End with */
#include <filename>
 Compiler directive
 Processed by preprocessor before compiling
 Begin with #

 Includes previously written code from a library


into your program
 E.g.
#include <iostream>
◼ Tellspreprocessor to include the input/output stream
header file <iostream>
◼ has operators for performing input and output within the
program
using namespace std;
 Indicates to compiler that this program uses objects
defined by a standard namespace called std.
 Ends with a semicolon
 Follows #include directives in the code
 Must appear in all programs
Function main
int main ( )
{
// function body
}
 Statements

 Instructthe program to perform an action


 All statements end with a semicolon (;)
Function main
 A part of every C++ program
 Exactly one main function per program
 A function is a collection of related statements that
perform a specific operation
 int indicates the return type of the function
◼ int main()
◼ This main function returns an integer (whole number)
 ( ) indicates no special information passed to the
function by the operating system
 Body is delimited by braces ({})
Return Statement

 return statement
 Oneof several means to exit a function
 When used at the end of main
◼ Thevalue 0 indicates the program terminated successfully
◼ Example
◼ return 0;

18
Output
19

 Standard output stream object


 std::cout

 “Connected” to screen
 Defined in input/output stream header file
<iostream>
 The stream insertion operator is <<

 The syntax of cout and << is:

 Called an output statement


 Expression evaluated and its value is printed at the
current cursor position on the screen
Output (continued)
20

 A manipulator is used to format the output


 Example: endl causes insertion point to move to
beginning of next line
Output (continued)
21

 The new line character is '\n'


 May appear anywhere in the string
cout << "Hello there.";
cout << "My name is James.";
◼ Output:
Hello there.My name is James.
cout << "Hello there.\n";
cout << "My name is James.";
◼ Output :
Hello there.
My name is James.
Output (continued)
22
Program Layout
 Compiler accepts almost any pattern of line
breaks and indentation
 Programmers format programs so they
are easy to read
 Place opening brace ‘{‘ and closing brace ‘}’
on a line by themselves
 Indent statements

 Use only one statement per line

 White space
 Blank lines, space characters and tabs
 Used to make programs easier to read

 Ignored by the compiler Slide 23


Common Programming Error
 Forgetting to include the <iostream> header file in
a program that inputs data from the keyboard or
outputs data to the screen causes the compiler to
issue an error message, because the compiler cannot
recognize references to the stream components
(e.g., cout).
 Omitting the semicolon at the end of a C++
statement is a syntax error.
 Preprocessor directives do not end in a semicolon.

24
Reserved Words (Keywords)
 Have special meaning in C++
 Cannot be used for other purposes
The Hello world program
When learning a new language, the first program people
usually write is one that display the message of “ Hello World”

// Here is the Hello world program in C++.


#include <iostream>
using namespace std;
int main() {
cout << “Hello world!”;

return 0;
}
Assignment 2
27

Write a C++ program that computes and display the


multiplication table of "5" as the following:
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

You might also like