Sei sulla pagina 1di 57

Programming Languages

 There are many programming languages like C, C++, Pascal,


BASIC, FORTRAN, Java, C#.
 These are all high-level languages.
 The language of computer is machine language ( Binary language).
Program Vs Software
 Software /Application program is a collection of instructions that
enable the user to interact with a computer, its hardware, or
perform tasks.
 Without software, most computers would be useless.
• For example, without your Internet browser software, you could
not surf the Internet and without an operating system, the browser
could not run on your computer.
Program Vs Software
 All applications are programs, but a program is not necessarily an
application. For example, there are always lots of programs running
on the background of an operating system, but since they weren't
developed for the end-user, they are not applications.
Memory Concepts
• Variable names
– Correspond to locations in the computer's memory
– Every variable has a name, a type, a size and a value
• int myVariable = 10; 4 bytes
– Whenever a new value is placed into a variable, it replaces
the previous value - it is destroyed
Type Name Value Size ?
– Reading variables from memory does not change them
• A visual representation
integer1 45

 2000 Prentice Hall, Inc. All rights reserved.


Fundamental data types
 Char
 Bool
 Short
 Int
 Long
 Float
 Double
 Long double
Fundamental data types
1 // Fig. 1.2: fig01_02.cpp

2 // A first program in C++


Outline
Comments
3 #include <iostream>
Written between /* and */ or following a //.
1. Comments
4
Improve program readability and do not cause the
5 int main() computer to perform any action. 2. Load <iostream>
6 { 3. main
preprocessor directive
7 std::cout << "Welcome to C++!\n";
Message to the C++ preprocessor.3.1 Print "Welcome
8
to C++\n"
Lines beginning with # are preprocessor directives.
9 return 0; // indicate that program ended successfully
#include <iostream> tells3.2 theexit
preprocessor
(returnto 0)
10 } include
C++ the contents
programs containofone
theor <iostream>,
filemore which
functions, one of
includes
which be main operations (such as printing to
input/output
must
the screen). Program Output
Welcome to C++! Parenthesis are used to indicate a function
int means
Prints the string of characters that main
contained between the an integer value.
"returns"
quotation marks. More in Chapter 3.
return is a way to exit a function
from a function. A left
The entire line, including brace { begins
std::cout, the the
<< body of every function
and a right to
means thatthe string "Welcome
return 0, in this case, operator, } ends it. and
braceC++!\n"
the semicolon (;), is called a statement.
the program terminated normally.

All statements must end with a semicolon.

 2000 Prentice Hall, Inc. All rights reserved.


Control Structure in C++
 Program that we tried so far are executed in an
orderly manner i.e. the statements are executed one
after another without getting repeated or ignored.
 Certain tasks require execution of some statements
ignoring the rest.
 These can be accomplish by using control
statements.
 Conditional Control statements
 Unconditional control statements
Conditional Control Statements
 If
 If else
 Nested if else
 Else if
 While
 Do while
 For
Unconditional Control Statements
 Break
 Continue
 Go to
 Return
Arithmetic Operators
C++ o p era tio n Arithm etic Alg eb ra ic C++ exp ressio n
o p era to r exp ressio n
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y

Modulus % r mod s r % s

Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, the


expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or % Multiplication Division Evaluated second. If there are several, they re
Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right.
Rational Operators
Sta nd a rd a lgeb ra ic C++ eq ua lity Exa mp le Mea ning of
eq ua lity op era tor or or rela tiona l of C++ C++ c ond ition
rela tiona l op era tor op era tor c ond ition

Relational operators
> > x > y x is greater than y
< < x < y x is less than y

 >= x >= y x is greater than or equal to y

 <= x <= y x is less than or equal to y

Equality operators
= == x == y x is equal to y

 != x != y x is not equal to y
Operators
 Unary Operator
– ++,--
 Binary Operator

 Ternary Operator
– conditional operator (?:)
Condition Control Statements
 These statements are executed when one or more
conditions is/are satisfied.
 C++ supports many such statements.
 IF
– The statement following the if statement is executed when
the condition given is true.
– if(condition)
– s1;
– s2;
– When the condition is true statement s1 and then
s2 is executed. If the condition is false only s2 is
executed.
Condition Control Statements
Condition Control Statements
Condition Control Statements
– If we want to execute more than 1 statements when
condition is true then we should write all those statements
within braces {} after if.
– if(condition)
– {
– s1;
– s2;
– }
– s3;
– when the condition is true statement s1, s2 and then s3 is
executed. If the condition is false only s3 is executed.

IF ELSE
– Also known as either or. This statement is used to select
one statement and ignore the other statements.
– if(condition)
– s1;
– else
– s2;
– when the condition is true statement s1 is
executed. If the condition is false s2 is executed. Thus one
of the statements, either s1 or s2 is always executed.

