Programming - U1 ( Basics of C++ ) (1)
Programming - U1 ( Basics of C++ ) (1)
Basics of C++
The Basics
• Program: a list of instructions that tells the
computer how to solve a problem or perform
a task.
• Programming: the act of defining these
instructions.
• Programmer: the person doing the defining.
• User: the person who ultimately makes use of
the program. (Examples??)
Remember
• Computers are digital devices that simply do
what they are told.
• We must speak their language to tell them
what to do. ??
• Does this mean, we should
speak binary? ( 0’s and 1’s)??
Programming Languages
Very High Level
Languages
(SQL, Prolog, …)
This is a comment line. All lines beginning with two slash signs (//) are
considered comments and do not have any effect on the behavior of the
program.
Lines beginning with a hash sign (#) are preprocessor directives.
All the elements of the standard C++ library are declared within what is
called a namespace, the namespace with the name std.
This line corresponds to the beginning of the definition of the main
function.
This line is a C++ statement. A statement is a simple or compound
expression that can actually produce some effect
Operators, Expressions and Statements
Statements
– A statement is the smallest independent unit in a C++ program.
– Statements in C++ generally end in semicolons.
Expressions
– Any arrangement of variables, constants and operators that
specifies a computation.
– 3.14, 42+3, and val1*(23+ val1) are expressions.
Variables are lvalues and so may appear on the left-hand side of
an assignment.
Literal constants are rvalues and so may not be assigned
Const variables are unmodifiable lvalues and may not be assigned.
The results of most operators are rvalues.
Arithmetic Operators
Note: the prefix version returns an lvalue, the postfix version returns an rvalue;
PROGRAM FLOW CONTROL
• In C++, by default, statements are executed sequentially.
• In reality, however, not many programs execute all their
statements in strict order from beginning to end.
• Most programs (like many humans) decide what to do in
response to changing circumstances.
• They may do one thing if some condition is met or another if
not (decisions).
• They may also have to do a certain action repeatedly
(looping).
• Therefore, C++ defines a set of flowofcontrol statements that
allow statements to be executed conditionally or repeatedly.
Decisions and Branching
• The if Statement unsigned short age;
cout<<”Enter your age:> “;
Example: cin>>age;
if (age < 17)
cout<<”You are too young\n”;
unsigned short age; else
cout<<”Enter your age:> “; if (age<40)
cin>>age; cout<<”Don't worry, you are still
young\n”;
if (age < 17)
else
cout<<”You are too young\ if (age<70)
n”; cout<<”Still young at heart\n”;
cout<<”Thank you”<<endl; else
cout<<”Are you sure that is your
age?\n”;
Exercises:
1. Develop a program that tells students their grade based on the
following scale.
Mark>90 A
90 ≥ Mark > 85 A-
85≥ Mark > 82 B+
82 ≥ Mark > 76 B
76 ≥ Mark > 70 C+
70 ≥ Mark > 61 C
61 ≥ Mark > 50 C-
50≥ Mark > 43 D
Mark ≤ 43 F
The switch Statement
• As an alternative method of choosing among a
set of mutually exclusive choices, C++ provides
the switch statement. switch (variable)
{
• Syntax: case constant1:
statements
break;
case constant2:
statements
break;
…
case constantN:
statements
break;
default:
statements
}
Looping
• A program may also need to execute a group of
statements more than once. C++ provides us with the
while, do while, and for statements for performing
repetitive tasks.
The while Loop Statement
• A while statement repeatedly executes a statement if
a certain condition is true.
• Syntax:
while (condition)
statement
Loping …
• The do … while Loop Statement
• There are cases where we may want to execute the body of the
loop at least once before testing the condition. In other words,
there are times where we may need a posttest loop.
This is where the do … while loop comes in.
• Syntax:
do
statement
while (condition);
Looping …
The for Loop Statement
• The for loop is ideal for working with counters
because it provides built-in expressions that
initialize and update variables.
Syntax:
for (initialization; condition; update)
statement
Looping …
Nested Loops
• Just like we did with if … else statements, it is also
possible to put one loop inside another loop.
• The first loop is called the outer loop and the one
inside it is known as the innerloop. You can nest your
loops as much as you like.
Looping …
The continue and break Statements
• The break statement can also be placed inside a
loop. When it is encountered, the loop stops and
program jumps to the statement following the
loop.
Looping …
• The break statement provides a way to bypass the loop condition
and interrupt the loop
• In a nested loop, the break statement interrupts only the loop it
is placed in.
• The continue statement, placed inside a loop, causes the loop to
stop its current iteration and begin the next one.
• All statements in the body of the loop that come after it are
ignored.
Next Array and string