You are on page 1of 24

C++ How to Program

Introduction to C++
programming language
Program Life cycle
Analyze:
Define Problem

System and
Complete the
Software
documentation
Design

Choose the
Test and debug
interface

Write the
Program
variable
• a variable is a storage area in the computer's
memory.
• Every variable has:
• a name,
• a type,
• a value,
• address
• When new value is placed into a variable, it replaces
the previous value.
• Reading variables from memory does not change their
value.
Variable Name
• There are rules you should, and usually must, follow when
naming your variables.

• The name of a variable:


1. Must begin with a letter (A-Z), or _
2. Cannot have a period, $, +, -, /, and other special
characters.
3. Can have up to 255 characters. Please, just because it is
allowed, don't use 255 characters.
4. Must be unique inside of the function
5. the Name can't be keyword used in C++ Language
6. the Name of variable can't include space
• Once a variable has a name, you can use it as you see fit. For
example, you can assign it a value and then use the variable in
your program as if it represented that value.
Arithmetic operators

C o p e ra tio n Arith m e tic Alg e b ra ic C e xp re ssio n


o p e ra to r e xp re ssio 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
Rules for Division
• C++ treats integers different than doubles.
• 100 is an int.
• 100.0 , 100.0000, and 100. are doubles.
• The general rule for division of int and double types is:
• double/double -> double (normal)
• double/int -> double (normal)
• int/double -> double (normal)
• int/int -> int (special case: any decimal places discarded)
Assignment Operators
• Assignment operators abbreviate assignment expressions
c = c + 3;
can be abbreviated as c += 3; using the addition
assignment operator
• Statements of the form
variable = variable operator expression;
can be rewritten as
variable operator= expression;
• Examples of other assignment operators:
d -= 4 (d = d - 4)
e *= 5 (e = e * 5)
f /= 3 (f = f / 3)
g %= 9 (g = g % 9)
Increment and Decrement Operators
• Increment operator (++)
• Can be used instead of c+=1
• Decrement operator (--)
• Can be used instead of c-=1
• Preincrement
• Operator is used before the variable (++c or --c)
• Variable is changed before the expression it is in is
evaluated
• Postincrement
• Operator is used after the variable (c++ or c--)
• Expression executes before the variable is changed
Increment and Decrement Operators
• If c equals 5, then
x= ++c + 5; // the value of x is 11
but
x= c++ + 5; // the value of x is 10
• In either case, c now has the value of 6
• When variable not in an expression
• Preincrementing and postincrementing have the same
effect
++c;
• Has the same effect as
c++;
++Input/Output in C
input process output
"include "iostream#
;using namespace std

{ )(int main
; >>cin
;<<cout
}
A First Program
#include <iostream>
using namespace std;
int main() {
// Extract length and width
cout << "Rectangle dimensions: ";
float Length;
float Width;
cin >> Length >> Width;

// Compute and insert the area

float Area = Length * Width;

cout << "Area = " << Area << " = Length "
<< Length << " * Width " << Width << endl;
return 0;
}
The if/else Selection
Structure
int mark;
cout <<“Enter your mark”;
Cin>> mark;
char grade;
if (mark>90)
grade='A';
else if (mark>80)
grade='B';
else if (mark>70)
grade='C';
else if (mark>60)
grade=‘D';
else
grade='F';

cout <<“your grade is ” << grade;


The if/else Selection
Structure
• Compound statement:
• Set of statements within a pair of braces
• Example:
if ( grade >= 60 )
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course
again.\n" ;
}
• Without the braces, the statement
cout << "You must take this course
again.\n" );
would be executed automatically
Decision Making: Equality and Relational
Operators
Sta n d a rd a lg e b ra ic C e q u a lity Exa m p le Me a nin g o f
e q u a lity o p e ra to r o r o r re la tio na l of C C c o n d itio n
re la tio n a l o p e ra to r o p e ra to r c o n d itio n
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
Switch Statement
Example

switch (expression) { switch (i) {


case const_expr1; case 1:
cout << “*\n”;
statement1; break;
break; case 2:
cout << “**\n”;
case const_expr2; break;
statement2; case 3:
cout << “***\n”;
break; break;
… case 4:
cout << “****\n”;
default: break;
statementn; case 5:
cout << “*****\n”;
} break;
}
Logical Operators
• && ( logical AND )
Returns true if both conditions are true
• || ( logical OR )
Returns true if either of its conditions are true
• ! ( logical NOT, logical negation )
Reverses the truth/falsity of its condition
Unary operator, has one operand
• Useful as conditions in loops
Expression Result
true && false false
true || false true
!false true
The Essentials of Repetition
• Loop
• Group of instructions computer executes repeatedly while some
condition remains true
• Counter-controlled repetition
• Definite repetition: know how many times loop will execute
• Control variable used to count repetitions
• Sentinel-controlled repetition
• Indefinite repetition
• Used when number of repetitions not known
• Sentinel value indicates "end of loop"
Counter-Controlled Repetition
• Example:
int counter = 1; // initialization
while ( counter <= 10 ) { // repetition
condition
cout<< counter;
++counter; // increment
}
• The statement
int counter = 1;
• Names counter
• Declares it to be an integer
• Reserves space for it in memory
• Sets it to an initial value of 1
The for Repetition Structure
• Format when using for loops
for ( initialization; loopContinuationTest; increment )
statement
• Example:
for( int counter = 1; counter <= 10; counter++ )
cout<<counter<<endl;
– Prints the integers from one to ten No
semicolon
(;) after last
expression
The do/while Repetition Structure
• The do/while repetition structure
• Similar to the while structure
• Condition for repetition tested after the body of the loop is
performed
• All actions are performed at least once
• Format:
do {
statement;
} while ( condition );
The do/while Repetition Structure
• Example (counter = 1):
do {
cout<< counter ;
} while (++counter <= 10);
• Prints the integers from 1 to 10
The break and continue Statements
• break
• Causes immediate exit from a while, for, do/while or
switch structure
• Program execution continues with the first statement after the
structure
• Common usage of the break statement
• Escape early from a loop
• Skip the remainder of a switch structure
The break and continue Statements
• continue
• Skips the remaining statements in the body of a while, for or
do/while structure
• Proceeds with the next iteration of the loop
• while and do/while
• Loop-continuation test is evaluated immediately after the
continue statement is executed
• for
• Increment expression is executed, then the loop-continuation test is
evaluated
1 /* Fig. 4.12: fig04_12.c
2 Using the continue statement in a for structure */
3 #include <iostream>
4 using namespace std
5 int main()
6 {
7 int x;
8
9 for ( x = 1; x <= 10; x++ ) {
10
11 if ( x == 5 )
12 continue; /* skip remaining code in loop only
13 if x == 5 */
14
15 cout<<x;
16 }
17
18 cout<<endl<<"Used continue to skip printing the value 5\n";
19 return 0;
20 }

1 2 3 4 6 7 8 9 10
Used continue to skip printing the value 5

You might also like