Control Statements II: Branch. Branch Three ways: if, case, switch The if statement executes statement(s) if a specified condition is true if condition.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

3-1 Chapter 3 Flow of Control (part a - branching)
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
HST 952 Computing for Biomedical Scientists Lecture 3.
The Collatz Problem Ellen Dickerson.
Introduction to Flowcharting
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.
Control Flow C and Data Structures Baojian Hua
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Structured Program Development in C
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
CSC103: Introduction to Computer and Programming
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Pseudocode Demo for Payroll.c
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010.
Slide 1 PHP Operators and Control Structures ITWA 133.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Decision Making and Branching (cont.)
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
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.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Decision Making and Branching
1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Branch: if, case, switch.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
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.
Computational Astronomy 제 5 장 프로그래밍 기초 전산천문학 봄.
 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.
Python Programming Module 4 Sensors and Loops Python Programming, 2/e1.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Introduction To Flowcharting
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Control Structures.
Learning About the Loop Structure (continued)
White-Box Testing Techniques III
Control Structures: for & while Loops
Fractions 1/2 1/8 1/3 6/8 3/4.
White-Box Testing Techniques III
Repetition Control Structure
Computer Science Core Concepts
What Color is it?.
Boolean Expressions to Make Comparisons
Unit 3 Review Game.
Repetition (While Loop) LAB 9
Chapter 3: Selection Structures: Making Decisions
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Control Statements II: Branch

Branch Three ways: if, case, switch The if statement executes statement(s) if a specified condition is true if condition then statement if condition then begin statement(s) endif if condition then statement else statement if condition then begin statement(s) endif else begin statement(s) endelse

Branch (if) Begin Else block Then block If Condition TrueFalse Endelse

Example: Calculate salary based on hours 0-40 hours/week $10/hour hours/week $15/hour for overtime part >168 hours/week error, stop Jump1: Print, ‘Input hours’ Read, nhr If(nhr ge 0 and nhr le 40) then begin s=10.0*nhr Endif else begin if(nhr gt 40 and nhr le 168) then begin nhr_over=nhr-40 s=10.0* *nhr_over endif else begin print,’Incorrect hours’ goto, jump1 endelse Endelse Print, ‘Salary = ’, s

Branch (cont.) The case statement branches to the first matching case in a list of cases and executes the statement(s) there, then terminates and go to the statement after the “endcase” statement case expression of exp1: exp2: statement exp3: begin statements(s) end else: statement endcase case 1 of cond1: statement cond2: begin statements(s) end else: statement endcase

Branch (case) Begin Block 1 Match Case 1? YesNo Endcase Match Case 2? YesNo Match Case 3? YesNo Block 2 Block 3

Example: Calculate salary based on hours 0-40 hours/week $10/hour hours/week $15/hour for overtime part >168 hours/week error, stop Jump1: Print, ‘Input hours’ Read, nhr Case 1 of (nhr ge 0 and nhr le 40): s=10.0*nhr (nhr gt 40 and nhr le 168): begin nhr_over=nhr-40 s=10.0* *nhr_over end else: begin print,’Incorrect hours’ goto,jump1 end Endcase Print, ‘Salary = ’, s

Branch (cont.) The switch statement branches to the first matching case in a list of cases and executes the statement(s) there, then goto the second matching case … switch expression of exp1: exp2: statement exp3: begin statement(s) end else: statement endswitch Difference with the case statement: (1) “case” executes only the first matching case, while “switch” executes all matching cases (2) “case” doesn’t allow no matching, but “switch” does

Branch (switch) Begin Block 1 Match Case 1? YesNo Endswitch Match Case 2? YesNo Match Case 3? YesNo Block 2 Block 3

Seven colors ; black, red, green, blue, cyan, mag, white r=[ 0, 1, 0, 0, 0, 1, 1 ] g=[ 0, 0, 1, 0, 1, 0, 1 ] b=[ 0, 0, 0, 1, 1, 1, 1 ] tvlct, r*255, g*255, b*255

Filling line plots with colors: Polyfill Syntax: Polyfill, x, y, color=color (,/line_fill) Example: pro eclipse, x, y, w, h, col, iline n=500 temp=findgen(n+1)/n*2.0*!pi x1=x+w*0.5*sin(temp) y1=y+h*0.5*cos(temp) if(iline eq 0) then begin polyfill,x1,y1,color=col endif else begin polyfill,x1,y1,color=col, /line_fill endelse end plot,[0,1], /nodata eclipse,0.3, 0.3, 0.2, 0.2, 1, 0 eclipse,0.7, 0.7, 0.2, 0.2, 1, 1 end

User-defined symbols: Usersym Syntax: Usersym, x, y, color=color, /fill Example: w=1 x=[-1,1,1,-1,-1]*w y=[-1,-1,1,1,-1]*w usersym,x,y,color=1,/fill plot,findgen(10),psym=8

In-class assignment IV Use two different methods (if and case) to calculate the saturation vapor pressure (svp) for a given temperature T (in deg C) T> 0 C svp=6.112 e 17.67T/(T+243.5) T< 0 C svp=6.1115e T/(T ) T 100 C incorrect data Please confirm the Collatz conjecture (hailstone numbers): Take any natural number n. If n is even, divide it by 2 to get n/2. If n is odd, multiply it by 3 and add 1 to obtain 3n+1. Repeat the process indefinitely. No matter what number you start with, you will always eventually reach 1. Please save the numbers and print them out. Then plot the numbers using (a) bar plot, (b) a user-defined symbol