0% found this document useful (0 votes)
14 views14 pages

Lect 3

The document provides an overview of C++ programming concepts, focusing on logical operators, conditional statements, and control flow structures such as if, else, and switch statements. It includes various examples to illustrate the use of these concepts in practical coding scenarios. The content is aimed at helping students understand how to implement logical conditions and decision-making in their C++ programs.

Uploaded by

ha9791165
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)
14 views14 pages

Lect 3

The document provides an overview of C++ programming concepts, focusing on logical operators, conditional statements, and control flow structures such as if, else, and switch statements. It includes various examples to illustrate the use of these concepts in practical coding scenarios. The content is aimed at helping students understand how to implement logical conditions and decision-making in their C++ programs.

Uploaded by

ha9791165
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/ 14

Wasit University

College of Computer Science & It


Computer Science Department

The First Stage

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:

Operator Name Description Example


Returns true if both statements
&& Logical and x < 5 && x < 10
are true
Returns true if one of the
|| Logical or x < 5 || x < 4
statements is true
Reverse the result, returns false
! Logical not !(x < 5 && x < 10)
if the result is true

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;
}

C++ Part 3 - Practical


2

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;
}

Asst.Prof. Dr Sinan Adnan Al-Shammari


3

Part Three
C++ Conditions and If Statements

C++ Conditions and If Statements


You already know that C++ supports the usual logical conditions from
mathematics:

• Less than: a < b


• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C++ has the following conditional statements:

• Use if to specify a block of code to be executed, if a specified condition is


true
• Use else to specify a block of code to be executed, if the same condition
is false
• Use else if to specify a new condition to test, if the first condition is false
• Use switch to specify many alternative blocks of code to be executed

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.

C++ Part 3 - Practical


4

Example 1:
#include <iostream>
using namespace std;

int main()
{
int age = 15;
cout << "Welcome c++\n";

if (age < 18) // False


{
cout << " age is not greater than \n";
}
cout << "See You\n";
return 0;
}
Example 2:
#include <iostream>
using namespace std;

int main()
{
int age = 20;
cout << "Welcome\n";

if (age < 18) // False


{
cout << " age is greater than \n";
}
cout << "See You\n";
return 0;
}

The else if Statement


Use the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1)
{
// block of code to be executed if condition1 is true
}
else if (condition2)
{
// block of code to be executed if the condition1 is false
and condition2 is true
}

Asst.Prof. Dr Sinan Adnan Al-Shammari


5

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;
}

C++ Part 3 - Practical


6

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;
}

Asst.Prof. Dr Sinan Adnan Al-Shammari


7

The else Statement


Use the else statement to specify a block of code to be executed if the condition
is false.
Syntax
if (condition1)
{
// block of code to be executed if condition1 is true
}
else if (condition2)
{
// block of code to be executed if the condition1 is false
and condition2 is true
}
else
{
// block of code to be executed if the condition1 is false
and condition2 is false
}
Example 1:
#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";
}
else
{
cout << "Iam Sorry\n";
}
return 0;
}

C++ Part 3 - Practical


8

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;

if (age >= 18)


{
cout << "Welcome Your Age Is OK\n";
if (points >= 1000)
{
cout << "You Are VIP\n";
}
}

return 0;
}

Asst.Prof. Dr Sinan Adnan Al-Shammari


9

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";

cout << result;


return 0;
}

C++ Switch Statements


Use the switch statement to select one of many code blocks to be executed.
Syntax
switch (expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

C++ Part 3 - Practical


10

This is how it works:

• The switch expression is evaluated once


• The value of the expression is compared with the values of each case
• If there is a match, the associated block of code is executed
• The break and default keywords are optional, and will be described later
in this chapter

The break Keyword


When C++ reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no
need for more testing.
Example 1:
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Choose A Day From 1 To 25\n";
cin >> day;

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()
{

Asst.Prof. Dr Sinan Adnan Al-Shammari


11

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;
}

C++ Part 3 - Practical


12

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;
}

Asst.Prof. Dr Sinan Adnan Al-Shammari

You might also like