You are on page 1of 5

12/19/2022

Lecture-14
Corse Title: Programming Language
Course Code: EL-255
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2022
Offered to: S.E. (Electronic Engineering)

Department of Electronic Engineering


NED University of Engineering and Technology Karachi, Pakistan

Chapter-4

Conditions

1
12/19/2022

Conditional Statement
max=(num1 > num2) ? num1:num2;

Conditional Statement cont.


Write a program which takes 5 integers as input and
prints the largest
#include <iostream.h>
main()
{
int max=0,i, num;

for(i=1,i<=5,i++)
{
cout<< “\n enter integer”;
cin>> num;
max=(max > num) ? max:num;
}

cout<<“the largest number is ”<<max;


}
4

2
12/19/2022

Write a program which takes 5 integers as input and


prints the largest ( it should be able to handle both
negative and positive numbers)

Switch Case
In if-else / else-if ass the number of alternatives
increases the complexity of program increases
drastically.
• Switch statement is a multi-way decision
statement

3
12/19/2022

Switch Case
if(Condition 1) switch ( expression )
Statement 1 {
else case label1 :
{ body1
Statement 2 break;
if(condition 2)
{ case label2 :
if(condition 3) body2
statement 3 break;
else case label3 :
if(condition 4) body3
{ break;
statement 4 default :
} default-body
} break;
else }
{
statement 5 next-statement;
}
7
}

Rules of Using Switch Case


1. Case Label must be unique. switch ( expression )
2. Case Labels must ends with Colon. {
3. Case labels must have constants / constant expression. case label1 :
4. Case label must be of integral Type ( Integer, Character). body1
5. Case label should not be ‘floating point number ‘. break;
6. Switch case should have at most one default label.
case label2 :
7. Default label is Optional.
body2
8. Default can be placed anywhere in the switch.
break;
9. Break Statement takes control out of the switch.
case label3 :
10. Two or more cases may share one break statement. body3
11. Nesting ( switch within switch ) is allowed. break;
12. Relational Operators are not allowed in Switch default :
Statement. default-body
13. Macro Identifier are allowed as Switch Case. break;
14. Const Variable is allowed in switch Case Statement. }
15. Empty Switch case is allowed.
next-statement;

4
12/19/2022

THE END

You might also like