Lect 3
Lect 3
Asst.Prof. Dr
Sinan Adnan Al-Shammari
PROGRAMMING 1 C++
Part 3 - Practical
1
Part Three
C++ Logical Operators
C++ Logical Operators
As with comparison operators, you can also test for true (1) or false (0) values
with logical operators.
Logical operators are used to determine the logic between variables or values:
Example 1:
#include <iostream>
using namespace std;
int main()
{
int age = 20;
int point = 600;
cout << (age >= 18 && point >= 450) << endl; // 1 => true
return 0;
}
Example 2:
#include <iostream>
using namespace std;
int main()
{
int age = 14;
int point = 600;
cout << (age >= 18 && point >= 450) << endl; //0 => false
return 0;
}
Example 3:
#include <iostream>
using namespace std;
int main()
{
int age = 14;
int point = 600;
cout << (age >= 18 || point >= 450) << endl; // 1 => true
return 0;
}
Example 4:
#include <iostream>
using namespace std;
int main()
{
int age = 14;
int point = 320;
cout << (age >= 18 || point >= 450) << endl; //0 => false
return 0;
}
Example 5:
#include <iostream>
using namespace std;
int main()
{
cout << (100 == 10 || 50 == 10 || 20 == 10 || 10 == 10) << endl;
// 1 => True
return 0;
}
Example 5:
#include <iostream>
using namespace std;
int main()
{
cout << (10 == 10) << endl; // 1 => True
cout << !(10 == 10) << endl; // 0 => False
cout << !(100 == 10) << endl; // 1 => True
return 0;
}
Part Three
C++ Conditions and If Statements
You can use these conditions to perform different actions for different decisions.
The if Statement
Use the if statement to specify a block of C++ code to be executed if a condition
is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an
error.
Example 1:
#include <iostream>
using namespace std;
int main()
{
int age = 15;
cout << "Welcome c++\n";
int main()
{
int age = 20;
cout << "Welcome\n";
Example 1:
#include <iostream>
using namespace std;
int main()
{
int age = 15;
if (age >=18)
{
cout << " age is not greater than \n";
}
return 0;
}
Example 2:
#include <iostream>
using namespace std;
int main()
{
int age = 15;
int point = 700;
if (age >= 18)
{
cout<<"welcome your age is ok \n";
}
else if (point > 400)
{
cout << "welcome your point is ok \n";
}
return 0;
}
Example 3:
#include <iostream>
using namespace std;
int main()
{
int age = 20;
int point = 700;
if (age >= 18)
{
cout<<"welcome your age is ok \n";
}
else if (point > 400)
{
cout << "welcome your point is ok \n";
}
return 0;
}
Example 3:
#include <iostream>
using namespace std;
int main()
{
int age = 15;
int point = 300;
int rank = 7;
if (age >= 18)
{
cout<<"welcome your age is ok \n";
}
else if (point > 400)
{
cout << "welcome your point is ok \n";
}
else if (rank > 6)
{
cout << "welcome your rank is ok \n";
}
return 0;
}
Example 4:
#include <iostream>
using namespace std;
int main()
{
int age = 15;
int point = 300;
int rank = 3;
if (age >= 18)
{
cout<<"welcome your age is ok \n";
}
else if (point > 400)
{
cout << "welcome your point is ok \n";
}
else if (rank > 6)
{
cout << "welcome your rank is ok \n";
}
return 0;
}
Nested if in C++
If is a type of condition checking in which a condition turns out to be true a block
of statements is executed.
When a number of if blocks are present one after another with the same scope
(the same scope means under one { } block), then that condition is termed as a
Nested if condition. If the first condition is True, we go into the next if condition
and the subsequent condition is checked until we get a false condition, and the
checking stops.
Syntax:
// if base_condition is true
// every inside the { } block will be executed
if (base_condition)
{
statement 1............... statement 2 ..............
}
Example:
#include <iostream>
using namespace std;
int main()
{
int age = 25;
int points = 1500;
return 0;
}
Ternary Operator
There is also a short-hand if else, which is known as the ternary operator
because it consists of three operands.
It can be used to replace multiple lines of code with a single line, and is often
used to replace simple if else statements:
Syntax
variable = (condition) ? expression True : expression False;
Example 1:
#include <iostream>
using namespace std;
int main()
{
int age = 15;
cout << (age >= 18 ? "Age Is OK\n" : "Age Is Not OK\n");
return 0;
}
Example 2:
#include <iostream>
using namespace std;
int main()
{
int age = 15;
string result = age >= 18 ? "Age Is OK\n" : "Age Is Not OK\n";
if (day == 1)
{
cout << "Open From 08:00 To 14:00";
}
else if (day == 2)
{
cout << "Open From 08:00 To 14:00";
}
else if (day == 3)
{
cout << "Open From 10:00 To 16:00";
}
else
{
cout << "Closed";
}
Example 2:
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Choose A Day From 1 To 25\n";
cin >> day;
switch (day)
{
case 1:
cout << "Open From 08:00 To 14:00";
break;
case 2:
cout << "Open From 08:00 To 14:00";
break;
case 3:
cout << "Open From 10:00 To 16:00";
break;
default:
cout << "Closed";
}
return 0;
}
Example 3:
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Choose A Day From 1 To 25\n";
cin >> day;
switch (day)
{
case 1:
cout << "Open From 08:00 To 14:00";
//break;
case 2:
cout << "Open From 08:00 To 14:00";
break;
case 3:
cout << "Open From 10:00 To 16:00";
break;
default:
cout << "Closed";
}
return 0;
}
Example 4:
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Choose A Day From 1 To 25\n";
cin >> day;
switch (day)
{
case 1:
case 2:
cout << "Open From 08:00 To 14:00";
break;
case 3:
cout << "Open From 10:00 To 16:00";
break;
default:
cout << "Closed";
}
return 0;
}
Example 5:
#include <iostream>
using namespace std;
int main()
{
float day;
cout << "Choose A Day From 1 To 25\n";
cin >> day;
switch (day)
{
case 1.4:
case 2:
cout << "Open From 08:00 To 14:00";
break;
case 3:
cout << "Open From 10:00 To 16:00";
break;
default:
cout << "Closed";
}
return 0;
}