0% found this document useful (0 votes)
3 views

Lec 3

Uploaded by

o0kwame0o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lec 3

Uploaded by

o0kwame0o
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Structured Programming

using C++

Lecture 3 (15 Oct 2024)


Microsoft Teams Team

To follow the course:
– Open Microsoft Teams app.
– Sign-in with your university edu mail;
– Press on “Join or create a team” icon;
– In the “Join a team with a code” Enter the following
– code:

2r47chr

2
Operators in C++

Are used for operations!

Between values: cout << 5 + 6

Between variables: cout << x + y

Between variables and values: x = x + 4

Different types of operations:
– Arithmetic: + - * / % ++ --
– Assignment: = += -= *= /= %= &= |= ^= >>= <<=
– Comparison: == != > < >= <=
– Logical: && || !
– Bitwise: & | ~

3
Arithmetic Operators
01.cpp
int x = 15, y = 2;
float a = 15, b = 2;
cout << x + y << endl;
cout << x - y << endl;
cout << x * y << endl;
cout << x / y << endl;
cout << a / b << endl;
cout << x % y << endl;
cout << x++ << "\t" << x-- << "\t" << x << endl;
cout << ++y << "\t" << --y << "\t" << y << endl;
cout << 1 + 2 * 3 / 4 << endl;
cout << (1 + 2) * 3 / 4 << endl;

4
Assignment Operators
02.cpp
int x = 15, y = 2;
x = y; cout << x << endl;
x += y; cout << x << endl; //x = x + y
x -= y; cout << x << endl; //x = x - y
x *= y; cout << x << endl; //x = x * y
x /= y; cout << x << endl; //x = x / y
x %= y; cout << x << endl; //x = x % y

5
Comparison Operators
03.cpp
int x = 15, y = 2;
cout << (x == y) << endl;
cout << (x != y) << endl;
cout << (x > y) << endl;
cout << (x < y) << endl;
cout << (x >= y) << endl;
cout << (x <= y) << endl;
x = 2;
cout << (x >= y) << endl;
cout << (x <= y) << endl;

6
Logical Operators
04.cpp
int x = 15, y = 0;
cout << ((x == y) && (x != y)) << endl;
cout << ((x == y) || (x != y)) << endl;
cout << (!(x == y)) << endl;
cout << (!((x == y)&&(x != y))) << endl;
cout << (x && y) << endl;
cout << (x || y) << endl;
cout << (!x) << endl;

7
Strings
05.cpp
string firstName = "Marwa";
string last_name = "Yusuf";
//Note variable names
cout << firstName << last_name << endl;
cout << firstName << " " << last_name << endl;
string fullName = firstName.append(last_name);
cout << fullName << endl;
int x = 10, y = 20;
string a = "10", b = "20";
cout << x + y << endl;
cout << a + b << endl;
cout << x + a << endl; //wrong

8
More Strings
06.cpp
string name = "Fady Hasan";
cout << name.length() << "\t" << name.size() << endl;
cout << name[0] << "\t" << name[1] << "\t";
cout << name[name.length() - 1] << endl;
cout << name.at(0) << "\t" << name.at(1) << "\t";
cout << name.at(name.length() - 1) << endl;
name[0] = 'K';
cout << name << endl;
name = "Fady \t Hasan \n";
cout << name;

9
Math
07.cpp
#include <iostream>
#include <cmath>
using namespace std;

int main() {
cout << max(5, 10) << endl;
cout << min(5, 10) << endl;
cout << sqrt(9) << endl;
cout << round(27.15) << endl;
cout << log(2) << endl;
return 0;
}

10
Control Flow

The order which the instructions follow during program execution.

3 basic control structures:
– Sequence (ordinary order)
– Selection (if, if/else, switch)
– Repetition(while, do/while, for)

11
Control Flow - Sequence

All what we had till now was sequential execution.

12
Control Flow – Conditions – If

int x = 5, y = 10;
if (x > y)
cout << x << endl;
cout << "Bye!\n";

13
Control Flow – Conditions – If Else

int x = 5, y = 10;
if (x > y)
cout << x << endl;
else
cout << y << endl;
cout << "Bye!\n";

14
Control Flow – Conditions – If Else If Else If ... Else
08.cpp
int x, y, z;
cin >> x;
cin >> y;
cin >> z;
if (x == 20)
cout << x << endl;
else if (y == 20)
cout << y << endl;
else if (z == 20)
cout << z << endl;
else
cout << "Hard Luck!\n";
cout << "Bye!\n";

15
Control Flow – Conditions – Switch
09.cpp
int choice;
cin >> choice;
switch(choice) {
case 1 : printf("Arch\n"); break;
case 2 : printf("Elec\n"); break;
case 3 : printf("Mech\n"); break;
case 4 : printf("Civil\n"); break;
default : printf("Surv\n");
}
cout << "Good Luck!\n";
16
That’s Enough for Today

See You Next Week

17

You might also like