0% found this document useful (0 votes)
12 views

Lecture 4

Uploaded by

fakhrialamqazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture 4

Uploaded by

fakhrialamqazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

N T

M E
T E
TA
L S
N A
T I O
D I
O N LECTURE # 4
C
 A statement that is executed in a C++ program using
conditions are called Conditional statements.
 The statements of a computer program are executed one
after other in the order in which they are written are
called sequential execution of the program.

 Conditional statements are used to change the execution


order of statements in a program.

 Conditional Statements are used to execute (or ignore)


set of statements after testing the condition.

Department of Computer Science, Awkum


RELATIONAL EXPRESSION
 A relational expression consist of constants, variables
that are combined by a relational operator.

 A relational expression is written to find the relation


between two expressions. It returns only one value,
which is either true or false.

 10>6 relational expression, indicates the relation


between two constants. Expression will return true
value.

 ‘>’ is the relational operator between two values.


IF STATEMENT
 The “IF statement” is used to execute (or ignore) a set
of statements after testing a condition.

 “IF statement” evaluate a condition. If the given


condition is true, the statements(or a set of statements)
followed the “IF statement” is executed.

 If the given condition is false , the statement (or a set


of statement) following the “IF statement” condition is
ignored and the control transfer to the next statement.
SYNTAX OF IF-STATEMENT
If (condition)
Statement-1;
Statement-2;

 Condition specifies the condition or a


relational expression. If the condition is
true the statements following the “If-
statement” is executed. Otherwise the
statement followed in “If- statement” is
ignored.
If (condition)
{
Statement-1;
Statement-2;
}
Statement-3;

Condition

Set of statements

Statements after
If- structure

Flow chart: The “If statement”


PROGRAM
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
Void main() Void main()
{ {
Int a, b; Int n;
a=100;
Cout<<“Enter value for n”;
b=50;
If (a>b)
Cin>>n;
{ If(n>10)
Cout<<“islamabad”; {
Cout<<“islamabad”;
}
}
Cout<<“OK”;
Cout<<“OK”;
getch(); getch();
} }
PROGRAM
Write a program to input number. If the number is divisible by 3 then print
the output ‘the number is divisible by three’.

#include<iostream.h>
#include<conio.h>
Void main()
{
Int n;
Cout<<“Enter a number ?”;
Cin>>n;
If(n%3==0)
{
Cout<<“The number”<<n<<“is divisible by 3”;
}
getch();
}
Write a program to input two integer value and find out whether the first or
the second number is greater.

