Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

Programming for

Engineers - I

Lecture 4
Furqan Aziz
The Boolean data type
 Range : {true, false}
 Syntax
 bool var = true;
 bool var = false;

 Type Casting
 If you cast true to int, it is casted to 1.
 If you cast false to int, it is casted to 0.
 Numerical values can also be casted to boolean values.
 0 is casted to false.
 Any non-zero value is casted to true.
Choices and Decisions
 A computer can take logical decisions.
 Compare data values using some operators called
relational operators.
 Take two values and return either true or false.
 < less than
 > greater than
 <= less than or equal to
 >= greater than or equal to
 == equal to
 != not equal to
 Which operators are inverse of each other?
Relational Operators Examples
 Let I = 4 ,J = 7 and K = 2 Then
I < J will return true
 I < K will return false
 J > I will return true
 I <= i will return ……….
 I >= J will return ……….
 I = = J will return ……….
 I = = I will return ……….
 K ! = J will return ……….
 K ! = K will return ……….
 I ! = J will return ……….
 I = = J will return ……….
The if Statement
 The basic if statement
allows your program No Condition
is true?
to execute a single
statement, or a block
Yes
of statements enclosed
between braces, if a
given condition is true. Statement
or Block of
 Example Statements

int a = 0, b=1;
if (a < b)
cout<<“Hello”; Next
Statement
Making a decision
#include<isotream>
using namespace std;

int main()
{
int val;
cout<<“Enter a value between 50 and 100”;
cin>>val;
if(val < 50)
cout<<“Wrong value entered”;
if (val > 100)
cout<<“Wrong Value entered”;
cout<<“You have entered “<<val<<endl;
return 0;
}
Nested if statement
 You can include an if statement inside the body of
another if statement.
 Example
if ( x > 0)
{
if ( y < 0)
cout<< “ Hello “;
}
Nested if statement – An Example
 Write a simple program to test whether a character
entered from keyboard is uppercase letter or lowercase
letter.

Hint 
if (letter > ‘A’) is perfectly valid statement.
functions for testing characters
 isupper()
 islower()
 isalpha()
 isdigit()
 isxdigit()
 isalnum()
 isspace()
 iscntrl()
 isprint()
 isgrpah()
 ispunc()
=========
 tolower()
 toupper()
The if-else Statement
 The basic if-else statement
allows your program to
execute a single statement, No Condition
or a block of statements is true?
enclosed between braces,
if a given condition is
true, and another Yes
statement or block of
statements if the condition
is false. Statement Statement
or Block of or Block of
 Example Statements Statements
int a = 0, b=1; for false for true
if (a < b)
cout<<“ a is less than b”;
else
cout<<“a is greater than Next
or equal to b”; Statement
The if-else Statement - An Example
 Write a simple program to test whether a number
entered by a user is even or odd.

Hint 
if (num % 2 = = 0 )
Nested if-else statement
 You can an if-else statement inside an if-else statement.
 Dangling if-else problem?
Logical Operators
 Used for combining
conditions
 Logical AND - &&
 Logical OR - ||
 Logical Negation - !
op1 op2 AND OR op1 NOT
true true true true
true false
true false false true
false true false true false true
false false false false
Truth Table for Logical NOT
Truth Table for Logical And
and OR
Logical Operators - Example
 Int x = 4, y = 3, z = 7;
 (x>0) && (y<4) will return ……….
 (x<4 ) || (z < 8) will return ……….
 (y > 3 ) && !(z < 7) will return ……….
Conditional Operator
 Ternary operator
 Typically used to control output
 Examples
 Int x = 0, y = 5;
x = (y>=0) ? y : -y;
 int fruit = 10;
cout<<“You have “<<fruit<<(fruit = = 1 ? “fruit” : “fruits”);
The Switch Statement
 Enables you to select from multiple choices (called cases).
 Syntax
swithc(expression)
{
case val1 :
stat1;
stat2;
……
break;
case val2 :
stat1;
stat2;
……
break;
……………….
default:
stat1;
stat2;
……
}

You might also like