Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programming Fund Lec 02 and 03

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 14

PROGRAMMING FUNDAMENTAL

lECTUTRE 02
~ What is Programming?
1. Computer programming (often shortened to programming) is a process that
leads from an original formulation of a computing problem to executable
computer programs .
2. Programming involves activities:

· Analysis.

· Developing understanding.

· Generating algorithms.

· Verification of requirements of algorithms including their correctness and


resources consumption.

· Implementation of algorithms in a target programming language. (C++ in


our case)

3. Common Programming Elements:

There are certain elements that are common to all programming

languages:-

language Element Description

· Key Words : Word that have special meaning. Key words may only be
use for their intended purpose. Key words are also known as Reserved
words.

· Programmer-Defined Indentifier: Words or name defined by


Programmer. They are symbolic names that refer to variables or
programming routine.

· Operators: Operators perform operations on different operands. An


operand is usually a piece of data, like a number.

· Puntuation: Characters that marks the begninning or ending of a


statement, or separate items in a list.
· Syntax: Rules that must be followed in constructing a program. Syntax
dictstes how key words and operators must be used, and where puntuations
symbols must appears.

~ First Program in C++


using namespace std;

#include <conio.h>

#include<iostream>

main()

cout<<" Welcome to Lgu University"; getch();

→# is HASH and also called SHARP

→#include: This is a pre-processor directive. It is not part of our program; it is an


instruction to the compiler. It tells the C compiler to include the contents of a file
i.e. iostream. The compiler knows that it is a system file, and therefore looks for it
in a special place.

1. Namespace in C++ : -
· Different libraries may have functions or variables with same name.

· A namespace is designed to overcome this difficulty by using additional


information to differentiate similar functions, classes, variables etc. with the
same name available in different libraries.

· Using namespace, you can define the context in which names are defined.

· In essence, a namespace defines a scope.

· A namespace definition begins with the keyword namespace followed by the


namespace name as follows:
namespace namespace_name;

2. The Using Directive : -


· To call the namespace-enabled version of either function or variable, prepend the
namespace name as follows:

name_space::code;

· You can also avoid prepending of namespaces with the using namespace directive which
tells the compiler that the subsequent code is making use of names in the specified
namespace.

· The using directive can also be used to refer to a particular item within a namespace. For
example,

using std::cout;

3. #include directive : -
· Both user and system header files are included using the preprocessing directive ‘#include’.

· It has two variants:

#include <file> - used for system header files. It searches for a file named file in a standard
list of system directories.

#include "file“ - used for header files of your own program. It searches for a file named file
first in the directory containing the current file, then in the quote directories and then the same
directories used for <file>.

4. main() Function : -
· All C++ Must have main() function.

· When the operating system runs a program in C, it passes control of the computer over to
that program.

· The main() function uses its parentheses() to contain any information typed after the
program name at the command prompt. This is useful for more advanced programming.

· The curly braces {} are used for organization. They contain programming instructions that
belong to the function. Those programming instructions are how the function carries out its
task or does its thing.

· Without main() function a C++ program will not execute.

5. cout in C++ : -
· The predefined object cout is an instance of ostream class.

· The cout object is said to be "connected to" the standard output device, which usually is the
display screen.

· The cout is used in conjunction with the stream insertion operator, which is written as <<
which are two less than signs indicates the direction of data

cout<<" Welcome to LGU University";

· Thing between the double quotes (“ ”) is known as character string, will display it on the
screen.

6. Semicolon “;” in C++ : -


· The semicolon is part of the syntax of C++.

· It tells the compiler that you're at the end of a command.

· The semicolon (;) will be used at the end of the every statement other wise compiler will
report an error.

· The error reported as result of missing semicolon(;) is referred as syntax error.

~ Programming Errors :
Three Types of Errors

1. Syntax Error ( Detected By the Compiler )

2. Logical Error ( Produce incorrect result due to wrong logic )

3. Runtime Error ( Causes the program to abort )


~ Variables in C++ :
1. What is Variable?
· A variable is used to store a piece of data for processing.

· It is called variable because you can change the stored value with new value during program
execution.

· A variable is a named storage location, that stores a value of a particular data type.

2. Dissecting a Variable :
NAME VALUE TYPE
number 123 int

sum -456 int

pi 3.1416 double

average -55.68 double

A variable name has a name, stores a value of the declared type.

3. Variable Declaration Rule :


· A variable must start with:

~ Characters

~ Underscore _ (Not recommended)

· A variable must [recommended]

~ Consists of sequence of Upper/lower case Characters, numbers (0-9), _ up to certain


length.

~ have a name that is self-descriptive and closely reflects the meaning

of the variable e.g. age, user_name, email, address,ph_no

~ not be of single character and meaning less unless they are common names like x ,and z
for coordinators.

4. Variable Declaration Rule :


· It is perfectly okay to use long names of says 30 characters to make sure that
the name accurately reflects its meaning!

· Use singular and plural nouns prudently to differentiate between singular


and plural variables. For example, you may use the variable row to refer to a
single row number and the variable rows to refer to many rows.

