Lecture 18: 10/31/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.

Slides:



Advertisements
Similar presentations
Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Advertisements

 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
1 CSC103: Introduction to Computer and Programming Lecture No 8.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Expressions and statements Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
C++ for Engineers and Scientists Third Edition
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CIS Computer Programming Logic
Lecture 2: Static Methods, if statements, homework uploader.
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Basic Control Structures
Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Structures CPS120: Introduction to Computer Science Lecture 5.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Lecture 13: 10/10/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Control statements Mostafa Abdallah
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
1 Conditional Statements + Loops ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
CNG 140 C Programming (Lecture set 3)
BIL 104E Introduction to Scientific and Engineering Computing
Sequence, Selection, Iteration The IF Statement
JavaScript: Control Statements I
Chapter 8 - JavaScript: Control Statements I
CS149D Elements of Computer Science
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
CS149D Elements of Computer Science
Computer Science Core Concepts
CS149D Elements of Computer Science
CS149D Elements of Computer Science
Presentation transcript:

Lecture 18: 10/31/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 18: 10/31/2002

CS149D Fall Outline Finish Chapter 2  Mathematical and Trigonometric functions  Example page 48 Chapter 3  Flowcharts  Program Structures  Conditional expressions  Selection Statements

Lecture 18: 10/31/2002CS149D Fall Mathematical/Trigonometric functions #include Argument is a double and return value a double fabs(x)absolute value of x sqrt(x)square root of x pow (x,y)x to the power of y ceil (x)ceiling (3.1) = 4 floor(x)floor (3.99) = 3see page 46 for more functions #include Argument is a double and return value a double, angles are represented in radians (180 = PI * radians) sin(x) cos(x) tan(x,y)see page 47 for more functions

Lecture 18: 10/31/2002CS149D Fall Example Example page 48Velocity Computation Velocity = time time time Acceleration = 3 – velocity 2 See program on page 51

Lecture 18: 10/31/2002CS149D Fall Chapter 3 Flowcharts as a tool for algorithm representation Control structures (selection, repetition) Data Files

Lecture 18: 10/31/2002CS149D Fall Flowchart Symbols OperationPseudocode InputRead radius ComputationAssign area the value PI * radius 2 OutputWrite (or Print) radius, area Comparisonsif radius < 0 then Begin/End Read radius Area = PI * radius2 Write radius, area Is radius < 0 ? No Yes Start/Stop

Lecture 18: 10/31/2002CS149D Fall Program Structures 1/3 Sequence structure Steps that are performed one after another (all programs so far) Flowchart for the triangle area program Start read base, height Triangle area = 0.5 * base * height Print area Stop

Lecture 18: 10/31/2002CS149D Fall Program Structures 2/3 Selection structure Contains a condition that evaluates to either true or false If condition is true one set of statements is executed, otherwise (if false) another set of statements is executed If a > 10 Print B B = a * 20 Print C C = a/20 NoYes

Lecture 18: 10/31/2002CS149D Fall Program Structures 3/3 Repetition structure Repeat a set of steps as long as a condition is true Print x 2 for x = 0,1,…., 9 x = 0 Is x < 10 ? y = x 2 Print y Increment x Yes No

Lecture 18: 10/31/2002CS149D Fall Conditional Expressions A condition is an expression that can be evaluated to true or false Composed of expressions combined with relational and logical operators Relational operators (, >=, ==, !=) a < b  a = 10 b = 2a < b evaluates to false  a = 2 b = 10a < b evaluates to true a = b > c; If b > c then a is assigned 1 otherwise assigned 0 Logical operators ( ! (NOT), && (AND), || (OR))  a < b && b < c (a less than b) AND (b less than c)  a is –2, b is 9, c is 2condition evaluates to false (Why?)  Relational operators have higher precedence than logical operators. See precedence table page 67

Lecture 18: 10/31/2002CS149D Fall Selection Statements 1/2 if (condition) Statement 1; Example if ( A < 10) B = 5; C = 2; A statement can be a compound statement if (condition) { Statement 1;.. Statement n; } Example if (A < 10) { B = 5; A++; } C = 2; if/else statement if (condition) Statement 1; else Statement 2 ; Example if ( A < 10) B = 5; else C = 2;

Lecture 18: 10/31/2002CS149D Fall Selection Statements 2/2 if ( A < 10) B = 5; else { C = 2; D = 3; } Nested ifs if (x > y) if (y < z) K++; else M++; else J++; Compiler always matches else with closest if Nested ifs if (x > y) if (y < z) K++; else J++; When J++ gets executed?