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,//

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 清丰县第一实验小学 唐利娟.
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
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.
Plab - administration TA’s:TA’s: –Yoseph Barash –Liad Blumrosen –Michael Okun Contact ONLY ONLY website:
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
C Programming Constants and Control Structures. Constants Can be defined in two ways. –Traditional Method –#define PI 3.14 #define is a preprocessor command.
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.
The C# language fundamentals ( II ) Po Feng, Tsai.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
 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.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
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]
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.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
C++ Lesson 1.
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
Control Statements in C
Computing and Statistical Data Analysis Lecture 2
Def: A control structure is a control statement and
From C to C++.
Unit-1 Introduction to Java
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
From C to C++.
Week 4 - Monday CS222.
Control Structures.
Machine-Level Programming: Control Flow
Namespaces, Typedefs & Enums
פרטים נוספים בסילבוס של הקורס
Conditional Statements
SEASONS.
Enumerations CS Fall 1999 Enumerations.
פרטים נוספים בסילבוס של הקורס
Govt. Polytechnic,Dhangar
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
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.
Welcome back to Software Development!
Building Blocks of C Programming Language
Flow of Control.
CSC215 Lecture Control Flow.
Controlling Program Flow
Arrays Introduction to Arrays Reading for this Lecture:
Enum.
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
Presentation transcript:

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,// = WINTER + 2 AUTUMN// = WINTER + 3 };

enum – a new type 3 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 4 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!

5 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; enum – numbering

Use enum to eliminate magic numbers – alternative to #define 6

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

8 switch

9 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 10 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!

union

union – a new type A type that keeps (in different times) different types typedef union MyUnion { int i_val; double d_val; } MyUnion;

typedef union MyUnion { int i_val; double d_val; } MyUnion; MyUnion u; u.i_val= 3; printf("%d\n", u.i_val); u.d_val= 3.22; printf("%f\n", u.d_val); union – a new type, usage

14 goto

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