Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming - 1

Similar presentations


Presentation on theme: "Introduction to Programming - 1"— Presentation transcript:

1 Introduction to Programming - 1
Dr. Khizar Hayat Associate Prof. of Computer Science

2 C++ Program structure - Example
Start //This is the first program /* to explain the C++ program structure This is an example*/ #include<iostream> using namespace std; int main() { cout<<“Hello World!\n”; return(0); } Display “Hello World!” Stop Output Hello World!

3 Comments Single line Multiple lines //This is the first program
/* to explain the C++ program structure This is an example*/

4 Preprocessor directive
A preprocessor directive always begin with #, e.g. #include<iostream> Preprocessor directives are commands to tell the preprocessor to: Skip part of a file, e.g. #ifdef include another file, e.g. Define a constant or macro, e.g. #define Pi 3.14 Should be placed in the beginning for readability. Lines beginning with # are processed by the preprocessor before the program is compiled The #include <xyz> directive tells the preprocessor to grab the text of a header file (with .h extension and named xyz) and place it directly into the current file.

5 namespace The line using namespace std;
All the identifiers (variables, functions, classes and objects) in the ANSI standard header files are part of the std namespace In ANSI C++, cin and cout are written as std::cin and std::cout. To avoid using std:: one must write using namespace std; By using this line you also avoid the use of .h: #include<iostream> with using namespace std; instead of #include<iostream.h> without using namespace std;

6 The function main() C++ programs begin executing from main(), even if it is not written at the top of all functions in the program main() is part of every C++ program, i.e. exactly one function in a program must be main even if it is empty. Can “return a value”, e.g. int main() return an integer Body is delimited by braces ({})

7 Statements A statement instructs the program to perform an action.
Each statement must end with a semicolon ‘;’ Example cout<<“Hello World!\n”; How many statements in the lines below? cout<<“Hi\n”;cout<<“How’s life\n?”;

8 Escape sequences/characters
A character preceded by ‘\’, indicating special character output e.g. \n would output a new line \t would output a horizontal tab (space of 7 characters) \\ would output a \ (backslash) \a would output a beep \” would output a double quote “

9 Stream insertion operator
<< is the stream insertion operator e.g. cout<<“Hello”; Inserts the string “Hello” into the standard output, i.e. displays on the screen Note that the string is always enclosed in double quotes (“”) For a single character single quotes (‘’) are used

10 Syntax Errors A syntax error occurs when the compiler encounters code that violates C++ language rules, i.e. syntax. The compiler normally issues an error message to help the programmer locate and fix incorrect codes You will be unable to execute your program unless you locate and correct all the syntax errors. A Common Programming error is omitting the semicolon at the end of a C++ statement is a syntax error


Download ppt "Introduction to Programming - 1"

Similar presentations


Ads by Google