Download presentation
Presentation is loading. Please wait.
Published byClarence Craig Modified over 8 years ago
1
Switch Case, Enums, and Random Numbers CS 242 Tarik Booker California State University, Los Angeles
2
What we will cover… Switch case statements Enumerations (enums) Randomizations
3
Switch Case Statements Tired of writing long if else statements? Too Many Different Categories to decide from? Use Switch case statements to simplify them
4
Switch Case Statements (2) Syntax: switch( ) { case value1: //Code to execute break; case value2: //Code to execute break; default: //Execute this if none of the values are met break; } Now, you can use this instead of a lot of if-else’s
5
Switch Case Statements(3) Note: You must use the break statements after the end of each case, or else the case statement will keep running until it finds a break, or the end of the case statement Called fall-through behavior Values must be a constant integral expression! You cannot use variables in the case values!
6
Switch Case Statements (4) Look at p107 for an example (Change the couts to printf’s and the cin’s to scanf’s)
7
Enumerations There may be times when you want a variable that can change between only a few values You may also know all the possible values ahead of time Ex: A variable for background colors that a person can choose What colors? We can use enumerations for this These are variables that hold user-defined values Just use the keyword enum to define the type Ex: RainbowColor What colors are in the rainbow?
8
Enumerations (2) Syntax: enum RainbowColor { RC_RED, RC_ORANGE, RC_YELLOW, RC_GREEN, RC_BLUE, RC_INDIGO, RC_VIOLET }; (Don’t forget the semi-colon after the curly brace!) Now you can declare a variable of type RainbowColor!
9
Enumerations (3) Code: RainbowColor chosen_color = RC_RED; RainbowColor is our enumerated data type Chosen_color is a variable of our enumerated data type RC_RED is a value of our enumerated data type Note (for engineers) This is preferred for lower level programming, as it requires much less memory than a class or a struct Enums are rendered as integers!
10
Enumerations (4) You can now use this type of code: switch(chosen_color) { case RC_RED: /* paint red */ case RC_ORANGE: /* paint orange */ … case RC_VIOLET /* paint violet */ default: /*handle unexpected types */ } This code is legal
11
Enumerations (5) (I said earlier) Enumerations are implemented on the computer as integers The first value is 0, the second value is 1, etc. You can actually change the integer values of enums enum RainbowColor { RC_RED = 1, RC_ORANGE = 3, RC_YELLOW = 5, RC_GREEN = 7, RC_BLUE = 9, RC_INDIGO = 11, RC_VIOLET = 13 }; This may help with values of another system (or with code reuse)
12
Enumerations (6) Enumerations are also a great way to give names to hard- coded values Ex: Tic Tac Toe Instead of using numbers for places on the board, you can enumerate the spaces (but have them correspond to places on the board) enum TicTacToeSquare { TTTS_BLANK, TTTS_O, TTTS_X }; What other ways can you use enumerations?
13
Randomization Let’s say you want random input to you program Test values Games Rely on randomness (Why?) Random signals Noise The computer can generate (seemingly) random numbers Pseudo-random numbers Seem random, but generated by algorithm
14
How to get a random number? You need something called a seed Use the srand function It is a number that bases the random data in Use the time function to give a different number every time srand(time(NULL)); Once you get the seed, call rand() to generate the random number Displays a number between 0 and RAND_MAX RAND_MAX usually equals 32767
15
Generating a Range of Random Numbers Let’s say you need to get a random number between a certain range (0 – 10) You can use the modulus operator! Take the random number and mod with the size of the range Gets back a number from 0 to highest number of the range For a lower bound, just add the lowest number
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.