Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Similar documents
Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Structures in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Presented By : Gaurav Juneja

Computers Programming Course 7. Iulian Năstac

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Laboratory 4. INSTRUCTIONS (part II) I. THEORETICAL BACKGROUND

C Programming Lecture V

DB Basic Concepts. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Flow Control. CSC215 Lecture

Unit 3 Decision making, Looping and Arrays

COMP 208 Computers in Engineering

Solution for Data Structure

Computer Programming Unit 3

A control structure refers to the way in which the Programmer specifies the order of executing the statements

DBMS and its Architecture

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

SINGLE LINKED LIST. Algorithm for allocating memory for the new node

Introduction. C provides two styles of flow control:

Subject: PIC Chapter 2.

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Problem # 1. calculate the grade. Allow a student the next grade if he/she needs only 0.5 marks to obtain the next grade. Use else-if construction.

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

UIC. C Programming Primer. Bharathidasan University

Selection Statements. Pseudocode

SELECTION STATEMENTS:

DECISION MAKING STATEMENTS

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Data Types and Variables in C language

UNIT 3 FUNCTIONS AND ARRAYS

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

DECISION CONTROL AND LOOPING STATEMENTS

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

What we have learned so far

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Computers Programming Course 12. Iulian Năstac

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Software Design & Programming I

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Creating a C++ Program

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Introduction to C Programming. What is a C program?

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Dynamic Memory Allocation

Procedures, Parameters, Values and Variables. Steven R. Bagley

The following expression causes a divide by zero error:

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

CHAPTER 9 FLOW OF CONTROL

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Decision Making and Branching

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Text Input and Conditionals

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Subject: Fundamental of Computer Programming 2068

Control Statements. If Statement if statement tests a particular condition

Programming Fundamentals

DEPARTMENT OF ELECTRICAL AND ELECTRONIC ENGINEERING PROGRAMMING TECHNIQUES I EE271

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Part II IT Infrastructure (Computer Software)

In this lab, you will learn more about selection statements. You will get familiar to

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Department of Computer Applications

Relational Operators EXAMPLE. C++ By

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

int marks[10]; // fixed size and fixed address No change in Memory address.

PROGRAMMAZIONE I A.A. 2017/2018

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Computer Programming & Problem Solving ( CPPS )

Technical Questions. Q 1) What are the key features in C programming language?

Programming for Electrical and Computer Engineers. Loops

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

QUIZ. What is wrong with this code that uses default arguments?

BASIC ELEMENTS OF A COMPUTER PROGRAM

ECET 264 C Programming Language with Applications. C Program Control

Language comparison. C has pointers. Java has references. C++ has pointers and references

printf( Please enter another number: ); scanf( %d, &num2);

A Fast Review of C Essentials Part II

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

First of all, it is a variable, just like other variables you studied

Relational Operators and if. Class 10

Transcription:

Case Control Structure DCS COMSATS Institute of Information Technology Rab Nawaz Jadoon Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP)

Decision control structure In real life we are often faced with situations where we are required to make a choice between a number of alternatives rather than only one or two. For example, Which school to join??? or Which hotel to visit??? or Which girl/boy to marry (you almost always end up making a wrong decisions a different matter altogether!). 2

C provides a special control statement that allows us to handle such cases effectively; rather than using a series of if statements. Switch Switch 3

Decisions Using switch The control statement that allows us to make a decision from the number of choices is called a switch. Or more correctly a switch case-default, since these three keywords go together to make up the control statement. 4

Switch Expression can be an integer constant (1,2,3 or a character constant (x,y,z etc). The keyword case is followed by an integer or a character constant. Each constant in each case must be different from all the others. The do this lines in the above form of switch represent any valid C statement. 5

Switch Execution First, the integer expression following the keyword switch is evaluated. The value it gives is then matched, one by one, against the constant values that follow the case statements. When a match is found, the program executes the statements following that case, and all subsequent case and default statements as well. If no match is found with any of the case statements, only the statements following the default are executed. 6

Example The output of this program Would be!!! I am in case 2 I am in case 3 I am in default The output is definitely not what we expected! We don t want the cases except case 2. 7

Switch with Break statement If the concerned case statement executing then we use Break statement. Note that there is no need for a break statement after the default, since the control comes out of the switch anyway. 8

Flow Chart of Switch with Break 9

Switch cases The output of this program Would be!!! I am in case 2 10

11

Example The output of this program Would be!!! I am in case x Note: In fact here when we use v, a, x they are actually replaced by the ASCII values (118, 97, 120) of these character constants. 12

common set of statements for multiple cases. If an alphabet a is entered the case a is satisfied and since there are no statements to be executed in this case the control automatically reaches the next case i.e. case A and executes all the statements in this case. 13

Rules If there are multiple statements to be executed in each case there is no need to enclose them within a pair of braces (unlike if, and else). Every statement in a switch must belong to some case or the other. If a statement doesn t belong to any case the compiler won t report an error. However, the statement would never get executed. For example, in the following program the printf( )never goes to work. 14

Example Printf() within switch will never be executed!!! 15

Rules If we have no default case, then the program simply falls through the entire switch and continues with the next instruction (if any,) that follows the closing brace of switch. A float switch expression is not allowed. The advantage of switch over if is that it leads to a more structured program and the level of indentation is manageable, 16

Rules We can check the value of any expression in a switch. Thus the following switch statements are legal. switch ( i + j * k ) switch ( 23 + 45 % 4 * k ) switch ( a < 4 && b > 7 ) Expressions can also be used in cases provided they are constant expressions. Thus case 3 + 7is correct, however, case a + b is incorrect. In principle, a switch may occur within another, but in practice it is rarely done. 17

Can t do with a switch A float expression cannot be tested using a switch. Cases can never have variable expressions (for example it is wrong to say case a +3 :) Multiple cases cannot use same expressions. Thus the following switch is illegal: 18

Concept revision regarding switch The switch keyword is followed by an integer or an expression that evaluates to an integer. The case keyword is followed by an integer or a character constant. The control falls through all the cases unless the break statement is given. 19

The Goto Statement 20

Exit() The exit( int ) function is a standard library function which terminates the execution of the program. <stdlib.h> should be included into your program while using exit(integer) function; Mostly for exiting the control exit(0); is used. 21

Write a program using, Decision control structure Case control structure Goto statement Class Assignment Time: 5min That should produce the following output??? Enter choice (1 to 3) 1 I am in case 1 Do you want to execute again ( Y or N)???y Enter choice (1 to 3) 2 I am in case 2 Do you want to execute again ( Y or N)??? 22

Program using Switch and Goto Statement //Switch-case-default with Goto statement #include<stdio.h> #include<conio.h> #include<stdlib.h> main() { ss: int n; char ch; printf("\nenter choice (1 to 3) "); scanf("%d", &n); switch(n) { case 1: printf("\ni am in case 1\n"); printf("\ndo you want to execute again ( Y or N)???"); ch=getche(); if (ch=='y' ch=='y') goto ss; else exit(0); break; case 2: printf("\ni am in case 2\n"); printf("\ndo you want to execute again ( Y or N)???"); ch=getche(); if (ch=='y' ch=='y') goto ss; else exit(0); break; case 3: printf("\ni am in case 3\n"); exit(0); break; default: printf("\n You have entered a wrong value\n"); printf("\ndo you want to execute again ( Y or N)???"); ch=getche(); if (ch=='y' ch=='y') goto ss; else exit(0); break; } getche(); } 23

24