LECTURER NOTES: JAVA vs C++
Topic: Operators, Sequence, Selection, and Iteration
Contextual Examples: Zimbabwean Situations
1. OPERATORS
Arithmetic Operators: + - * / %
Example: Calculating ZESA Token Cost
Arithmetic Operator Example
JAVA C++
double tokenPrice = 5.0; #include <iostream>
int units = 4; using namespace std;
double total = tokenPrice * units;
[Link]("Total cost = $" + int main() {
total); double tokenPrice = 5.0;
int units = 4;
double total = tokenPrice * units;
cout << "Total cost = $" << total;
return 0;
}
Logical Operators: && || !
Example: EcoCash Transaction Approval
Logical Operator Example
JAVA C++
if(balance >= amount && pinCorrect){ if(balance >= amount && pinCorrect){
[Link]("Transaction cout << "Transaction Approved";
Approved"); }
}
2. SEQUENCE
Definition: Statements executed step-by-step in order.
Example: Kombi Fare Collection
Sequence Example
JAVA C++
double fare = 1.0; double fare = 1.0;
int passengers = 18; int passengers = 18;
double total = fare * passengers; double total = fare * passengers;
[Link]("Total collected = $" + cout << "Total collected = $" << total;
total);
3. SELECTION
Example: Pass or Fail (ZIMSEC style)
If-Else Example
JAVA C++
if(mark >= 50){ if(mark >= 50){
[Link]("Pass"); cout << "Pass";
}else{ }else{
[Link]("Fail"); cout << "Fail";
} }
4. ITERATION (LOOPS)
Example: Printing Class Register
For Loop Example
JAVA C++
for(int i = 1; i <= 5; i++){ for(int i = 1; i <= 5; i++){
[Link]("Student " + i); cout << "Student " << i << endl;
} }
While Loop Example: Deducting balance until zero
While Loop Example
JAVA C++
while(balance > 0){ while(balance > 0){
balance -= 10; balance -= 10;
} }