· White space (blank, tab, new-line) and other special characters (such as +, -,
*, /, @, &, commas, etc.) are not allowed.

· It cannot begin with a digit.

· An identifier cannot be a reserved keyword or a reserved literal (e.g. int,


double, if, else, for, main,while,break).

· Identifiers are case-sensitive. E.g. A rose is NOT a Rose, and is NOT a ROSE.

~ Variable Data Types :


1. Data Types :
· While doing programming in any programming language, you need to use
various variables to store various information.

· Variables are nothing but reserved memory locations to store values, this
means variable reserve some space in memory.

· Information store in variables may be of various data types like character,


wide character, integer, floating point, double floating point, boolean etc.

· Based on the data type of a variable, the operating system allocates memory
and decides what can be stored in the reserved memory.

2. Primitive Data Types :


· Integers: used to store numerical values(signed or unsigned).
· Characters: used to store Characters ‘A’ to ‘z’. Every alphanumeric digit and
or symbol encoded in single quotes in considered a character.

· Floating-point: used store the decimal point numbers.

· Boolean: A special type called bool, which takes a value of either true or
false.

3. Sizeof Operator :
· The sizeof is a keyword, but it is a compile-time operator that determines
the size, in bytes, of a variable or data type.

· The sizeof operator can be used to get the size of classes, structures, unions
and any other user defined data type.

· The syntax of using sizeof is as follows:

sizeof (data type)

· Where data type is the desired data type including classes, structures,
unions and any other user defined data type.

4. Sizeof Operator Example :


#include <iostream>

using namespace std;

main()

cout << "Size of char : " << sizeof(char) << endl;

cout << "Size of int : " << sizeof(int) << endl;

cout << "Size of float : " << sizeof(float) << endl;

cout << "Size of double : " << sizeof(double) << endl;

OUTPUT :
Size of char : 1

Size of int : 4

Size of float : 4

Size of double : 8

~ Primitive Data Types :


1. typedef Declaration :
· You can create a new name for an existing type using typedef. Syntax to define a new type
using typedef is:

typedef type newname;

· The following tells the compiler that feet is another name for int:

typedef int feet;

· Now, the following declaration is perfectly legal and creates an integer variable called
distance:

feet distance;

2. Character Data Type :


· The data type char is the smallest integral data type with range (128 to 127).

· char data type can represent every key on your keyboard.

· Enclosed in single quotation marks. e.g. 'A', 'a', '0', '*', '+', '$', '&', ‘ ‘

· Several character set are in use:

1. American Standard Code for Information Interchange (ASCII),

2. Extended Binary- Coded Decimal Interchange Code (EBCDIC).

· The ASCII character set has 128 values.


· 65 represents 'A', and the value 43 represents '+'.

~ Operators in C++ :
1. What is an operator?
· An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.

· C++ is rich in built-in operators and provides the following types of operators:

1. Arithmetic Operators

2. Relational Operators

3. Logical Operators

4. Bitwise Operators

5. Assignment Operators

6. Misc Operators

2. Arithmetic Operator :
Following are arithmetic operators available in C++ . Assume variable A holds 10 and variable B
holds 20, then:

3. Relational Operator :
· Relational operators are used to compare two values or expressions to evaluate the
relationship.

· Comparing floating point values using any of the relational operators is dangerous. This is
because small rounding errors in the floating point operands may cause an unexpected
result.

· A relational operator comparison will always return one of the two possible values either
true or false.

· For the example on next slide assume variable A holds 10 and variable B holds 20, then:

4. Logical Operator :
· The logical operators, logical AND (&&) and logical OR (||), are used to combine multiple
conditions formed using relational or equality expressions.

· The operator && corresponds to the Boolean logical operation AND, which yields true if
both its operands are true, and false otherwise.

· The operator || corresponds to the Boolean logical operation OR, which yields true if either
of its operands is true, thus being false only when both operands are false.

· The Logical Operator, logical NOT (!) has only one operand, to its right, and inverts it,
producing false if its operand is true, and true if its operand is false.
5. Bitwise Operator :
Bitwise operator works on bits and perform bit-by-bit operation. The

truth tables for &, |, and ^ are as follows:

6. Assignment Operator :
An assignment operator is the operator used to assign a new value to

a variable:
~ Assignment Rules :
· Assigns a literal value (of the RHS) to a variable (of the LHS) evaluates an expression (of
the RHS) and assign the resultant value to a variable (of the LHS).

· The RHS shall be a value; and the LHS shall be a variable (or memory address).

· Some Valid and Invalid Assignments

X+ 3 = y + 4 Wrong

x +4 = Z Wrong

2=x Wrong

Z = x +4 Correct

x=y Correct

x=2 Correct

~ Assignment Example :
~ OPERATOR PRECEDENCE :
What is Precedence
· In mathematics and computer programming, the order of operations (sometimes called
operator precedence) is a rule used to clarify which procedures should be performed first in
a given mathematical expression.

Highest: ( )

Next: *,/,

% Lowest: + , -

You might also like