1 / 21

PROGRAMMING LANGUAGE

PROGRAMMING LANGUAGE. Lecture #8 SWITCH STATEMENT. By Shahid Naseem (Lecturer). LECTURE OUTLINES. LECTURE OUTLINES. The “SWITCH” Statement The “BREAK” Statement Difference between “nested if-else” & “Switch” Statements. The “ goto ” Statement . Flow Chart.

Download Presentation

PROGRAMMING LANGUAGE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PROGRAMMING LANGUAGE Lecture #8 SWITCH STATEMENT By ShahidNaseem (Lecturer)

  2. LECTURE OUTLINES

  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)

  4. THE “NESTED-IF-ELSE” STATEMENT • 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; Control Structure (Civil Engineering Department)

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

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

  7. THE “NESTED-IF-ELSE” STATEMENT 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”; } Control Structure (Civil Engineering Department)

  8. THE “SWITCH” STATEMENT • 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. Control Structure (Civil Engineering Department)

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

  10. THE “SWITCH” STATEMENT • 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<iostream.h> Void main () { int n; cout<<“enter any value”<<endl; cin>>n; Switch(n%2) Control Structure (Civil Engineering Department)

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

  12. THE “BREAK” STATEMENT • 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. Control Structure (Civil Engineering Department)

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

  14. DIFFERENCE B/W “NESTED-IF-ELSE” AND “SWITCH” STATEMENTS Control Structure (Civil Engineering Department)

  15. THE “GOTO” STATEMENT • 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: goto label; Control Structure (Civil Engineering Department)

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

  17. FLOWCHART • 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. Control Structure (Civil Engineering Department)

  18. SYMBOLS OF FLOWCHART • 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. START EN D Control Structure (Civil Engineering Department)

  19. SYMBOLS OF FLOWCHART • 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. INPUT/OUTPUT Control Structure (Civil Engineering Department)

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

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

More Related