Lecture 4
Lecture 4
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.
Condition
Set of statements
Statements after
If- structure
#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”.
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
#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
}
FALSE TRUE
Condition-1
FALSE TRUE
Condition-2
Execute Execute
Statements-3 Statements-2
Next Statements
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.
{
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.
2) || OR operator
3) ! NOT operator
AND OPERATOR
The && (AND) operator is used to combine two or more
relational expression.