Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)

Similar presentations


Presentation on theme: "Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)"— Presentation transcript:

1 Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)

2

3 LECTURE OUTLINES The “SWITCH” Statement The “BREAK” Statement Difference between “nested if-else” & “Switch” Statements. The “goto” Statement. Flow Chart Control Structure (Civil Engineering Department) 3

4 THE “NESTED-IF-ELSE” STATEMENT Control Structure (Civil Engineering Department) When an “if-else” structure is placed in another “if- else “structure, it is called “nested-if-else” structure. It is used for multiple selection. Syntax if (condition-1) statement-1; else if (condition-2) statement-2; else statement-3; 4

5 THE “NESTED-IF-ELSE” STATEMENT Control Structure (Civil Engineering Department) 5 Block-1 Statement after if- else structure TRUE FALSE CONDITION-1 Block-2 FALSE TRUE CONDITION-2

6 THE “NESTED-IF-ELSE” STATEMENT Control Structure (Civil Engineering Department) Write a program to perform simple arithmetic operation by using “nested –if-else” structure. #include Void main () { int a,b; char op; cout<<“enter first integer, operator & second integer/n ”; cout<<“press enter key”; cin>>a>>op>>b; 6

7 THE “NESTED-IF-ELSE” STATEMENT Control Structure (Civil Engineering Department) If (op==‘+’) cout<<“Addition=“<<(a+b); Elseif (op==‘-’) cout<<“Subtraction=“<<(a-b); Elseif (op==‘*’) cout<<“Multiplication=“<<(a*b); Else if (op=‘/’) cout<<“Division=“<<(a/b); Else if (op=‘%’) cout<<“Remainder=“<<(a%b); Else cout<<“Invalid input”; } 7

8 THE “SWITCH” STATEMENT Control Structure (Civil Engineering Department) The “Switch” statement, is used as a substitute of “Nested-if-else statements”. It is used when multiple choices are given and one choice is to be selected. The “nested-if-else” structure becomes complicated in multiple choices. The “Switch Statement” is used in such situations. Only one condition is given in the “switch statement” and multiple choices are given inside the main body. 8

9 THE “SWITCH” STATEMENT Control Structure (Civil Engineering Department) Syntax switch (expression) { case const-1: statements; break; case const-2: statements; break; default: statement;} 9

10 THE “SWITCH” STATEMENT Control Structure (Civil Engineering Department) Write a program to input an integer value. Test the integer value if the value is divisible by 2, then print the message “Divisible by 2” otherwise “Not divisible by 2” by using switch statement. #include Void main () { int n; cout<<“enter any value”<<endl; cin>>n; Switch(n%2) 10

11 THE “SWITCH” STATEMENT Control Structure (Civil Engineering Department) { Case o: cout<<“Divisible by 2”<<endl; Break; case 1: cout<<“Not divisible by 2”<<endl; Break; } Cout<<“ok”<<endl; } 11

12 THE “BREAK” STATEMENT Control Structure (Civil Engineering Department) The “BREAK” statement is used to exit from the body of the switch structure. In the switch statement, the break statement is normally used at the end of statements in each case. It exits the control from the body of switch structure. If it is not used then the statements of other cases that come after the matching case will also be exectued. 12

13 ASSIGNMENT #4 Control Structure (Civil Engineering Department) Write a program to perform simple arithmetic operation by using SWITCH STATEMENT 13

14 DIFFERENCE B/W “NESTED-IF-ELSE” AND “SWITCH” STATEMENTS Control Structure (Civil Engineering Department) 14 NESTED IF-ELSE STATEMENTSWITCH STATEMENT i.It becomes complicated for multiple selections. It is easy to understand for multiple selections. ii.It uses an independent expression for each case. It uses a single expression for all cases, but each case must have a constant value of integer type or character type. iii.The test condition can be given in a special range of value. If the given condition matches then the statements under it will be executed. Only a single expression is given in the switch statement which returns a single value. The test condition cannot be given in a specified range. It is drawback.

15 THE “GOTO” STATEMENT Control Structure (Civil Engineering Department) The “goto” statement is an unconditional control transfer statement. It is used to transfer the control to a specified label in the name program without evaluating any condition. Syntax: gotolabel; 15

16 THE “GOTO” STATEMENT Control Structure (Civil Engineering Department) #include cout<<“ok”; Void main ()} { cout<<“this program explains the”; cout<<“ use of goto statemen”<<endl; Gotoabc; cout<<“programming in C++\n”; cout<<“it is an object oriented”; cout<<“programming language”; Abc: cout<<“program is terminated\n”; 16

17 FLOWCHART Control Structure (Civil Engineering Department) Flowchart is the graphical representation of an algorithm. It is a way of representing the flow of data, the operations performed on the data and the sequence in which the operations are performed on the data. Flowchart is similar to the map of the building. A computer programmer creates a flowchart of a problem before writing the actual computer program. 17

18 SYMBOLS OF FLOWCHART Control Structure (Civil Engineering Department) Flow Lines: The line with an arrow head represents the direction of flow of information or operation between various blocks in the flowchart. Start/End An oval shape symbol is used to represent Start as well as end of a flowchart. 18 STARTEN D

19 SYMBOLS OF FLOWCHART Control Structure (Civil Engineering Department) Input/Output: The INPUT and OUTPUT are represented in the flowchart by parallelogram symbol. Processing The processing step in the flowchart is represented by a rectangular block. 19 INPUT/OUTPUT

20 SYMBOLS OF FLOWCHART Control Structure (Civil Engineering Department) Decision: A diamond symbol is used in the flowchart for representing decision or selection. 20 True False

21 FLOWCHART DRAWN Control Structure (Civil Engineering Department) A flowchart to test if number A is greater than B. 21 START INPUT A INPUT B Print A Print B STOP IF A>B True False


Download ppt "Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)"

Similar presentations


Ads by Google