CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
1 Algorithms Basic Control Structures Comparisons and if (…) statement What is a function? Math Library Functions Character Functions Reading Sample Programs.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
CS 201 Functions Debzani Deb.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
C++ for Engineers and Scientists Third Edition
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
Basic Elements of C++ Chapter 2.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Variables, Assignment & Math Storing and naming data.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSC 107 – Programming For Science. Spacing in a Program  C++ ignores spaces in a program  This also means where newlines placed ignored  #define &
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Variables, Expressions, and Standard Functions. Topics Basic calculation Expressions, variables, and operator precedence Data types Input / Output Examples.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Instructor - C. BoyleFall Semester
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Lecture 01b: C++ review Topics: if's and switch's while and for loops.
CS 125 Lecture 9 Martin van Bommel. Output printf used to display integers, reals (floats and doubles), and strings printf(” control string ”, exp 1,
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.
Decision Statements, Short- Circuit Evaluation, Errors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Bill Tucker Austin Community College COSC 1315
Variables, Operators, and Expressions
Simple C Programs.
Today Variable declaration Mathematical Operators Input and Output Lab
CSC 4630 Meeting 7 February 7, 2007.
Variables Mr. Crone.
Computing and Statistical Data Analysis Lecture 2
BIL 104E Introduction to Scientific and Engineering Computing
Chapter 4: Making Decisions.
Selections Java.
TMF1414 Introduction to Programming
Fundamental of Java Programming Basics of Java Programming
Introduction to Programming
Math Library and IO formatting
Multiple Files Revisited
Output Formatting Bina Ramamurthy 4/16/2019 BR.
Rules of evaluation The value of a number is itself.
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel

Example 1 “ x is not equal to either 2 or 3” if (x != 2 || x != 3) No, it should be if (!(x == 2 || x == 3)) or if (x != 2 && x != 3)

Example 2 “x is in the range from 0 to 10 exclusive” if (0 < x < 10) No, it should be if (0 < x && x < 10)

Example Leap year every fourth year, except centuries, then just every fourth century –year is divisible by 4 but not by 100, or –year is divisible by 400 Try ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)

Bad Example if (x < 5) cout << ”A”; if (x > 5) cout << ”B”; else cout << ”C”;

If Blocking Rule For any if statement that requires (1) more than a single line, or (2) an else clause, always use braces to enclose in a separate block the statements under the control of the if statement.

Single-line if statement if ( condition ) statement where: condition is the Boolean value to test statement is a single statement to be executed if condition is true

Multiline if statement if ( condition ) { statements; } where condition is the Boolean value to test statements is a block of statements to be executed if condition is true

if-else statements if ( condition ) { statements T ; } else { statements F ; }

Cascading if statement if ( condition 1 ) { statements 1 ; } else if ( condition 2 ) { statements 2 ;... } else { statements none }

Crafting a Program Use comments to tell readers what they need to know, including difficult code Use indentation to show bodies of loops Use meaningful names Use convention for names - lastNumber Use standard idioms when appropriate Avoid unnecessary complexity

Designing for Change #define construct - symbolic constants #define symbol value symbol - name for symbolic constant value - replaces symbol in precompilation Why? Easier to modify program

Named Constants A variable whose content cannot be changed while program is running const double PI = ; Variable still has data type and memory location, but is read-only

Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls cout << ”Hello.\n”; Useless statements n1 + n2;

Embedded Assignments Assignment expression can be used as part of a larger expression in a statement Its value is the value assigned z = (x = 6) + y; x is assigned value 6, then z assigned 6 + y Difficult to read Used rarely and only when makes sense

Multiple Assignments Embedded assignments useful to set several variables to the same value n1 = n2 = n3 = 0; Assignment operator evaluated right to left Avoid mixed types; e.g. double d and int i d = i = 1.5; Assigns i value 1, thus 1 assigned to d

Math Library Functions for performing math operations abs(x) absolute value of argument sqrt(x) square root of argument pow(y, x) y x sin(x) sine (argument in radians) cos(x) cosine (argument in radians) tan(x) tangent (argument in radians) log(x) natural logarithm exp(x) e x