Switch Case, Enums, and Random Numbers CS 242 Tarik Booker California State University, Los Angeles.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Programming Assignment #3 CS-2301, B-Term Programming Assignment #3 User-defined Functions Due, November 18, 11:59 PM (Assignment adapted from C:
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
 2007 Pearson Education, Inc. All rights reserved C Functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
C++ for Engineers and Scientists Third Edition
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
1 Lecture 3 Part 1 Functions with math and randomness.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CPS120 Introduction to Computer Science Iteration (Looping)
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Chapter 8: Arrays.
CPS120: Introduction to Computer Science Decision Making in Programs.
Previously Repetition Structures While, Do-While, For.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
C++ Programming Lecture 10 Functions – Part II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
CPS120 Introduction to Computer Science Iteration (Looping)
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
1 CS161 Introduction to Computer Science Topic #8.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Random numbers in C++ Nobody knows what’s next....
Working with Loops, Conditional Statements, and Arrays.
CPS120: Introduction to Computer Science Decision Making in Programs.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
COMP Loop Statements Yi Hong May 21, 2015.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
I F S TATEMENTS Tarik Booker CS 290 California State University, Los Angeles.
 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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Chapter 5: Control Structures II (Repetition)
CS161 Introduction to Computer Science
Chapter 10 Programming Fundamentals with JavaScript
פרטים נוספים בסילבוס של הקורס
Selection CSCE 121 J. Michael Moore.
We’re moving on to more recap from other programming languages
Control Structures Part 3
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Intro to Programming Concepts
Presentation transcript:

Switch Case, Enums, and Random Numbers CS 242 Tarik Booker California State University, Los Angeles

What we will cover… Switch case statements Enumerations (enums) Randomizations

Switch Case Statements Tired of writing long if else statements? Too Many Different Categories to decide from? Use Switch case statements to simplify them

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

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!

Switch Case Statements (4) Look at p107 for an example (Change the couts to printf’s and the cin’s to scanf’s)

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?

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!

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!

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

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)

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?

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

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

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