IF ELSE
IF ELSE
IF ELSE IF ELSE
– It is a branching statement which can choose and execute on of the
statements available depending upon the condition.
– if(condition1)
– s1;
– else if(condition2)
– s2;
– else
– s3;
– when the condition1 is true statement s1 is executed and
rest are ignored. When condition1 is false condition2 is verified. If
condition2 is true statement s2 is executed and other statements
are ignored and so on. Thus only one statement is executed from
the top, depending upon the condition. The statement following
else is executed when all the conditions are false.
IF ELSE IF ELSE
IF ELSE IF ELSE
IF ELSE IF ELSE
While
 It is a repeated structure statement which repeats a
statement given as long as the condition is true.
 while(condition)
 statement1;
 statement1 is executed till the condition is true.
While
Lecture:2 Control Statement CSC-115 Programming Fundamentals

While
Do while
– Like while statement it repeats a statement given as long
as the condition is satisfied unlike in while statement the
condition is checked at the end of the structure.
– do
– {
– statement1;
– }
– while(condition);
– statement1 is executed till the condition is true.

Do while
Do while
Do while
For
– It is a repeated structure which can repeat a statement as
long as the given condition is satisfied.
– for(statement1; condition;
statement2)
– statement3;
– where statement1 initializes control variable, condition
is used to check whether statement written after loop
can be repeated or not , statement2 is used to modify
the control variable. Statement3 is a simple statement
(having only one statement) or compound statement (a
set of statements written within braces {} ) which can
be repeated as long as condition is true or satisfied.
Switch
– It is a multi branch statement which can be used to select
and execute one of the available statements.
– switch(value)
– {
– case 1: statement 1; break;
– case 2: statement 2; break;
– case n: statement n; break;
– default: statement d;
– }
– Where value can be a variable of type numeric or character.
The case label 1 to n can also be written with constant
identifiers.
Lecture:2 Control Statement CSC-115 Programming Fundamentals

Switch
 When the value assigned matches with case label 1
statement 1 is executed.
 The break statement written after statement 1 transfers the
control out of the switch statement.
 When the value doesn’t match with case label 1 then it
checks with case label 2 and so on.
 When the value assigned doesn’t match with any of the case
labels (1 to n) then the default clause is considered and the
statement d is executed.
 Default clause is optional like else clause in if-else-if-else
statement.
Switch
Switch
Unconditional
 Return
 Goto
 Break
 Continue
The Goto Statement
 This system does not require any condition.
 This system passes control anywhere in the program without
considering any condition.
 Here a label is any valid label either before or after goto.
 The label must start with any character and can be constructed
with rules used for forming identifiers.
 Avoid using the goto stataemnt.
 Goto label;
------
-------
--------
Label:
The Goto Statement
The Goto Statement
The Goto Statement
The Goto Statement
The Break Statement
 The break statement allows the programmer to terminate the loop.
 The break skips from the loop or the block in which it is defined.
 The control then automatically passes onto the first statement
after the loop or the block.
 The break can be associated with the conditional statements (
specially switch case)
 We can also use break statements in the nested loops.
 If we use break statement in the innermost loop, then the control
of the program is terminated from that loop only and resumes at
the next statement following that loop.
 The widest use of this statement is in the switch case where it is
used to avoid flow of control from one case to the other.
The Break Statement
The Break Statement
The Break Statement
The Continue Statement
 The continue statement works somewhat like the break statement.
 Instead of forcing the control to the end of the loop( as it is in the
case of break), the continue case causes the control to pass on to
the beginning of the block / loop.
 In the case of for loop the continue case initiates the testing
condition and increment on steps has to be executed (while rest
of the statement following the continue are neglected ).
 For while and do while , the continue case causes control to pass
on to conditional tests .
 It is useful in a programming situation, where it is required that
particular iterations occur only up to some extent or when some
part of the code has to be neglected.
The Continue Statement

City University of Sciences & Information Technology Peshawar


Lecture:2 Control Statement CSC-115 Programming Fundamentals

The Continue Statement


Lecture:2 Control Statement CSC-115 Programming Fundamentals

The Continue Statement


Lecture:2 Control Statement CSC-115 Programming Fundamentals

The Continue Statement


Lecture:2 Control Statement CSC-115 Programming Fundamentals

Array
 An array is a series of elements of the same type placed in
contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.
 An array stores a fixed-size sequential collection of
elements of the same type.
 An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of
variables of the same type.
 All arrays consist of contiguous memory locations. The
lowest address corresponds to the first element and the
highest address to the last element.
Lecture:2 Control Statement CSC-115 Programming Fundamentals

Array Declaration and Initialization


 To declare an array in C++, the programmer specifies the
type of the elements and the number of elements required
by an array as follows −

 type arrayName [ arraySize ];


 This is called a single-dimension array.
The arraySize must be an integer constant greater than
zero and type can be any valid C++ data type. For
example, to declare a 10-element array called balance of
type double, use this statement −
 double balance[10];
Lecture:2 Control Statement CSC-115 Programming Fundamentals

Array Declaration and Initialization


 You can initialize C++ array elements either one by one or
using a single statement as follows −
 double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
 You will create exactly the same array as you did in the
previous example.
 balance[4] = 50.0;
Lecture:2 Control Statement CSC-115 Programming Fundamentals

The End

Potrebbero piacerti anche