Sei sulla pagina 1di 5

)1 – ‫حال‬201(‫ حاسب آلي‬: ‫المقـرر‬

Lecture # 4
STRUCTURE OF C++ PROGRAM
(Structure of a Program, Sample Programs)

2
)1 – ‫حال‬201(‫ حاسب آلي‬: ‫المقـرر‬

Structure of C++ Program


1. Program definition

A computer program is a sequence of instructions that tell the computer what


to do.

2. Statement

The most common type of instruction in a program is the statement.

Each Statements in C++ are terminated by a semicolon.

There are many different kinds of statements in C++. The following are some
of the most common types of simple statements:

1 int x;
2 x = 5;
3 cout << x;

int x is a declaration statement. It tells the compiler that x is a variable. All


variables in a program must be declared before they are used.

x = 5 is an assignment statement. It assigns a value (5) to a variable (x).


cout << x; is an output statement.

An expression is an mathematical entity that evaluates to a value. For


example, in math, the expression 2+3 evaluates to the value 5.

For example, the statement x = 2 + 3; is a valid assignment statement. The


expression 2+3 evaluates to the value of 5. This value of 5 is then assigned to
x.

1
)1 – ‫حال‬201(‫ حاسب آلي‬: ‫المقـرر‬

3. Functions

In C++, statements are typically grouped into units called functions. A


function is a collection of statements that executes sequentially. Every C++
program must contain a special function called main().

4. Taking a look at a sample program

Now that you have a brief understanding of what statements, functions are,
let’s look at a simple hello world program.

Consider our hello world program:

1) #include<iostream.h>
2) int main()
3) {
4) Cout<<"Hello World";
5) Return 0;
6) }

Line 1 is a special type of statement called a preprocessor directive.

Line 2 declares the main() function, which as you learned above, is


mandatory. Every program must have a main() function.

Lines 3 and 6 tell the compiler which lines are part of the main function.

Line 4 is our output statement

Line 5 is a new type of statement, called a return statement.

5. Comment

A comment is a line (or multiple lines) of text that are inserted into the source
code to explain what the code is doing.

3
)1 – ‫حال‬201(‫ حاسب آلي‬: ‫المقـرر‬

6. Sample programs

Write a program to add two numbers

//Program for addition of two numbers

#include<iostream.h>

int main()

int x=10;

int y=20;

int C=0;

C=x+y;

cout<<"addition of two numbers are"<<C;

Return 0;

Write a program to subtract two numbers

//Program to subtract two numbers

#include<iostream.h>

int main()

int x=10;

int y=20;

int C=0;

C=x-y;

cout<<"Subtraction of two numbers are "<<C;

4
)1 – ‫حال‬201(‫ حاسب آلي‬: ‫المقـرر‬

Return 0;

Excersice:

Write a program to divide two numbers

Write a program to multiply two numbers

Write a program to Subtract two numbers

Write a program to find average of two numbers

//Program to find average of two numbers

#include<iostream.h>

int main()

int a;

int b;

float C;

cin>>a;

cin>>b;

C=(a+b)/2;

cout<<"Average of two numbers are ----------"<<C;

Return 0;

Note: cin is the opposite of cout

whereas cout prints data to the console, cin reads data from the console.

Potrebbero piacerti anche