Conditionals 1.The if Statement 1. switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Conditional Statements Introduction to Computing Science and Programming I.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
“Operators” (i.e. “symbols”) 1.Overview: Specific Symbols that Represent Specific Actions 2.Arithmetic 3.Relational 4.Boolean 5.Output values 1.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
CIS 3301 C# Lesson 3 Control Statements - Selection.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
1. switch statement 2. “Nested” statements Conditionals, part 2 1.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Error Handling Tonga Institute of Higher Education.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
Decision Statements, Short- Circuit Evaluation, Errors.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
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 this.
JavaScript: Conditionals contd.
Matlab Programming for Engineers
Java Programming Fifth Edition
Chapter 4: Making Decisions.
Python: Control Structures
Chapter 4 (Conditional Statements)
Chapter 4: Making Decisions.
JavaScript: Control Statements.
Conditional Statements
Selection Statements Chapter 4 Attaway MATLAB 4E.
The switch statement: an alternative to writing a lot of conditionals
Logical Operations In Matlab.
CS2011 Introduction to Programming I Selections (II)
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Control Structures Part 3
Selection Statements Chapter 4 Attaway MATLAB 5E.
Presentation transcript:

Conditionals 1.The if Statement 1

switch statement Allows for evaluation of multiple cases of the same variable The switch statement is looking for the variable to have an exact match to one of the cases. (No a<x && x<=b ) Specification may have multiple values enclosed in braces {…} The default case catches any values of the parameter other than the specified cases. The default case should trap bad parameter values. 2

General Template switch variable case specification 1. case specification n otherwise end 3 if elseif. elseif else end There is no limit to the number of cases.

switch Example 1: Multiple Cases Instead we use… switch month case {1,3,5,7,8,10,12} % 31-day months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-day months days = 30; otherwise fprintf('Invalid Entry.\n'); end 4 Let us modify the calendar example from an if to a switch if month==1 || month== 3 || month== 5 || … month== 7 || month== 8 || month== 10 || month== 12 Big advantage: reduces long OR statements of equality

Simulated “Run” % Suppose month is 4 switch month case {1,3,5,7,8,10,12} % 31-days months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-days months days = 30; otherwise fprintf('Invalid Entry.\n'); end 5

Simulated “Run” % Suppose month is 4 switch month case {1,3,5,7,8,10,12} % 31-days months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-days months days = 30; otherwise fprintf('Invalid Entry.\n'); end 6

Simulated “Run” % Suppose month is 4 switch month case {1,3,5,7,8,10,12} % 31-days months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-days months days = 30; otherwise fprintf('Invalid Entry.\n'); end 7

Simulated “Run” % Suppose month is 4 switch month case {1,3,5,7,8,10,12} % 31-days months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} %30-days months days = 30; otherwise fprintf('Invalid Entry.\n'); end 8

Simulated “Run” % Suppose month is 4 switch month case {1,3,5,7,8,10,12} % 31-days months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-days months days = 30; otherwise fprintf('Invalid Entry.\n'); end 9

Simulated “Run” % Suppose month is 4 switch month case {1,3,5,7,8,10,12} % 31-days months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-days months days = 30; otherwise fprintf('Invalid Entry.\n'); end 10

Simulated “Run” % Suppose month is 4 switch month case {1,3,5,7,8,10,12} % 31-days months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-days months days = 30; otherwise fprintf('Invalid Entry.\n'); end 11

Simulated “Run” % Suppose month is 4 switch month case {1,3,5,7,8,10,12} % 31-days months days = 31; case 2 days = 29; % leap year to be coded.. case {4,6,9,11} % 30-days months days = 30; otherwise fprintf('Invalid Entry.\n'); end ……… 12

switch Example 2: strings 13 switch statements can also be used to evaluate strings month = input('Enter the month: ', 's') switch month case {'Jan','March','May','July'... } %31-days - days = 31; case 'Feb' days = 29; %leap year to be coded.. case {'April', 'June','Sept','Nov'} %30-days days = 30; otherwise fprintf('Invalid Entry.\n'); end

switch - Menu's Several programs request the user to select an item from a menu: 14

switch Example 3: Menu Options %ask user what he'd like to do menu_choice = input('Select Item 1 to 4: '); %direct code to proper action switch menu_choice case 1 fprintf('You have selected 1.\n') case 2 fprintf('You have selected a number 2.\n') case 3 fprintf('You have selected a number 3.\n') case 4 fprintf('You have selected a number 4.\n') otherwise fprintf('Invalid Entry.\n'); end 15

if versus switch As general ideas: 16 ifswitch Combination of || statements that check equality Example: x==1 || x==2 || x==3 || x==4 Yes√ preferred Inequalities ( =, >)YesImpossible Conditions with &&: x == 2 && x == 7 YesImpossible Conditions that check multiple variables Such as: x==4 && y==7 √ preferredImpossible Menusok…√ preferred