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

Grade11 Short Note

Uploaded by

elsabetminale2
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)
39 views

Grade11 Short Note

Uploaded by

elsabetminale2
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/ 48

Introduction to programming

Computer programming –known as programming or coding is the


process of writing, testing, debugging/troubleshooting and maintaining
the source code of computer programs.
. Computer programs also known as source code often written by
professionals known as computer programmers (simply programmers.)
What is programming language?
Programming language is a set of different category of written symbols
that instruct computer hardware to perform specified operations
required by the designer.
What skill do we need to be a programmer?
For some one to be a programmer, in addition to basic skills in computer, needs to have
the following major skills
1. Programming language skill: knowing one or more programming language to talk to
the computer and instruct the machine to perform a task.

2. Problem solving skill: skills on how to solve real world problem and represent the
solution in understandable format

3. Algorithm development: skill coming up with sequence of simple and human


understandable set of instructions showing the step of solving the problem. Those
set of steps should not be dependent on any programming language or machines.
In every programming language there are set of rules
that govern the symbols in a programming language.
These set of rules determine how the programmer
can make the computer hardware to perform a
specific operation. These of sets of rules are called
syntax.
Compilers and interpreters
Any program written in a language others than machine language needs
to be translated to machine language. The set of instructions that do
this task are known as translators.
Compilers: is a computer program that translates a series of statements
written in source code(a collection of statements in a specific
programming language) into a resulting object code(translated
instructions of the statement in a programming language).
A compiler changes or translates the whole source code in to executable
machine code (also called object code)which is output to a file for later
execution.
Interpreters: is a computer program that translates a single high level
statement and executes it and goes to the next high level language line.
Structure of a program
all the fundamental components a c++ program have:
1. #include<iostream.h>
2. int main()
3. {
4. // my first program in c++
5. cout<<“hello world”;
6. }
Line1: #include<iostream.h>
lines beginning with a hash sign (#) are directives read and interpreted by what is
known as the preprocessor .the directive #include<iostream.h>,instructs
#include<iostream.h> the preprocessor to include a section of standard C++ code
known as header iostream that allows to perform standard input output operation.
Line2: int main()
This line initiates the declaration of a function.
. Every C++ program has a main function.
The function named main is special function in all C++
program, it is the function called when the program is run.
Line3: two slash lines indicate that the rest of the line is a
comment inserted by the programmer but which has no
effect on the behavior of the program. Programmer use
short explanation or observations concerning the code or
program.
Line3 and 6:{and}
The open brace({ )at line3 indicates the beginning of main function
definition, and the closing brace (}) at line 6, indicates its end. Every
thing b/n these braces is the function body that defines what
happens when main is called. All functions use braces to indicate
the beginning and ending of their definition.
Note: . all c++ statement ends must end with a semicolon
character(;).
. Any C++ program file should be saved with file named
extension
“.cpp”
The program has been structured in different lines and properly
indented, in order to make it easier to understand for the human
reading it. But c++ does not have strict rules on indentation or on
how to split instructions in different lines. For example, instead of
int main()
{
Cout<<“hello world”;
}
We could have written:
int main(){ Cout<<“hello world”; }
All in a single line, and this would have had exactly the same
meaning as the preceding code
preprocessor directive(those that begin by # are out of this
general rule since they are not statements. They are a line read
and processed by the preprocessor before proper compilation
begins. Preprocessor directive must be specified in their own
line and, because they are not statement, do not have to end
with a semicolon(;).
Comments
C++ support two ways of commenting code:
1. // line comment
2. /* block comment*/
The first of them, known as line comment, discards every
thing from where the pair of slash signs(//)are found up
to the end of that same line. The second one, known as
block comment, discard every thing between the /*
characters and the first appearance of the */
characters,with the possibility of including multiple line.
Basic input/output
. Cout is an object used for printing data to the
screen.
To print a value to the screen, write the word cout,
followed by the insertion operator(<<).
. Cin used for taking input from the keyboard. To take
inputs from the keyboard, write the word cin,
followed by extraction operator(>>)
Variable and data type
Variables
. A variable is a reserved place in memory to store information in.
. A variable will have three components.
. variables are used for holding data values so that they can be used
in various computations in a program
. All variables have three important properties:
Data type: a type which is established when a variable is
defined. (e.g. integer, real, character etc). Data type describes the
property of data and the size of reserved memory.
Cont…………
name: A name which will be used to refer to the value in the
variable. A unique identifier for the reserved memory location.

value: A value which can be changed by assigning a new value


to the variable.
Fundamental data types
Fundamental data types are basic types implemented directly by
the language that represent the basic storage units supported
natively by most systems. They can mainly be classified into:
character types: They can represent a single character, such as ‘ A ‘
or ‘ $ ‘ . The most basic type is char , which is a one-byte character.
Other
types also provided for wider characters.
Numerical integer types: They can store a whole number value
such as
7 or 1024 they exist in a variety of sizes, and can either be signed
or unsigned , depending on whether they support negative values
or not.
Cont………….

Floating-point types: They can represent real values , such


as 3.14 or 0.01 with different levels of precision , depending
on which of three floating-point types is used.
Boolean type:The Boolean type known in C++ as bool, can
only represent one of two states, true or false.
I
Identifiers

. A valid identifier is a sequence of one or more letters,


digits or underlined symbols. The length of an identifier is
not limited. Neither space nor marked letters can be part
of an identifier.
. only letters and underlined charcters are valid.
. variable identifiers should always begin with a letter or
underscore. By any means they should not bigin with a
digit .
. key words should not be used as names for identifiers.
Cont……………………………………………..

. C++ is case sensitive. Small letter and capital letters are different
for C++
E.g: variable Age is not identical with variable age.
. Another rule that you have consider when inventing your own
identifiers is that they can not match any keyword of the C++
language nor your compiler’s specific ones, which are reserved
keywords. The standard reserved keywords are :
constants
A constant is any expression that has a fixed value.
. Like variables, constants are data storage locations in the
computer memory. But, constants, unlike variables their
content cannot be changed after the declaration.
. C++ provide two types of constants: literal and symbolic
constants.
. Literal constants: is a value typed directly in to the program
wherever it is needed.
int num=43;
43 is a literal constant in this statement
Cont--------------------------
. Symbolic constant:- is a constant that is represented by a
name, similar to that of variables. But unlike a variable, its
value can’t be changed after initialization.We have two ways to
declare symbolic constant. These are using #define and the
const keyword.
Example :-
Cont--------------------------------------------------------------------------

1. #include<iostream.h> 2. #include<iostream.h>
int main() { #define x 9.5

Constfloat X=3.5; int main(){


Float y=7.4; float y=7.4;
Float sum; float sum;

sum=X+Y; sum=x+y;
Cout<<sum; cout<<sum;
return 0; return 0;
} }
Cont-------------------------------------------------------------
3. #include<iostream.h>
int main() {
float X=3.5;
float y=7.4;
X=7.8;
float sum;

sum=X+Y;
cout<<sum;
return 0;
}
Operators
. An operator is a symbol that makes the machine to take an action.
.different operators act on one or more operands and can also have
different kinds of operators.
. C++ provide several categories of operands:
 Assignment operator
 Arithmetic operator
 Relational operator
 Increment/decrement operator
 Logical operator
 Conditional operator
 Comma operator
 The sizeof operator
1. Assignment operator(=)
The assignment operator assign a value to a variable .
The assignment operator always takes place from right to left,
and never the other way round.
X=5;
This statement assigns the integer value 5 to the variable x.
x=y;
This statement assigns to variable x the value contained in
variable y. The value of x at the moment this statement is
executed is lost and replaced by the value of y.
Example :-
#include<iostream.h>
int main(){
int a, b;
a=10 ;
b=4;
a=b;
cout<<“a:”<<endl;
cout<<a<<endl;
cout<<“b:”<<endl;
. Assignment operators are expression that can be evaluated. That
means that the assignment itself a value. For example
y= 2+(x=5);
In this expression, y is assigned the result of adding 2 and the value of
another assignment expression (which has itself a value of 5).it is
roughly equivalent to:
x=5;
y=2+x;
With the final result of assigning 7 to y.
The following expression is also valid in c++.
x=y=z=5;
It assigns 5 to the all three variables : x, y and z; always from right to
left.
2. Arithmetic operators(+, -, *, /, %)
The five arithmetic operation supported by C++ are:
Operator description
+ addition
_ subtraction
* multiplication
/ division
% modulo
Operations of addition, subtraction, multiplication and division
corresponds literally to their respective mathematical
operators. the last one, modulo operator, represented by a
percentage sign(%), gives the remainder of a division of two
values. For example
x=11%3;
results in variable x containing the value of 2, since dividing
11 by 3 result in 3, with the remainder of 2.
3.Compund assignment operator(+=, -*, /=, %=, >>=, <<=)
Expression equivalent to
y+=x; y=y+x;
x-=5; x=x-5;
x/=y; x=x/y;
price*=units+1; price=price*( units+1);
Example :
#include<iostream.h>
int main()
{
int a, b=3;
a=b;
a+=2; // equivalents to a=a+2;
cout<<a ;
return 0;
}
4. Increment/decrement operator (++, --)
The increase operator (++) and the decrease
operator(--)increase and decrease by one the value stored in
a variable. They are equivalent to += and -=,respectively.
Example 1. x=3;
y=++x; //x contains 4, y contains 4
Example 2. x=3;
y=x++; // x contains 4, y contains 3
5. Relational operator(==, !=, >, <, >=, <=)
Operator description
== equal to
!= not equal to
> Greater than
< less than
>= greater than or equal to
<= less than or equal to
Example :- suppose that a=2, b=3 and c=6, then:
1. (a==5) //evaluates to false, since a is not equal to 5
2. (a*b>c) // evaluates to true, since (2*3>=6) is true
3. (b+4>a*c) // evaluates false, since (3+4>2*6) is false
4. ((b=2)==a) // evaluates to true
6. Logical operator (!,&&,||)
. The operator ! is the C++ operator for the Boolean operation
NOT.it has only one operands, to its right ,and inverts it, producing
false if its operands is true, and true if its operands is false.
For example
!(5==5) // evaluates to false
!(6<=4) // evaluates to true
!(true) // evaluates to false
!(false) // evaluates to true
. The logical operator && and || are used when evaluating two
expressions to obtain a single relational result. The operator &&
corresponds to the Boolean logical operation AND which yields true if
both it operands are true, and false otherwise.
&& operator (and)
a b a&&b
true true true
true false false
false true false
false false false
The operator || corresponds to the Boolean logical operation
OR, which yields true if either of it operands are true, thus
being false only when both operands are false.
|| operator (or)
a b a||b
true true true
true false true
false true true
false false false
For example:
1. ((5==5)&& (3>6)) //evaluates to false(true && false)
2. ((5==5)||(3>6)) // evaluate to true ( true||false)
7. Comma operator(,)
The comma operator (,) is used to separate two or more
expression that are included where only one expression is
expected. When the set of expression has to be evaluated for a
value .for example
a= (b=3, b+2);
would first assign the value 3 to b, and then assign b+2 to
variable a. so at the end, variable a would contain the value 5
while variable b would contain value 3.
8. sizeof
This operator accepts one parameter, which can be either a
type or a variable, and returns the size in bytes of that type .
x= sizeof (char);
Here , is assigned the value 1, because char is a type with a
size of one byte.

Statements and flow control


A simple c++ statement is each of the individual instruction
of a program, like the variable declaration and expression
seen in previous section.
They always end with a semicolon (;) and are executed in
the same order in which they appear in a program.
But programs are not limited to a linear sequence of
statements. During its process, a program may repeat
segments of codes, or take decisions and bifurcate. For
that purpose, C++ provides flow control statements that
serve to specify what has to be done by our program,
when and our circumstances.
Conditional statement

Control structure Iteration statement(loop)

Breaking control statement

Conditional – are mainly used for decision making. Depends


on the value of an expression decision can be made.
1. If statement- is the simplest of the decision statement.
The syntax of the if statement is as follows.
if(condition)
body of if;
Here, condition is the expression that is being evaluated. If this condition is
true, statement is executed. If it is false, statements is not executed (it is
simply ignored).
For example, the following code fragments prints x is 100 only if the value
stored in the variable is indeed 100:
1. if(x==100)
2. Cout<<“ x is 100)
If we want more than a single statement to excuted in case that the
condition is true we can specify a block using braces{ }:
if(x==100+
{
cout<<“x is “;
cout<<x;
2. If else statement- the condition is evaluated first if that is
true the if body is executed otherwise else body is executed.
The syntax:
if(condition)
body of if;
else
body of else
Activity
Write a program which reads a number and the prints the
messages for that number (that is even od odd).
3. Nestedif-else statement- in if statement of if-else
statement in place of body we can use again if or if-else this
concept is known as nested structure.
Example:
Write a program which reads three numbers and then find
the largest one.
4. Switch statement – the switch or case switch is an
alternate of nested if-else structure. This is a multi way
decision maker that tests whether an expression matches
one of the numbers of values.
Syntax:
Switch(expression){
case value 1:
statement1;
statement 2;
.
.
statement n;
breaks;
case value 2:
statement1;
statement 2;
.
.
statement n;
breaks;
case value n:
statement1;
statement 2;
.
.
statement n;
breaks;
default:
statement1;
statement 2;
.
.
statement n;
}
Iteration statement(loop)
1. For loop
the for is the easiest loop, this consists of three expressions. The
syntax
for(initialization expression; test expression; assignment)
body of if;
If body of loop consists multiple statements that that looks like
for(initialization expression; test expression; assignment){
statement 1;
statement 2;
.
statement n; }
2. While loop
The while loop allows programs to repeat a statement of series of statements, over
and over, as long as a certain test condition is true. The syntax:
while(test condition){
block of code;
}
3. do-while loop
do{
statement 1;
.
.
}
While(condition);
Breaking control statement
1. The break statement- break leaves a loop, even if the condition for its end is
not fulfilled. It can be used to end an infinite loop, or to force it to end before
its natural end.for example, lets stop the countdown before its natural end:
#include<iostream.h>
int main(){
for( int n=10; n>0;n--){
cout<<n<<“ , ”;
if(n==3)
{
cout<<“ countdown aborted”;
break;
}
}
}
2. The continue statement- the continue statement causes the
program to skip the rest of the loop in the current iteration, as if the
end of the statement block had been reached, causing it to jump to the
start of the following iteration.for example, lets skip number 5 in our
countdown:
#include<iostream.h>
int main(){
for(int n=10;n>0; n--){
if(n==5) continue;
cout<<n<<“ , “;
}
cout<<“lifoff!\n”;
}

You might also like