Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Operators And

Loops
Daniel M. Ahiatrogah
Regent University College
September 2013
Recap of Preview Lesson
Dont use keywords as variables names
Give your variable meaningful names that reflect their
use.
Constants cannot be changed while the program is
running.
There are two ways of defining constants in C++ :
#define and using the const keyword
#define length 5
const int length = 5;
Const is a better way of defining a variable in C++.

Regent University College of


Science and Technology 2
Definition of Operators
An operator is a symbol that causes the
compiler to take an action.
Operators operates on operands.
Operands are expression.
An operator is a symbol that tells the compiler
to perform specific mathematical or logical
manipulations.

Regent University College of


Science and Technology 3
Categories of Operators
Thefollowings
The followingsare
aretypes
typesofofoperators
operatorsinin
C++
C++
Arithmetic
ArithmeticOperators
Operators
Relational
RelationalOperators
Operators
Logical
LogicalOperators
Operators
Bitwise
BitwiseOperators
Operators
Assignment
AssignmentOperators
Operators
Misc
MiscOperators
Operators
Regent University College of
Science and Technology 4
Arithmetic Operators
+ :Adds two operands
- : subtract second operand from first
* : for multiplying tow or more operands.
/ : divide numerator by de-numerateor.
% : Modulus Operator and remainder of
after an integer division
++ : increase integer value by one.
-- : decrease integer value by one.

Regent University College of


Science and Technology 5
#include <iostream>
using namespace std;

int main()
{
int result, i = 4, j = 3;
result = i%j;
cout << result <<endl;
i++;
cout << i <<endl;
i++;
cout << i <<endl;
i++;
i++;
i--;
cout << i << endl;
system("pause");
return 0;
}
Regent University College of
Science and Technology 6
Relational Operators
== : equal to
!= : not equal to
> : greater than
>= : greater than or equal to
< : less than
<= : less than or equal to

Regent University College of


Science and Technology 7
Logical Operators
&& : Logical And
|| : Logical OR
! : Logical Not

Regent University College of


Science and Technology 8
Bitwise Operators
Bitwise operator works on bits and perform bit-
by-bit operation. The truth tables for and (&),
and or (|)are as follows:
A B A&B A|B

0 0 0 0

0 1 0 1

1 1 1 1

1 0 0 1

Regent University College of


Science and Technology 9
Assignment Operator
= : [a = 4 ;]
+=: [ a +=b is the same as a = a + b ]
-= : [ a -=b is the same as a = a-b]
*= : [ a*=b is the same as a = a*b]
/=: [ a /=b is the same as a = a/b]
%= [ a %b is the same as a = a % b]

Regent University College of


Science and Technology 10
Misc Operators
Sizeof :
sizeof operator returns the size of a variable.
Condition? X: Y : [Conditional operator]
If Condition is true ? then it returns value X :
otherwise value Y

Regent University College of


Science and Technology 11
Loops

Regent University College of


Science and Technology 12
Introduction
A Loop statement is used when a block of code
is to be executed several times.
This is not the case with sequential code
execution: code is executed ones within a block.
Loops contain a condition that becomes the
reason for which particular line(s) of codes are
executed or otherwise within the block.
There are four types of loops statements in C++
programming language.
Regent University College of
Science and Technology 13
Regent University College of
Science and Technology 14
Types of Loops
for loop
Execute a sequence of statements within its block
multiple times.
while loop
Executes a sequence of statements while a
condition is true. The Loops only terminates when
the said condition becomes false. Condition is
tested at the beginning of the loop body.

Regent University College of


Science and Technology 15
Types of Loops
do while loop
Executes a sequence of statements while a
condition is true. The Loops only terminates when
the said condition becomes false. Condition is
tested at the end of the loop body.
nested loops
You can use one or more loop inside any another
while, for or do..while loop.

Regent University College of


Science and Technology 16
For Statement
Syntax:
for ( init; condition; increment)
{
statement(s);
}
Eg: for ( int i=1; i<5; i++)
{
cout << i;
}
Regent University College of
Science and Technology 17
While Loop
while (condition) Eg:
{ int i=0;
statement(s); while ( i < 5)
} {
cout << i;
i ++;
}

Regent University College of


Science and Technology 18
do . while Loop
do Eg:
{ int i = 0;
statement(s); do
} {
while( condition ); cout << i ;
i ++ ;
}
while ( i < 5 );

Regent University College of


Science and Technology 19
Nested Loop . 1
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
Regent University College of
Science and Technology 20
Nested Loop . 2
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
Regent University College of
Science and Technology 21
Nested Loop . 3
do
{
statement(s);
do
{
statement(s);
} while( condition );
}while( condition );
Regent University College of
Science and Technology 22
References
http://www.tutorialspoint.com/cp
lusplus/cpp_loop_types.htm

Regent University College of


Science and Technology 23

You might also like