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

Computer Programming 1 CPP - Week2

The document provides an overview of the structure and syntax of a simple C++ program, including the use of preprocessor directives, the main function, and program statements. It explains the purpose of operators, input/output operations, and variable declarations, along with examples demonstrating these concepts. Additionally, it covers memory concepts related to variable storage and assignment in C++.

Uploaded by

ymtskits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Computer Programming 1 CPP - Week2

The document provides an overview of the structure and syntax of a simple C++ program, including the use of preprocessor directives, the main function, and program statements. It explains the purpose of operators, input/output operations, and variable declarations, along with examples demonstrating these concepts. Additionally, it covers memory concepts related to variable storage and assignment in C++.

Uploaded by

ymtskits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CSC 201

COMPUTER PROGRAMMING I

Week 2 – Program Syntax

Department of Computer Science,


2022/2023
The structure of C++ Program
Consider the structure of simple C++ program
below:

1. #include<headerfile>
2. return – type main( )
3. {
4. executable – statements;
5. }
Explaination

 The first line is a preprocessor directive that tells the


C++ compiler where to find the definition of objects
that are used in executable statements section.
 The identifier headerfile is the name of a file in the
standard C++ library.
 The pound sign # is required whenever an import
is needed from C++ standard libary, followed by
“include” keyword. It is regarded a preprocessor
directive;
 the angle brackets < > are required to indicate that
the word “headerfile” is the name of a standard C+
+ library file.
Explaination
 The second begins with a return type, this indicate the
type of data to be return by the main ( ) function

 The main() function is also required in every C++


program; it tells where the program begins.

 The identifier main is the name of a function, called


main function of the program

 The required parentheses that follow the word “main”


indicate that it is a function.
Explaination

 The last three lines constitute the actual body of the


program.

 A program body is a sequence of program statements


enclose in braces { }.

 Finally, note that every program statement must end


with a semicolon (;).
First C++ Program
 Example 1: this program simply prints “welcome to
C++ class”.

#include<iostream>
Int main ( )
{
//prints “Welcome to C++ class!”
cout<< “Welcome to C++ class! \n”;
}
Explaination

 The first line of this source code is a preprocessor


directive that tells the C++ compiler where to find the
definition of the cout object that is used on the third
line.

 The identifier iostream is the name of a file in the


standard C++ library.

 The expression <iosteam> is called a standard


header.
Explaination

 The second line is also required in every C++


program. It tells where the program begins.

 The keyword int is the name of the data – type in C++.


It stands for “integer”.

 It is used here to indicate the return – type for the


main function, when the program has finished
running, it can return an integer value to the
operating system to signal some resulting status.
The last two statements are enclosed in braces
{ }. In this example there is only one statement:

cout<<“Welcome to C++ class !\n”;

 This command tells the compiler to send the


string “Welcome to C++ class !\n” to the
standard output stream object cout.
 The symbol << represents the C++ output
operator.
 Finally, note that every program statement must
end with a semicolon (;).
 Notice how the program is formatted in two lines
of source code.
 That formatting makes the code easier for humans
to read.
 The C++ compiler ignores such formatting.
 It reads the program the same as if it were written
all on one line, like this:
#include<iostream>
int main ( ) { cout<< “welcome to computer science department!\
n”; }

 The last two characters \n is used to insert a


newline.
 A comment in a program is a string of characters that
the preprocessor removes before the compiler
compiles the programs.

 In C++ any text that follows the double slash


symbol //, up to the end of the line, is a comment.

 You can also use another style of comment like this:

/* prints “Welcome to Computer Science Department”*/


 The fifth line,
return 0;

 Is optional for the main ( ) function in standard C+


+. We include it here only because some
compilers expect it to be included as the last line
of the main function.
 An operator is something that performs an action on
one or more objects.
 example of an operators are: <<, >>, +, ++, etc

 The output operator << performs the action of sending


the value of the expression listed on its right to the
output stream listed on its left.

 cout object is called a “stream” because output sent


to it flows like a stream.

 Ifseveral things are inserted in to the cout stream,


they fall in line, one after the other as they dropped in
to the stream, like leaves falling from a tree in to a
natural stream of water.
THE OUTPUT OPERATOR
 The symbol << is called the output operator in
C++. (It is also called the output operator or the
stream insertion operator.)
 It inserts values in to the output stream that is
named on its left.
 We usually use the cout output stream. This
ordinarily refers to the computer screen so the
statement,
 cout<<43; Would display the number 43 on the
screen.
Example: 2 the program has the same output
as that of example 1.

#include<iostream>
int main()
{
//prints “welcome to computer science department!”
cout<<”Welcome”<< “to”<< “C++”<<”Class!”<<endl;
}
CHARACTERS AND LITERALS

 A character is an elementary symbol used collectively to


form meaningful writing.

 Characters are stored in computer as integers.

 The nextline character ‘\n’ is one of the nonprinting


characters.

 It is a single character formed using the backslash \ and


the letter n.

 there are several other characters form this way, including


the horizontal tab character ‘\t’ and the alert character ‘\a’.
THE INPUT OPERATOR

 In C++, input is almost as simple as output. The


input operator >> (also called the get operator or
the extraction operator) works like the output
operator <<
 Example:
#include<iostream>
int main()
{
//test the input of integer, float and characters
int m, n;
cout<<”enter the two integers:”;
cin>>m>>n;
cout<<”m=”<<m<<” “<<”n=”<<n<<endl;
}
VARIABLES AND DECLARATIONS

 A variable is a symbol that represents a storage


location in the computer’s memory.

 The information that is stored in that location is


called the value of the variable.

 One common way for a variable to obtain a value


is by an assignment. This has the syntax

Variable = expression;
 First, the expression is evaluated and then the
resulting value is assigned to the variable. The
equals sign “=” is the assignment operator in C++.

Example: in this example, the integer 32 is assigned to the


variable x, and the value of the expression x+23 is assigned
to the variable y.
#include<iostream>
int main()
{ //prints “x=32 and y=55”:
int x,y;
x = 32;
cout<<”x =”<<x;
y =x+23
cout<<”y =”<<y<<endl;
 Every variable in a C++ program must be declared
before it is used. The syntax is,

Specifier type name initializer;

 Where specifier is an optional keyword such as


const. type is one of the C++ data types such as int,
name is the name of variable, and initialization clause
such as =32.

 The purpose of a declaration is to introduce a name to


the variable; i.e, to explain to the compiler what the
name means. The type tells the compiler what range
of values the variable may have and what operations
can be perform on the variable.
Memory Concepts
 Variable names such as number1, number2
and sum actually correspond to locations in the
computer's memory.
 Every variable has a name, a type, a size and a
value.
 when the statement

int number1;
cin >> number1;
 is executed, the characters typed by the user
are converted to an integer that is placed into a
memory location to which the name number1
has been assigned by the C++ compiler.

You might also like