Enum.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Unit 9 It's warm 清丰县第一实验小学 唐利娟.
9-Jun-14 Enum s (and a review of switch statements)
Module 4: Statements and Exceptions. Overview Introduction to Statements Using Selection Statements Using Iteration Statements Using Jump Statements Handling.
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
What season is it?. How can we tell it’s fall? annie apple Who is this? Right! It’s annie apple. She likes to tell you all about apples that become ripe.
Chapter 4 Program Control Statements
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
The C# language fundamentals ( II ) Po Feng, Tsai.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
 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
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Week 4 - Monday.  What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
From C to C++. 2 Why C++ is much more fun than C (C++ FAQ)? 1.Classes & methods - OO design 2.Generic programming - Templates allow for code reuse 3.Stricter.
 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.
Enum. enum – a new type 2 enum is a set of constant int values, that defines a type: enum Season { WINTER,// = 0 by default SPRING,// = WINTER + 1 SUMMER,//
C++ Lesson 1.
© 2016, Mike Murach & Associates, Inc.
Computing and Statistical Data Analysis Lecture 2
Def: A control structure is a control statement and
From C to C++.
A season is a time of the year.
Unit-1 Introduction to Java
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
From C to C++.
Chapter 4: Making Decisions.
Switch, Rounding Errors, Libraries
Week 4 - Monday CS222.
Control Structures.
Switch Statements, Do While, Break, Continue, Goto, Comma Operator
Machine-Level Programming: Control Flow
Namespaces, Typedefs & Enums
פרטים נוספים בסילבוס של הקורס
Conditional Statements
(and a review of switch statements)
(and a review of switch statements)
SEASONS.
Enumerations CS Fall 1999 Enumerations.
Coding Constructs considered Violations of Structured Programming
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
SystemVerilog for Verification
Control Structures Part 3
CMPE212 – Reminders The other four assignments are now posted.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Program Flow.
Programming Language C Language.
COP 3330 Notes 4/6.
CSC215 Lecture Control Flow.
Controlling Program Flow
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

enum

enum – a new type enum is a set of constant int values, that defines a type: enum Season { WINTER, // = 0 by default SPRING, // = WINTER + 1 SUMMER, // = WINTER + 2 AUTUMN // = WINTER + 3 };

enum – a new type enum is a set of constant int values, that defines a type: typedef enum Season { WINTER, // = 0 by default SPRING, // = WINTER + 1 SUMMER, // = WINTER + 2 AUTUMN // = WINTER + 3 } Season;

enum – a new type, usage typedef enum Season {WINTER, SPRING, SUMMER, AUTUMN} Season; Season curr_seasons= SPRING; curr_season= 19; // legal, but UGLY! int prev_season= WINTER; // legal, but UGLY!

enum – numbering typedef enum Color { RED=2, GREEN, //==3 By default enums start with 0 and then +1 for each one. This can be changed: typedef enum Color {    RED=2, GREEN, //==3 BLUE=8 } Color;

Use enum to eliminate magic numbers – alternative to #define

enums – why? More readable code Code less error prone Accessible for debugger Use of the numerical values is not disabled, but bad programming usually!

switch

switch if (integer value == 3) //statement or block else if (integer value == 5) //statement or block switch (integer value) { case 3: //statement or block break; //optional. Otherwise fall-through! case 5: //statement or block case default: //statement or block }

switch & enum switch (s) { case WINTER: typedef enum Season {WINTER, SPRING, SUMMER, AUTUMN} Season; Season s; ... switch (s) {    case WINTER: printf(“Cold and raining\n”);     break; //optional. Otherwise fall-through! case SPRING: printf(“Nice weather\n”); break; //optional. Otherwise fall-through!

goto

DANGER: SPAGHETTI CODE! Avoid whenever possible. goto keyword goto bla; whee: //code here bla: //code there DANGER: SPAGHETTI CODE! Avoid whenever possible. Q: When do use? A: There are cases. e.g. : Break from a lot of nested loops Forward jump to error handler