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

Programming - U1 ( Basics of C++ ) (1)

Chapter one introduces the basics of C++ programming, defining key terms such as program, programming, programmer, and user. It covers programming languages, algorithms, program construction, operators, expressions, statements, and flow control mechanisms like decisions and looping. Additionally, it explains the use of control statements such as if, switch, and various loop constructs for executing code conditionally and repetitively.

Uploaded by

ermikasa2716
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)
5 views

Programming - U1 ( Basics of C++ ) (1)

Chapter one introduces the basics of C++ programming, defining key terms such as program, programming, programmer, and user. It covers programming languages, algorithms, program construction, operators, expressions, statements, and flow control mechanisms like decisions and looping. Additionally, it explains the use of control statements such as if, switch, and various loop constructs for executing code conditionally and repetitively.

Uploaded by

ermikasa2716
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/ 23

Chapter one

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, …)

High Level Languages


(C++)

Low Level Languages


(Assembly, Machine Code)
The Blueprint
• You can think of an algorithm as the blueprint
(plan) of your program.
• Before you begin writing a program, you first
have to understand the problem and then
develop a step-by-step solution (the
algorithm).
• Developing an algorithm to solve a problem is
usually more difficult than writing the code.
Example Flowcharts
Basic Program Construction

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: expr stands for any expression to be operated on by the operator.The


term operand is also used.
Relational and Logical Operators
 return values of type bool.
 used to test certain conditions to find out whether they are true or not.
Increment and Decrement Operators

 Operation of adding or subtracting one from a variable is done by


increment (++) and decrement (--) operators
 There are two forms of these operators:
• prefix and postfix.
Prefix increment operation increments its operand and returns the
changed value as its result
Postfix version increments the operand but returns the copy of the
original unchanged value as its result
Increment and Decrement 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy