CS1061 C Programming Lecture 9: switch Statement and Variable Scope A. O’Riordan, 2004.

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

Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April if, if … else.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Control Flow C and Data Structures Baojian Hua
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
C++ for Engineers and Scientists Third Edition
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
UNIT II Decision Making And Branching Decision Making And Looping
Storage Classes.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Conditional Statement
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 05 (Part III) Control Statements: Part II.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
CNG 140 C Programming (Lecture set 3)
CSE 220 – C Programming Loops.
Fundamentals of PL/SQL part 2 (Basics)
Programmation impérative - Prof. Béat Hirsbrunner
Control Structures.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Switch Statements, Do While, Break, Continue, Goto, Comma Operator
A First Book of ANSI C Fourth Edition
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 2.1 Repetition.
Homework Any Questions?.
Control Structures Part 3
CSC215 Lecture Control Flow.
Presentation transcript:

CS1061 C Programming Lecture 9: switch Statement and Variable Scope A. O’Riordan, 2004

Nested if statements Conditional statements can be nested. Here is an example: if (mark >= 80){ printf(“You achieved distinction\n”); } else{ if (mark >= 60){ printf(You achieved merit\n”); } else{ printf(“You fail\n”); printf(“Hard luck, try again\n”); } } /* end outer else block */

switch statement C has a special statement ideal for such examples. It is a multi-way conditional statement called the switch statement. The alternative outcomes are labelled with the case keyword. A conditional expression is evaluated and execution continues from the case label with a value that matches. The expresssion and the label value must be integer-valued. If none match execution continues from the default label. A break statement is often used to exit the switch statement before execution flows into the next alternative. The default part of the switch is optional. If it is omitted and none of the cases are chosen, the flow of control continues after the switch statement.

Form of switch statement The syntax is now given: switch(integer-expr){ case value1: case value2: … case valuen: [default: ] } A is a squence of statements. E.g. a=a+2; printf(“%d”,a); The square brackets indicate that the default part is optional.

break and continue Break statement: When a break statement is encountered the currently executing loop exits and execution continues after the loop. For example: for ( ; ; ){ printf("Executed only once\n"); break; printf("Never ever executed\n"); } Continue statement: The continue statement is similar to the break statement, but instead of exiting the loop, execution resumes back at the next iteration of the loop.

switch Example char letter; printf("Enter a character: "); scanf("%c", &letter); switch(letter){ case 'a': printf("\nYou have selected \'a\'\n"); printf("Have a nice day\n"); break; case 'b': printf("\nYou have selected \'b\'\n"); printf("Enjoy your day\n"); break; default: printf("\nYou did not enter a valid letter\n"); printf("Goodbye!\n"); }

goto Statement Another instruction supported is the much-maligned goto statement. This transfers control to a labelled instruction. It is rare for the use of goto to be necessary and the desired effect will nearly always be achieved by correctly subdividing the program into functions and using the break and continue statements. label: ; /*jumping to here*/. goto label;

Scope of a variable Every identifier in a program has a scope, the region of the program where it is defined. It is the declaration that dictates the scope of an identifier. Specifically the enclosing block ({}) is the scope of all variables except so- called global variables, which are defined at the top-level and are visible everywhere in the file where declared. Here is a simple example, which illustrates variable scope: int c = 123; int main(){ char c = ‘b’; } All references to c in main() refer to the char c, whereas all references to c outside main refer to the int c.

Scope of a variable (2) This can be shown schematically with a diagram: Local variables have, in C parlance, the storage class automatic. Local variables may be prefixed with the keyword auto, e.g. auto int i; This rarely appears in C programs because it is the default.

Global Variables Globally declared variables and functions can be seen everywhere in the program, i.e. by all functions. But it is considered good software engineering practice to have as few global variables as possible. If you find that your code has a lot of global variables consider re-designing the program. #include char global_char = ‘G’; int main(){ printf("A global character %c\n",global_char); }