Professional Documents
Culture Documents
Mathematics & Computer Programming: Toqeer Mahmood
Mathematics & Computer Programming: Toqeer Mahmood
Programming
Toqeer Mahmood
Dept. of Civil Engg.
UET, Taxila
Lecture
#
05
“Switch” Statement
“Switch statement” is used as a substitute
of “nested if-else”
In “Switch statement” only one condition
is given.
Multiple choices are given inside the
main body as “Cases” in the “switch
statement”
It evaluates an expression and returns a
value.
“Switch” Statement
“switch statement” is executed depending
upon the value returned by the expression.
The returned value is compared with the
constant values given in the cases.
If the returned value matches the case
value, the statements under that “case” are
executed.
“Switch” Statement
Syntax:
switch(expression)
{
case const-1:
statement/s;
break;
case const-2:
statement/s;
break;
………………..
………………..
case const-n:
statement/s;
break;
default:
statement/s;
}
“Switch” Statement
where
◦ const-1, const-2 etc. are numeric constants or
character constants.
◦ Keyword “default” is also used in the
statement.
◦ If any of the case does not match then the
statement/s under the default executes.
“Break” Statement
The “break” statement is used to exit
from the body of the switch structure or
from the loop structure.
It is used after the end of statement in the
case.
If it is not used then the statements of
other cases that come after the matching
case will also be executed.
Sample program (Even/Odd)
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
clrscr()
cout<<“Enter any number = ”;
cin>>n;
switch (n%2)
{
case 0:
cout<<“The input number is EVEN”;
break;
case 1:
cout<<“The input number is ODD”;
break;
}
cout<<“End of the program”<<endl;
getch();
}
Logical Operators
Logical operators are used to combine
relational expression or relational conditions.
The expression containing logical operators
is called logical expression or logical
condition.
It is also called compound condition or
compound expression.
The output of the logical expression is either
TRUE or FALSE.
Logical Operators
&& AND operator
|| OR operator
! NOT operator
AND operator
The && (AND) operator is used to
combine two or more relational
expressions.
If all expressions are true then the 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.
AND operator (Program)
Write a program to find out the largest number from three input numbers.
#include <iostream.h>
#inlcude <conio.h>
void main()
{
int a, b, c;
cout<<“Enter Ist value =”; cin>>a;
cout<<“Enter 2nd value =”; cin>>b;
cout<<“Enter 3rd value =”; cin>>c;
if ((a>b) && (a>c))
cout << “Ist value is greater”;
else if ((b>a) && (b>c))
cout << “2nd value is greater”;
else
cout <<“3rd value is greater”;
getch();
}
OR operator
The || (OR) operator is used to combine more than one
relational expression.
If any one of the given relational expressions is true,
the output will be true other wise the output will false.
e.g.
if ((age>60) || (job_length>30))
cout << “Retire from the job”;
else
cout<<“Continue the job”;
! (Not) Operator
It inverts the value returned by the
relational expression or the compound
expression.
e.g.
If x=5 and y=10 then the logical
expression ! (x>y) returns TRUE.
Because (x>y) returns false and the “!”
(NOT) operator inverts the result into
TRUE.
! (Not) Operator (Program)
Write a program to find out the greater value from two input values.
#include <iostream.h>
#inlcude <conio.h>
void main()
{
int a, b;
cout<<“Enter Ist value =”; cin>>a;
cout<<“Enter 2nd value =”; cin>>b;
! (a<b) ? cout << “Ist value is greater” : cout << “2nd value is greater”;
getch();
}
“goto” statement
The “goto” statement is an unconditional
control transfer statement.
It is used to transfer the control to a specified
label in the same program without evaluating
any condition.
Syntax:
goto label;
where;
“Label” represents that label in the program to
which the control is to be transferred.
“goto” statement
Write a program to print first ten natural numbers. Use “goto” and
“if” statement.
#include <iostream.h>
#include <conio.h>
void main()
{
int c = 1;
abc:
cout << c <<endl;
c++;
if (c<=10) goto abc;
}
Lab work
Write a program to input 2 integers to perform arithmetic operation by using
switch statement.
*****************************
* 1. Addition *
* 2. Subtraction *
* 3. Multiplication *
* 4. Division *
* 5. Remainder *
*****************************
Addition of the number = 10 (say….)
Write a program to input a number and generate table with that input number
(using goto statement)
Output as 2 x 1 = 2
2x2=4
………..
2 x 10 = 20
Lab work
Write a program to input a single
character and print a message “it is
vowel” if the input is vowel otherwise
print message “it is not a vowel”.
Write a program to input a single digit
from 0 to 9 and print the input value in
words. E.g. if the input value is 0 then
print “ZERO” etc.