0% found this document useful (0 votes)
10 views21 pages

Lecture 4 Boolean and Conditions in C++

The document covers the basics of Booleans and conditional statements in C++. It explains the boolean data type, boolean expressions, and the use of if, else, and else if statements for controlling the flow of a program based on conditions. Additionally, it discusses nested if statements to handle complex conditional logic.

Uploaded by

mazen45619
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)
10 views21 pages

Lecture 4 Boolean and Conditions in C++

The document covers the basics of Booleans and conditional statements in C++. It explains the boolean data type, boolean expressions, and the use of if, else, and else if statements for controlling the flow of a program based on conditions. Additionally, it discusses nested if statements to handle complex conditional logic.

Uploaded by

mazen45619
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/ 21

1

Computer Programming
CHAPTER 2
BOOLEANS AND CONDITIONS
2
Booleans

 In programming, you will need a data type that can only have one of two
values, like:
 Yes/No
 On/Off
 True/False

C++ has bool data type, which can take a value Yes(1) or No(0)
3
Boolean Values

 Boolean variable is declared as bool and can only take two values True/False:
#include <iostream>
using namespace std;
int main() {

bool T = true;
bool F = false;
cout << T << endl; // Outputs 1 (true)
cout << F << endl; // Outputs 0 (false)
}
4
Boolean expression

 A Boolean expression returns a Boolean value that is either 1/0


 You can use the comparator operators such as greater than (>) or less than (<), to find out if the
expression is true or false:

#include <iostream>
using namespace std;
int main() {
int x = 10;
int y = 9;
cout << (x > y) << endl;
}
5
Boolean expression (cont.)

#include <iostream>
using namespace std;
int main() {
int x = 10;
int y = 9;
cout << (x == 9) << endl;
}
6
Conditions

 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
7
The if Statement

 Use the if statement to specify a block of C++ code to be executed if a


condition is true
if (condition) {
// block of code to be executed if the condition is true
}
Example:
if (20 > 18) { // this condition is always true
cout << "20 is greater than 18";
}
8
The if Statement (cont.)

#include <iostream>
using namespace std;
int main() {
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y“<< endl;
}
}
9
The if Statement (cont.)

#include <iostream> If the user enters value


using namespace std; greater than or equal
60
int main() {
int x;
cin >> x;
if (x >= 60) {
cout << “Passed“<< endl;
}
}
10
The if Statement (cont.)

#include <iostream> If the user enters value


using namespace std; less than or equal 60

int main() {
int x;
cin >> x;
if (x >= 60) {
cout << “Passed“<< endl;
}
}
11
The if Statement activity diagram
12
The else Statement

 Use the else statement to specify a block of code to be executed if


the condition is false

if (condition) {
// block of code to be executed if the condition is true
}
else {
// block of code to be executed if the condition is false
}
13
The else Statement (cont.)

#include <iostream>
using namespace std;
int main() {
int time = 20;
if (time < 18) {
cout << "Good day.";
}
else {
cout << "Good evening.";
}
}
14
The if Statement (cont.)

#include <iostream>
using namespace std; If the user enters value
Less than or equal 60
int main() {
int x;
cin >> x;
if (x >= 60) {
cout << “Passed“<< endl;
}
else {
cout << “Failed“<< endl; }
}
15
The if else Statement activity diagram
16
The else if Statement

 Use the else if statement to specify a new condition if the first condition is false
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
}
17
The else if Statement (cont.)

if (x >= 90)
{cout << "A"<< endl;}
else if (x >= 80)
{cout<<"B"<<endl;}
else if (x >= 70)
{cout<<"C"<<endl;}
else if (x >= 60)
{ cout<<"D"<<endl;}
else cout<<"Failed"<<endl;
18
The else if Statement (cont.)

if (x >= 90)
{cout << "A"<< endl;}
else if (x >= 80)
{cout<<"B"<<endl;}
else if (x >= 70)
{cout<<"C"<<endl;}
else if (x >= 60)
{ cout<<"D"<<endl;}
else cout<<"Failed"<<endl;
19
Nested if

int x=6,y=3; Braces indicate that the second if statement is


in the body of the first and the else is
if ( x > 5 ){
associated with the first if statement
if ( y > 5 )
cout << "x and y are > 5";}
else cout << "x is <= 5";
20
Nested if (cont.)

int x=6,y=3; Braces indicate that the second if statement is


in the body of the first and the else is
if ( x > 5 )
associated with the second if statement
{ if ( y > 5 )
cout << "x and y are > 5";
else cout << "x is <= 5";}
21
End

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy