Selection CSCE 121 J. Michael Moore.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
The if-else Statements
Conditionals with Strings The comparison operators we’ve seen so far (==, !=, >=, > etc.) all apply to numbers ( ints floats doubles etc.) and return either.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CSC Programming for Science Lecture 11: Switch Statements & ?: Operator.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
CS 180 Recitation 9/20/07 - 9/21/07. Announcements Exam 1 is Tuesday, September 25 th  Rooms: Sec WTHR 104 Sec MATH 175  Time: 7:00.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
C Programming Lecture 11.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Branches and Program Design
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 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.
 Type Called bool  Bool has only two possible values: True and False.
Java Programming Fifth Edition
Decision Making in C.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
A mechanism for deciding whether an action should be taken
Lecture 3- Decision Structures
Flow of Control.
Expressions and Control Flow in JavaScript
Chapter 4: Making Decisions.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Iteration CSCE 121 J. Michael Moore.
Loop Control Structure.
More Selections BIS1523 – Lecture 9.
Conditional Statements
IF if (condition) { Process… }
Other Conditional Methods
Selection (if-then-else)
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 7 Conditional Statements
Logical Operations In Matlab.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Conditionals.
The Java switch Statement
CMPE212 – Reminders The other four assignments are now posted.
Nate Brunelle Today: Conditional Decision Statements
Decisions, decisions, decisions
Decision making and control functions
Chapter 3: Selection Structures: Making Decisions
CSC215 Lecture Control Flow.
Controlling Program Flow
Presentation transcript:

Selection CSCE 121 J. Michael Moore

If if (bool expression) { } if (bool expression) // single statement However, it is good practice to go ahead and use them. You might need to add more statements later and forget to add curly braces. if (bool expression) { } if (bool expression) // single statement true Note: Curly braces are optional if there is a single statement. false

If-else if (boolean expression) { // do if true } else { // do if false } false true Note: Curly braces are optional if there is a single statement.

Multiple if-else Note: Curly braces are optional if there is a single statement. if (boolean expression) { // do stuff } else if (bool expr) { // do other stuff } else { // do yet other stuff } false false true true

Switch true false true false Note: Curly braces are optional if there is a single statement. switch (value) { case val1: // do stuff break; case val2: // do other stuff default: // yet other stuff } true false true false

Switch with fall-through Note: Curly braces are optional if there is a single statement. switch (value) { case val1: // do stuff case val2: // do other stuff break; default: // yet other stuff } true false true false

Switch Value used for comparison must be an integer, char, or enumeration. More on enumerations later. When a matching case is found, code is executed until encountering a break. This can result in code for multiple cases executing for a single case.