Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 4 - Control Statements
Fundamental of C programming
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
 Control structures  Algorithm & flowchart  If statements  While statements.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
The switch Statement, DecimalFormat, and Introduction to Looping
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
Conditional Control Flow By Muhammad Ahsan Qadar SACS Programming Fundamentals Workshop 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
Conditionals CS 103 February 16, Blast from the Past: C14 Dating Problem Statement: Calculate the age of a fossil from its C-14 radioactivity Problem.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
CSE123 Lecture 3 Files and File ManagementScripts.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Digital Image Processing Lecture 6: Introduction to M- function Programming.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Calvin College Controlling Behavior The if, switch and for Statements.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
ITEC 2600 Introduction to Analytical Programming
Making Choices with if Statements
The switch Statement, and Introduction to Looping
ECE 1304 Introduction to Electrical and Computer Engineering
Selection Statements by Ahmet Sacan
ITM 352 Flow-Control: if and switch
Scripts & Functions Scripts and functions are contained in .m-files
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Conditions and Ifs BIS1523 – Lecture 8.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
3 Control Statements:.
If selection construct
3. Decision Structures Rocky K. C. Chang 19 September 2018
Summary Two basic concepts: variables and assignments Basic types:
Computer Science Core Concepts
Using Decision Structures
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Selection Statements Chapter 3.
Chap 7. Advanced Control Statements in Java
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Lecture 9: Implementing Complex Logic
REPETITION Why Repetition?
Presentation transcript:

Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and then put it in if x == 7 fprintf('Lucky number!\n'); else fprintf('Small number\n'); end else if x == 13 fprintf('Unlucky number!\n'); else fprintf('Big number\n'); end

Control Constructs  Sequential Control Normal execution, by order of statements.  Selection or branching statements Statement chosen from two or more. Ex: if construct  Loops Statements are repeated. Ex: for loop

Selection or branching statements Most common: if construct IF expression statements ELSEIF expression statements ELSEIF expression statements. : ELSE statements END

More on if construct Must have one if and an end. May have any number of elseif clauses. May have one else clause. if constructs can be nested.

Exercise Write a program that prints out the days when the user enters a number. ‘1’ is ‘Sunday’ ‘2’ is ‘Monday’ ‘3’ is ‘Tuesday’ Any other number is ‘Any other day’

Some Definitions Nesting – Including a branching statement as one of the branches of another branching statement. It is allowed in Matlab and all other languages. Making a program robust. –A program is robust only if it does something reasonable with all inputs. –Example: handle numbers with imaginary components, as in last week’s example.

More Definitions Noun: Branch. A point in a program where the control can switch to more than one command. Verb: Branch. To choose a statement based on the state of the variables

Boolean Logic The expression in an if construct is an example of Boolean logic. Boolean has only two states: True and False. Traditionally, this is thought of as 1 = True and 0 = False. In Matlab, 0 = False, and True is any non- zero value.

Logical Operators and Truth Tables XYX&YX|Y True0 00 Operators AND ( & ) OR ( | ) NOT ( ~ )

Relational Operators ==Equal to >Larger than <Less than >=Larger or equal to <=Less or equal to ~=Not equal to

Precedence of operators Arithmetic operators Relational operators ~ & |

Examples of Relational and Logical operators in an if construct if X > 0 & X<10 fprintf(‘Positive number less than 10’); end % if grades == ‘A’ | grades == ‘B’ fprintf(‘My CGPA will not suffer’); end

switch statement or switch construct: switch switch_expression case case_expression, statements case case_expression, statements case {case_expression1, case_expression2, case_expression3,...} statements... otherwise statements end

When to use the switch construct Switch is appropriate when there are a "small" number of constant values that must be treated differently. Useful only when it is possible to list all possible values of an expression. Especially useful for menus or highly restricted data validation No logical or relational statements. Just listed values. –Example: x < 1.70: if-statement (because there are an infinite number of values) –Example: x == 1 or x == 2: switch statement.

Example of switch construct grades=input(‘Enter my CS 103 grade’,’s’); switch grades case ‘A’ fprintf(‘Plan for med school’); case ‘B’ fprintf(‘Can still plan for med school’); case {‘C’, ‘D’} fprintf(‘Do not plan for med school’); otherwise fprintf(‘Practice: Do you want fries with that?’); end

Exercise with Switch construct Write a program that prints out the days when the user enters a number. ‘1’ is ‘Sunday’ ‘2’ is ‘Monday’ ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, is ‘The rest of the week’ Any other number is ‘Not a real day’

Example Write a program that tells whether a measurement is long enough or not. Less than ten feet is too short Ten to fifteen feet is good More than fifteen is too long

Example Write a program that will print a series of choices for the users and gets an input from them.