#include<iostream.h>
#include<conio.h>
Void main()
{
Int a,b;
Cout<<“Enter First value”;
Cin>>a;
Cout<<“Enter second value”;
Cin>>b;
If (a>b)
Cout<<“first value is greater”;
If(b>a)
Cout<<“Second Value is greater”;
getch();
“IF-ELSE” STATEMENT
 Another form of “IF- statement”.

 In “IF-ELSE” statement, one condition and two blocks


of statements are given. Either one of the two blocks of
statements is executed after evaluating a condition.

 The “IF-ELSE” statement test the given relational


condition. If the condition is true then the first block
of statement is executed. If the condition is false, the
first block of statement is ignored and the second block
following the else is executed.
SYNTAX OF IF-ELSE
STATEMENT
If (condition)
{
Statement-1;
Else
Statement-2;
}
If (condition)
{
Statement-1;
Else
Statement-2;
}
SYNTAX OF IF-ELSE STATEMENT
Syntex-1 Syntex-2

If (condition) If (condition)
{ {
Statement-1;
Statement-1;
Statement-2; first block
Else
Statement-m;
Statement-2; }
} Else
{
Statement-1;
Statement-2; Second block
Statement-n;
}
FALSE TRUE
Condition

Block-2
Block-1

Statements after
If- structure

Flow chart: The “IF–Else statement”


PROGRAM
Write a program to a number from the keyboard. Use if-else statement to
find out whether the number is less than or greater than 100.

#include<iostream.h>
#include<conio.h>
Void main()
{
Int n;
Cout<<“Enter value”;
Cin>>n;
If (n>100)
Cout<<“ Number is greater than 100”;
else
Cout<<“ Number is less than 100”;
getch();
}
Assignment

Write a program to input two values from the


keyboard. Use if-else statement to print
largest number.
NESTED IF STATEMENT
 When an “If statement” is used within another “if-
statement”, it is called “Nested if statement”.

 Nested if statement is used multi way decision –making.

SYNTAX OF “NESTED IF”


 If (Condition-1)
{
If (Condition-2)
{
Statement- 2;
}
Statement- 3;

}
FALSE TRUE
Condition-1

FALSE TRUE
Condition-2

Execute Execute
Statements-3 Statements-2

Next Statements

Flow chart: The “Nested -IF statement”


 In the given syntax, if the condition-1 is true then the
control shifts to next “if-statement” and condition-2 is
tested.

 If the condition-2 is true then statement-2 is executed.


The control will than pass to statement-3 and this
statement will be executed.

 If condition-2 is false, then statement-2 is skipped and the


control shifts to statement-3 and it will be executed.

 The “if-statement” for testing condition-2 is within


another “if-statement”. This “if-statement” is called the
“nested-if statement”.
PROGRAM
 Write a program to input three integer values. Compare
the three values to find out if they are equal.

 Use “nested if statement” and print the output “All


values are equal” if condition is true. Otherwise print
“values are different”.
#include<iostream.h>
#include<conio.h>
Void main()
{
Int a, b, c;
Cout<<“Enter first value ?”;
Cin>>a;
Cout<<“Enter second value ?”;
Cin>>b;
Cout<<“Enter third value ?”;
Cin>>c;
If(a= =b)
{
If(a= =c)
cout<<“All values are equal ”;
}
else
cout<<“ these All values are different ”;
getch ();
}
Assignment
write a program to find out the grade of the student
based on the obtained marks of three subject.
A-grade
B-grade
C-grade
fail
THE “NESTED IF-ELSE” STRUCTURE
 When an “if else ” structure is placed in another “if-else”
structure, it is called “nested if-else ” structure.
Syntax of Nested IF-else

If(condition-1)
Statement-1;
else if(condition-2)
Statement-2;
else if(condition-3)
Statement-3;
else if(condition-m)
Statement-m;
else
statement-n;
#include<iostream.h>
#include<conio.h>
Void main()
{
Int a, b; Char op;
Cout<<“Enter 1st value”;
Cin>>a;
Cout<<“Enter 2nd value”;
Cin>>b;
Cout<<“Enter operator”;
Cin>>op;
If(op = =‘+’);
Cout<<“Addition = ”<<a+b;
Else If(op = =‘-’);
Cout<<“Subtraction= ”<<a-b;
Else If(op = =‘/’);
Cout<<“Division= ”<<a/b;
Else If(op = =‘%’);
Cout<<“Remainder= ”<<a%b;
else
Cout<<“invalid input”;
Getch();
THE “SWITCH” STATEMENT
 The “switch statement ” is used as a substitute of
“Nested-If statement” it is used when multiple choices
are given and one choice is to be selected.
 The nested if-else structure become complicated in
multiple choices therefore Switch statements are used in
such conditions.
 Only one condition is given in “Switch statement” and
multiple choices are given inside the main body as cases.
 The “switch statement” evaluates an expression and
return a value.
 One of the choice or case in switch statement is executed
depending upon condition.
 If a choice is true then the statement inside that choice
will be executed.
SYNTAX OF SWITCH
Switch(expression)
{
Case const-1:
Statements;
break;
Case const-2:
Statements;
break;
Case const-n:
Statements;
break;
default:
Statements;
The const-1,const-2 are the numeric constants or character
constants.
In the given syntax of switch statement, the given
expression is first executed and then the value returned
by the expression is compared with the values of
constants given in each case.

The keyword default is used in the body of switch


statement. If no case is matched then the statement under
the default are executed. it is optional
#include<iostream.h> Case ‘-’:
#include<conio.h> Cout<<“Subtraction= ”<<a-b;
Break;
Void main()
Case ‘*’:
{ Cout<<“Multiplication= ”<<a*b;
Int a, b; Char op; Break;
Cout<<“Enter 1st value”;
Cin>>a; Case ‘/’:
Cout<<“Division= ”<<a/b;
Cout<<“Enter 2nd value”;
Break;
Cin>>b;
Cout<<“Enter operator”; Case ‘%’:
Cin>>op; Cout<<“Remainder= ”<<a%b;
Switch(op) Break;

{
Default:
Case ‘+’: Cout<<“invalid input”;
Cout<<“Addition= ”<<a+b; }
break; Getch();
}
#include<iostream.h>
#include<conio.h>
Void main()
{
Int n;
Cout<<“Enter any value”;
Cin>>n;
Switch(n%2)
{
Case 0:
Cout<<“divisible by 2”;
break;
Case 1:
Cout<<“Not divisible by 2”;
break;
}
getch();
}
BREAK STATEMENT
The break statement is used to exit from the body of the
switch structure.

In switch statement the break is used to at the end of each


case.

In switch statement if any of the case is matched, that


statement in that specific case will be printed and break
statement will stop the control in the main body of
switch statement.
NESTED IF-ELSE & SWITCH STATEMENT

Nested If-else statement Switch Statement

1. It becomes complicated for 1. It is easy to understand for


multiple selection multiple selections.
2. It uses independent expression for 2. It uses a single expression for all
each case cases, but each case must have a
constant value for integer type or
character type.
3. The test condition can be given in 3. Only one expression is given in
specified range of values the switch statement. Which return a
If the given condition matches then single value .
the statement under it will be The test condition cannot be given a
executed. specified range. It is the major
drawback of the switch statement.
LOGICAL OPERATOR
 Logical operators are usually used in conditional
Statements

1) &&  AND operator

2) ||  OR operator

3) !  NOT operator
AND OPERATOR
 The && (AND) operator is used to combine two or more
