CS005 Introduction to Programming:

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
Fundamental Programming Fundamental Programming More on Selection.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Decision making If.. else statement.
Lesson Objectives Aims To be able to write an algorithm in Pseudo Code
Conditional or Decision Logic
Selection: Non-linear programs
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Chapter 4 MATLAB Programming
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Building Java Programs Chapter 4
Introduction to Programming in MATLAB
CS005 Introduction to Programming:
Selection Statements by Ahmet Sacan
Chapter 4: Control Structures
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Adapted from slides by Marty Stepp and Stuart Reges
If, else, elif.
Simple Control Structures
Chapter 4: Making Decisions.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
CS005 Introduction to Programming
Lecture 4: Conditionals
Topic 11 Scanner object, conditional execution
Microsoft Visual Basic 2005 BASICS
Selection By Ramin && Taimoor
Control Structures – Selection
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
Conditional Statements
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Alternate Version of STARTING OUT WITH C++ 4th Edition
Executes a block of statements only if a test is true
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
CS005 Introduction to Programming
CSc 110, Autumn 2016 Lecture 9: input; if/else
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming: Matlab
If selection construct
Visual Basic – Decision Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Lecture 6: Conditionals AP Computer Science Principles
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Selection Statements Chapter 3.
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
CS31 Discussion 1D Fall18: week 2
Presentation transcript:

CS005 Introduction to Programming: Matlab Eamonn Keogh eamonn@cs.ucr.edu

Conditional Branching The if statement Get the Users Score Selectively run some code Check if score less than 90 Yes No Display Warning Message Continue…

Conditional Branching The if/else statement Get the Users Score Selectively run one of two code options Check if score less than 90 No Yes Display Confirmation Message Display Warning Message Continue…

Conditional Branching The if/elseif statement Get the Users Score Selectively run one of three or more code options Check score Display A Display B Display C Display Fail Continue…

We can further generalize the if/else statement to include the elseif clause if some logical expression is true do these statements elseif some other logical expression is true do these statements instead else end The if block The elseif block The else block In the above code, exactly one block will happen. There is no way that two or three could happen, and there is no way none could happen. This elseif statement divides the world into three mutually exclusive worlds

The last generalization of the if/elseif statement is it realize that we can have more than one elseif if some logical expression is true do these statements elseif some other logical expression is true do these statements instead elseif yet another logical expression is true else end The if block First elseif block Second elseif block The else block In the above code, exactly one block will happen. There is no way that more than one could happen, and there is no way none could happen. This elseif statement divides the world into three or more mutually exclusive worlds

GetExamScore Let us write a function that: Failing C B A Asks a user to enter the grade they got out of 100 Echoes the letter grade to the screen Returns that grade (No input parameters) GetExamScore (echo letter grade) 79 Failing C B A 70 80 90 100 score

function Score = GetExamScore() Score = input('Enter your score : '); if Score >= 90 disp('A quality work'); elseif Score >= 80 disp('B quality work'); elseif Score >= 70 disp('C quality work'); else disp('Failing'); end EDU>> EamonnsScore = GetExamScore() Enter your score : 99 A quality work EamonnsScore = 99

Wrong! function Score = GetExamScore() Score = input('Enter your score : '); if Score >= 90 disp('A quality work'); elseif Score >= 80 disp('B quality work'); elseif Score >= 70 disp('C quality work'); elseif Score >= 0 disp('Failing'); end Wrong! Common mistake The last block of code must be a else, not an elseif. (contrast with previous slide)

GetHeight Let us write a function that: Short Tall Asks a user to enter their height in feet (no inches) Echoes the {tall,medium,short} to the screen Returns their height in feet (no inches) (No input parameters) If you are taller than 6 foot, you are tall If you are 5 foot, you are medium If you are 4 foot or shorter, you are short GetHeight (echo height) 4 Medium Short Tall 5 6 Height

In this code it is never possible to be short, only tall or medium. function Height = GetHeight() % Has bug Height = input('Enter your height in feet only : '); if Height >= 6 disp('Tall'); elseif Height < 6 disp('Medium'); else disp('Short'); end In this code it is never possible to be short, only tall or medium. function Height = GetHeight() Height = input('Enter your height in feet only : '); if Height >= 6 disp('Tall'); elseif Height >= 5 disp('Medium'); else disp('Short'); end We need to test all the breakpoints (critical values), plus or minus 1

Tall Short Short Tall Short Short Medium Medium Medium 6 5 Tall 6 5 Medium Short Medium Short Tall if Height >= 6 5 6 Medium Short elseif Height >= 5 5 6 Short else 5

We can have if statements inside of if statements We can have if statements inside of if statements. This is called nesting. if some logical expression is true else do these statements instead end if some logical expression is true do these statements else do these statements instead end The if block The else block

GetUserCode Let us write a function that: Asks a user to enter their sex and age Returns a code, from one to four 1 : man 2 : woman 3: boy 4: girl Echoes the persons label to the screen (No input parameters) EDU>> GetUserCode Enter 1 for male, 2 for female : 1 Enter your age : 55 Man Enter 1 for male, 2 for female : 2 Enter your age : 5 Girl GetUserCode (echo person label) 4

function Label = GetUserCode() sex = input('Enter 1 for male, 2 for female : '); age = input('Enter your age : '); if sex == 1 disp('Male'); else disp('Female'); end Let us build up to it. Let us ignore age for now, and ignore the return value. This code just correctly displays male/female EDU>> GetUserCode Enter 1 for male, 2 for female : 2 Enter your age : 2 Female EDU>>

Let us ignore the return value for now function Label = GetUserCode() sex = input('Enter 1 for male, 2 for female : '); age = input('Enter your age : '); if sex == 1 if age >= 18 disp('Man'); else disp('Boy'); end disp('Woman'); disp('Girl'); Let us build up to it. Let us ignore the return value for now This code just correctly displays man/woman/boy/girl EDU>> GetUserCode Enter 1 for male, 2 for female : 1 Enter your age : 1 Boy Enter your age : 55 Man Enter 1 for male, 2 for female : 2 Enter your age : 2 Girl Woman EDU>>

The whole thing. function Label = GetUserCode() sex = input('Enter 1 for male, 2 for female : '); age = input('Enter your age : '); if sex == 1 if age >= 18 disp('Man'); Label = 1; else disp('Boy'); Label = 3; end disp('Woman'); Label = 2; disp('Girl'); Label = 4; The whole thing.

function Label = GetUserCode() sex = input('Enter 1 for male, 2 for female : '); age = input('Enter your age : '); if sex == 1 if age >= 18 disp('Man'); Label = 1; else disp('Boy'); Label = 3; end disp('Woman'); Label = 2; disp('Girl'); Label = 4; As an aside, we can begin to appreciate how important pretty printing is