Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

More on Algorithms and Problem Solving
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
ITEC113 Algorithms and Programming Techniques
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Introduction to Computers and Programming Lecture 5 New York University.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
The University of Texas – Pan American
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
DECISIONS In the Name of Allah The Most Merciful The Most Compassionate DECISIONS
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
CPS120: Introduction to Computer Science Decision Making in Programs.
C Lecture Notes 1 Structured Program Development.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Flow Statements
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
CPS120: Introduction to Computer Science Decision Making in Programs.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Branching statements.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Decisions Chapter 4.
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programming Fundamentals
Control Structures.
Lecture 2: Logical Problems with Choices
Intro to Programming Week # 3 If-else Statement Lecture # 6
Structured Program
Control Structures: Selection Statement
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Structures: Selection Statement
Selection Control Structure
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Structural Program Development: If, If-Else
Presentation transcript:

Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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

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

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