0% found this document useful (0 votes)
26 views24 pages

Lecture # 3

This document discusses conditional statements in C++, including: 1. Boolean expressions that evaluate to true or false using relational and logical operators. 2. The && and || operators that combine Boolean expressions. 3. Comparison operators and precedence rules. 4. Simple and compound if, if-else, and multiway if-else statements that control program flow based on conditions. 5. Common mistakes like using = instead of == and the importance of braces with if-else statements.

Uploaded by

Amaim Fatima
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)
26 views24 pages

Lecture # 3

This document discusses conditional statements in C++, including: 1. Boolean expressions that evaluate to true or false using relational and logical operators. 2. The && and || operators that combine Boolean expressions. 3. Comparison operators and precedence rules. 4. Simple and compound if, if-else, and multiway if-else statements that control program flow based on conditions. 5. Common mistakes like using = instead of == and the importance of braces with if-else statements.

Uploaded by

Amaim Fatima
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/ 24

Conditional

statements

Lecture # 3
Course Instructor:
Dr. Afshan Jamil
Outline
Boolean The && The ||
Truth tables
expression operator operator

Simple
Comparison Precedence Flow of
branching
operators rules control
mechanism

Compound Multiway if-


Simple if If-else
statement else

Common
Nested if
mistake to
else Examples
avoid
Boolean expression

• A Boolean expression is an expression that can be


thought of as being true or false.
• A Boolean expression can be evaluated in the same
way that an arithmetic expression is evaluated.
• Boolean expression uses relational operations such
as == , >, and < and Boolean/logical operations
such as &&, ||, and ! to produce one of the two
values true and false as the result.
CONTD…
• C++ sometimes uses integers as if
they were Boolean values.
• The compiler will treat any nonzero
number as the value true and will
treat 0 as the value false.
• Examples:
• (x==y)
• !( ( y < 3) ||(y > 7) )
The && operator

• You can form a more elaborate Boolean


expression by combining two simple tests
using the “and” operator &&.
• Syntax (For a Boolean Expression Using &&)
– (Comparison_1) && (Comparison_2)
• Example
– (score>0) && (score<10)
– (marks>==90) && (marks<=100)
The || operator

• You can form a more elaborate Boolean


expression by combining two simple tests
using the “or” operator ||.
• Syntax (For a Boolean Expression Using ||)
– (Comparison_1) || (Comparison_2)
• Example
– (x == 1) || (x == 5)
– (count>10) ||(count<50)
Truth tables
Comparison/Relational
operators
Precedence rules
• Binary operations of equal precedence are evaluated
in left-to-right order.
• Unary operations of equal precedence are evaluated
in right-to-left order
Flow of control

• The order in which the statements in your program


are performed is called flow of control.
• There are two ways to specify flow of control, using
• Branching mechanism
that lets your program choose between two
alternative actions, choosing one or the other
depending on the values of variables.
• Looping mechanism
that lets your program repeat an action several
times.
Simple branching mechanism

– If it is, do the following:


– Statement 1
– If it is not, do the following:
– Statement 2
• There is a C++ statement that does exactly
this kind of branching action. The if-else
statement chooses between two alternative
actions.
Compound statement
• A list of statements enclosed in a pair of braces is
called a compound statement.
• A compound statement is treated as a single
statement by C++ and may be used anywhere that a
single statement may be used.
• If more statements are desired for a branch, the
statements must be enclosed in braces to convert
them to one compound statement.
• If two or more statements not enclosed by braces
are placed between the if and the else, then the
compiler will give an error message.
Simple if statement

1.if(condition)
2. {
3. //code to be executed

4. }
Example
1. #include <iostream>
2.using namespace std;
3.
4.int main ()
5. {
6. int num = 10;
7. if (num % 2 == 0)
8. {
9. cout<<"It is even number";

10. }
11. return 0;
12.}
If-else statement
1. if(condition)
2. {
3. //code if condition is true
4. }
5. Else
6. {
7. //code if condition is false
8. }
Example
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num = 11;
5. if (num % 2 == 0)
6. {
7. cout<<"It is even number";
8. }
9. else
10. {
11. cout<<"It is odd number";
12. }
13. return 0;
14.}
Multiway if-else
1. if(condition1)
2. {
3. //code to be executed if condition1 is true
4. }else if(condition2)
5. {
6. //code to be executed if condition2 is true
7. }
8. else if(condition3)
9. {
10.//code to be executed if condition3 is true
11.}
12....
13.else{
14.//code to be executed if all the conditions are false
15.}
Example
#include <iostream> else if (num >= 70 && num < 80)
using namespace std; {
int main () { cout<<"B Grade";
int num; }
cout<<"Enter a number to check gra else if (num >= 80 && num < 90)
de:"; {
cin>>num; cout<<"A Grade";
if(num >= 0 && num < 50) }
{ else if (num >= 90 && num <= 100)
cout<<"Fail"; {
} cout<<"A+ Grade";
else if (num >= 50 && num < 60) }
{ else
cout<<"D Grade"; {
} cout<<“wrong number";
else if (num >= 60 && num < 70) }
}
{
cout<<"C Grade";
}
Common mistake to avoid

• One common mistake is to use the


symbol = when you mean ==
• This error is not caught by many
compilers.
• Example:
– if(x=12)
– cout<<“True”;
– else
– cout<<“false”;
Nested if-else statements
• An if-else statement within an if statement
Example
#include<iostream>
using namespace std;
int main()
{
int n ;
cout<<"Enter any integer number: " ;
cin>>n ;
if ( n < 100 )
{
cout<<“Given number is below 100\n";
if( n%2 == 0)
cout<<“And it is EVEN" ;
else
cout<<"And it is ODD" ;
}
else
cout<<“Given number is not below 100" ;
}
return 0;
Example to illustrates the importance of
using braces in if-else statements.
• #include<iostream>
• using namespace std;
• int main()
• {
• int marks_sub1, marks_sub2;
• cout<<"Enter marks for two subjects:";
• cin>>marks_sub1 >> marks_sub2;
• if (marks_sub1 > 40)
• if (marks_sub2 > 60)
• cout<<"You can enter the competition";
• else //this else belongs to which if
statement
• cout<<"You can exit";
• }
Class Task

• Write a program which takes character as input and


tell whether it is vowel or consonant.
THE END

You might also like