relational expression.

 If all relational expression are true then output returned by


the compound expression is true.

 If any one of the relational expression in the compound


expression is false, the output is false.

 Example, if X=10,y=15,Z=5 then the compound expression


(x<y) && (z = = 5) return true because both the expression
(x<y) and (z = =5) are true. Similarly, the compound
expression (x>y) && (z= =5) and (x<y) && (z>x) will
return false values.
#include<iostream.h> Cout<<“1stvalue is
#include<conio.h> greater”;
Void main() Else
{ If ( (b>a) &&(b>c) )
Int a, b,c; Cout<<“2nd value is
Cout<<“Enter 1st value”; greater”;
Cin>>a; Else
Cout<<“Enter 2 value”;
nd Cout<<“3 rd
value is
greater”;
Cin>>b;
getch();
Cout<<“Enter 3 value”;
rd
}
Cin>>c;
If((a>b) && (a>c))
THE (OR) OPERATOR
 The || (OR) operator is used to combine more than one
relational expression.

 If any one of the given expression is true, the output will


be true otherwise the output will be false.

 For example if x=0,y=15,z=5 then the compund


expression (x>y) || (z= = 5) return true because (z= =5)
is true.

 Similarly,(x!=y) || (x = = y) || ( z>10 ) will return true


because (x!=y) is true.

 If all the relational expression in the compund condition


are false then the output will be false.
#include<iostream. h>
#include<conio. h>
Void main()
{
Int a;
Char op;
Cout<<“enter an integer value”;
Cin>>a;
Cout<<“enter any operator”;
Cin>>op;
If((a<0)||(op = =‘y’)||(op = =‘ n’))
Cout<<“ok, condition is true”;
Else
Cout<<“All relational expressions are false”;
getch();
}
NOT OPERATOR
 Not operator is used to reverse the meaning of an
expression.

 It represented by the sign (!).


X !X
False True
True False

 The sign(!) is a prefix operator it is always used in the


front of a condition.

 The condition must enclosed in parenthesis ().


PROGRAM USING NOT OPERATOR

 Write a program to take a number from the


keyboard that is not equal to 3 ?

You might also like