CPT211
CPT211
STRUCTURES
Beware of mistaking
the assignment = for
the equality ==
6
Relational Operators
Examples:
Expression Meaning Value
8 < 15 8 is less than 15 true
6 != 6 6 is not equal to 6 false
7
Logical (Boolean) Operators
• Logical or Boolean operators enable you to
combine logical expressions
A unary operator
Binary operators
8
Logical (Boolean) Operators
• The && operator (logical and)
– If both operands are true, the result is true
– If either or both operands is false, the comparison is
false
• The || operator (logical or)
– If either or both of the operands are true, the
comparison is true
– The comparison is false only if both operands are false
• The ! operator (logical not)
– The not operator reverses the logical value of the one
operand 9
3 Basic Control Structures
There are three types of Control Structures
1. Sequence
2. Selection/Conditional/Decision
3. Looping/ Repetition/ Iteration
1. Sequential Structure
• Default structure in programming
– statements are executed one after another in
the order of their appearance in the source
code, i.e. execute first statement, then second
statement, then third statement, etc
– Example:
#include <iostream>
using namespace std;
main()
{
int a = 5; //first statement
int b = 6; //Second statement
int sum; //Third statement
main()
{
// declare variable num
int num;
main()
{
int num;
main()
{
int month;
main()
{
int num;
main()
{
int num;
if (num > 0)
cout << num << " is POSITIVE\n";
else
if (num < 0)
cout << num << " is NEGATIVE\n";
else
cout << num << " is ZERO\n";
}
Example #2
#include <iostream>
using namespace std;
main()
{
int scores;
cout<<"enter scores: ";
cin>>scores;
if (scores>=70)
cout<<"A";
else
if(scores>=60)
cout<<"B";
else
if(scores>=50)
cout<<"C";
else
if(scores>=45)
cout<<"D";
else
if(scores>=40)
cout<<"E";
else
cout<<"F";
}
CASCADED if-else STATEMENT
• If nesting is carried out to too deep a level and indenting is not
consistent then deeply nested if-else statements can be
confusing to read and interpret.
• Thus, a more consistent layout based on the syntax below is
used, which we can also refer to as cascaded if-else
statement:
if ( condition1 )
statement1 ;
else if ( condition2 )
statement2 ;
...
else if ( condition-n )
statement-n ;
else
statement-e ;
Example #2
#include <iostream>
main()
{
int num;
if (num > 0)
cout << num << " is POSITIVE\n";
else if (num < 0)
cout << num << " is NEGATIVE\n";
else
cout << num << " is ZERO\n";
}
Example #3
#include <iostream>
main()
{
int month;
main()
{
int ctr; // initialize counter
main()
{
int ctr;
<initialization>;
while (<expression>)
<statement>;
• Observe that in the while syntax, we did not include the <change of
state>, simply because it is written as part of the <statement>.
• This implies that the <change of state> can be written at the
beginning, in the middle or at the last line of <statement>.
• Note that <statement> can be a single or a block of statements. If
what we have is the latter, we need to enclose the block of
statements in a pair of curly brackets, i.e., {}.
Example #1
#include <iostream>
main()
{
int ctr;
ctr = 1; // initialization
while (ctr <= 5) // conditional check
{
cout << "Jose Rizal University\n";
ctr++; // change of state
}
}
Example #2
#include <iostream>
using namespace std;
main()
{
int ctr;
ctr = 2; // initialization
while (ctr <= 20) // conditional check
{
cout << ctr << "\n";
ctr = ctr + 2; // change of state
}
}
Example #3
#include <iostream>
void main(void)
{
int ctr;
int num;
ctr = 1;
while (ctr <= 10)
{
cout << "Input integer number " << ctr << ": ";
cin >> num;
cout << "Integer number " << ctr << "= " << num << "\n";
ctr++;
}
}
THE do-while STATEMENT
• The syntax for a do-while loop is as follows:
<initialization>;
do
<statement>;
while (<expression>);
main()
{
int ctr;
ctr = 0; // initialization
main()
{
int num=10;
do
{
cout << num << "\n";
num--;
} while (num > 0);
}
Counters and Accumulators
• A counter is a variable that is used to keep
track of the count (as the name suggest)
of a certain group of items. Usually,
– its data type is int
– it is initialized to a value of 0
– incremented by 1 inside a loop
Example (Counter)
main()
{
int ctr; // this is for the loop counter
int num; // this is for the input value
int ctr_positive; // this is for the counter
sum = 0; // initialization
cout << "The sum of the integers is " << sum << "\n";
}
break and continue Statements
• break
– We have already seen the use of the break
statement when we discussed the switch-
case statement.
– There is actually no difference in the
semantics if being used in a loop structure.
– If executed, it terminates the loop structure
that contains it.
Example (break)
main()
{
int num; // this is for the input value
int sum; // this is for the accumulator
sum = 0; // initialization
cout << "The sum of the integers is " << sum << "\n";
}
break and continue Statements
(continued…)
• continue
– can only used inside loops.
– When executed, it transfers control to the
<change of state> statement of the for loop
and, and to the <expression> part of the while
or do-while loop skipping any statements that
follow it.
Example (continue)
main()
{
int ctr; // this is for the loop counter
int sum; // this is for the accumulator
sum = 0; // initialization
cout << "The sum of the integers is " << sum << "\n";
}
Practical Exercises #1
Question 1: Quarters in a year:
• {Jan-March = 1st}
• {April-Jun = 2nd }
• {July-Sept = 3rd }
• {Oct-Dec = 4th }
Write a nested if – else statements to
make selections between the quarters
Write an alternative switch-case to make
selections within the quarters
Practical Exercises #2
Question 2: Write a program to countdown
using; (i.e 10-1)
1. while-loop
2. For loop
3. Do while loop
Reference Text
• Robert Lafore, Object oriented
programming in C++. Fourth edition, Sams
publishing, 2002.