C Skills
C Skills
C Skills
C++ Basics
What is an Identifier?
• An identifier is the name to denote labels,
types, variables, constants or functions, in a
C++ program.
• C++ is a case-sensitive language.
– Work is not work
simple structured
address
float double long double
pointer reference
C++ Primitive Data Types
Primitive types
integral floating
unsigned
Premitive Data Types in C++
• Integral Types
– represent whole numbers and their negatives
– declared as int, short, or long
• Character Types
– represent single characters
– declared as char
– Stored by ASCII values
• Boolean Type
• declared as bool
• has only 2 values true/false
• will not print out directly
• Floating Types
– represent real numbers with a decimal point
– declared as float, or double
– Scientific notation where e (or E) stand for “times 10 to the ” (.55-e6)
Samples of C++ Data Values
int sample values
4578 -4578 0
bool values
true false
starting = count + 5;
• Expression evaluation:
– Get value of count: 0
– Add 5 to it.
– Assign to starting 5
Input and Output
• C++ treats input and output as a stream of
characters.
• stream : sequence of characters (printable or
nonprintable)
• The functions to allow standard I/O are in
iostream header file or iostream.h.
• Thus, we start every program with
#include <iostream>
using namespace std;
Include Directives and Namespaces
• include: directive copies that file into your
program
• namespace: a collection of names and their
definitions. Allows different namespaces to
use the same names without confusion
Keyboard and Screen I/O
#include <iostream>
executing
Keyboard program Screen
cin cout
cin >> x;
cin >> y;
• converts x to double and then does mixed division, not integer divide
• static_cast<int> (z) - will truncate z
• static_cast <int> (x + 0.5) - will round positive x {use () to cast complete
expression)
• Cast division of integers to give real result:
int x=5, y=2; double z; z = static_cast <double>(x/y) ; // 2.0
Arithmetic Operators
• Operators: +, -, * /
• For floating numbers, the result as same as Math
operations.
• Note on integer division: the result is an integer. 7/2
is 3.
• % (remainder or modulo) is the special operator just
for integer. It yields an integer as the result. 7%2 is
1.
• Both / and % can only be used for positive integers.
• Precedence rule is similar to Math.
Arithmetic Expressions
• Arithmetic operations can be used to express the
mathematic expression in C++:
b 2 4ac b *b 4* a *c
x( y z ) x * ( y z)
1
2 1 /( x * x x 3)
x x3
ab (a b) /(c d )
cd
Simple Flow of Control
• Three processes a computer can do:
– Sequential
expressions, insertion and extraction operations
– Selection (Branching)
if statement, switch statement
– Repetition/Iteration (Loop)
while loop, do-while loop, for loop
bool Data Type
• Type bool is a built-in type consisting of just
2 values, the constants true and false
• We can declare variables of type bool
bool hasFever; // true if has high temperature
bool isSenior; // true if age is at least 55
• The value 0 represents false
• ANY non-zero value represents true
Boolean Expression
• Expression that yields bool result
• Include:
6 Relational Operators
< <= > >= == !=
3 Logical Operators
! && ||
Relational Operators
are used in boolean expressions of form:
ExpressionA Operator ExpressionB
temperature > humidity
B * B - 4.0 * A * C > 0.0
abs (number) == 35
initial != ‘Q’
• Notes:
o == (equivalency) is NOT = (assignment)
o characters are compared alphabetically. However, lowercase letters are
higher ASCII value.
o An integer variable can be assigned the result of a logical expression
o You cannot string inequalities together:
Bad Code: 4<x<6 Good Code: (x > 4) &&(x < 6)
Relational Operators
int x, y ;
x = 4;
y = 6;
EXPRESSION VALUE
x<y true
x+2<y false
x != y true
x + 3 >= y true
y == x false
y == x+2 true
y=x+3 7
y=x<3 0
y=x>3 1
Logical Operators
are used in boolean expressions of form:
ExpressionA Operator ExpressionB
A || B (true if either A or B or both are true. It is false otherwise)
A && B (true if both A and B are true. It is false otherwise)
or
Operator Expression
!A (true if A is false. It is false if A is true)
Notes:
Highest precedence for NOT, AND and OR are low precedence.
Associate left to right with low precedence. Use parenthesis to override priority or for
clarification
– x && y || z will evaluate “x && y ” first
– x && (y || z) will evaluate “y || z” first
Logical Operators
int age ;
bool isSenior, hasFever ;
float temperature ;
age = 20;
temperature = 102.0 ;
isSenior = (age >= 55) ; // isSenior is false
hasFever = (temperature > 98.6) ; // hasFever is true
EXPRESSION VALUE
isSenior && hasFever false
isSenior || hasFever true
!isSenior true
!hasFever false
Precedence Chart
• ++, --, !, - (unary minus), + (unary plus) Highest
• *, /, %
• + (addition), - (subtraction)
• <<, >>
• <, <=, >, >=
• ==, !=
• &&
• ||
• = Lowest
Boolean Expression (examples)
taxRate is over 25% and income is less than $20000
age is 21 or 22
Boolean Expression (examples)
(taxRate > .25) && (income < 20000)
TRUE
expression
statement(s) FALSE
Simple if Statement Syntax
if (Boolean Expression)
Statement
if (Bool-Expr)
{
Statement_1
…
Statement_n
}
These are NOT equivalent. Why?
if (number == 0 ) if (number == 0 )
{ cout << “Hmmmm ”;
cout << “Hmmmm ”; cout << “You entered invalid number.\n”;
cout << “You entered invalid number.\
n”;
}
When number has value 0, the
When number has value 0, the output will be:
output will be: Hmmmm You entered invalid number.
Hmmmm You entered invalid number.
When number has value NOT 0,
When number has value NOT 0, the output will be:
there is NO output. You entered invalid number.
These are equivalent. Why?
if (number == 0 ) if (!number )
{ {
. .
. .
} }
TRUE FALSE
expression
if (Bool-Expression )
{
“if clause”
}
else
{
“else clause”
}
Loop
• is a repetition control structure.
• causes a single statement or block of
statements to be executed repeatedly until a
condition is met.
• There are 3 kinds of loop in C++:
– While loop
– Do-While loop
– For loop
While Loop
SYNTAX
while ( Expression )
{
… // loop body
}
• No semicolon after the boolean expression
• Loop body can be a single statement, a null statement,
or a block.
While Loop Mechanism
do
{
cin>>x;
if (x % 2 ==0)
break;
} while (x > 0); // exits when an even number is entered
CONTINUE Statement
allows you to skip the rest of the loop body and go
back to the beginning of the loop.
do
{
cin>>x;
if (x % 2 == 0)
continue;
cout<<x<<endl;
} while (x <100);
//prints out all odd numbers entered less than 100
Program Style
• Indenting:
– Separate processes with blank lines
– Blank lines are also ignored and are used to increase
readability.
– indent statements within statements (loop body)
• Comments:
– // tells the computer to ignore this line.
– for internal documentation. This is done for program
clarity and to facilitate program maintenance.
General rules for Comments
• Place a comment at the beginning of every file
with the file name, version number, a brief
program description, programmer’s name.
• Place a descriptive comment after each
variable declared.
– Use a blank line before and after variable
declarations
• Place a descriptive comment and a blank line
before each subtask.
Constants
• Syntax: const type identifier = value;
• Ex: const double TAX_RATE = 0.08;
• Convention: use upper case for constant ID.
Why use constants?