Adapted from Pearson Addison-Wesley Addison Wesley is an imprint of Chapter 4: Selection Structures: if Statement Adapted from Problem Solving & Program.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6: Modular Programming Problem Solving & Program Design in.
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10: Recursion Problem Solving & Program Design in C Sixth Edition.
Computer Programming w/ Eng. Applications
Chapter 4 Selection Structures: if and switch Statements Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering.
Adapted from Pearson Addison-Wesley. Addison Wesley is an imprint of Chapter 3: Math Functions Adapted from Problem Solving & Program Design in C Sixth.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8: Arrays Problem Solving & Program Design in C Sixth Edition.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Chapter 4 Selection Structures: if and switch Statements.
Chapter 4 Selection Structures: if and switch Statements Instructor: Alkar / Demirer.
C programming.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 2: Overview of C Problem Solving & Program Design in C Sixth.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 4: Selection Structures: if and switch Statements Problem Solving.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Recursion Problem Solving and Program Design in C 5th Edition.
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
Chapter 4 Selection Structures: if and switch Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 2: Overview of C Problem Solving & Program Design in C Seventh.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
CHAPTER 4: The Basic of C CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (May 2012)
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
A. Abhari CPS1251 Topic 4: Control structures Control Structures Overview Conditions if Statement Nested if Statements switch Statement Common Programming.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 9: Recursion Problem Solving & Program Design in C Seventh.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 13: Programming in the Large Problem Solving & Program Design.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
© 2012Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Department of CSE Control Structures: Selection.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Simple C Programs.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
Chapter 4 (Conditional Statements)
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Introduction to C Programming
Introduction to Programming
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
C Programming Variables.
Lec 5.
Selection Structures: if and switch Statements
Computer Programming Techniques Semester 1, 1998
Control Structures Lecture 6.
Comments Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example.
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
Chapter 2, Part III Arithmetic Operators and Decision Making
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

Adapted from Pearson Addison-Wesley Addison Wesley is an imprint of Chapter 4: Selection Structures: if Statement Adapted from Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

1-2 © 2010 Pearson Addison-Wesley. All rights reserved. 1-2 Review Constant and variable names … Example CANNOT start with a number2i CAN contain a number elsewhereh2o CANNOT contain any arithmetic operators... r*s+t CANNOT contain any other punctuation marks... £ !!a CAN contain or begin with an underscore _height_ CANNOT be a C keyworddouble CANNOT contain a spaceim stupid CAN be of mixed casesXSquared

1-3 © 2010 Pearson Addison-Wesley. All rights reserved. 1-3 Review #include int main() { int a,b,c,d,e; a = 10; b = 4.3; c = 4.8; d = 'A'; e = ; printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d); printf("e = %d\n", e); printf("b+c = %d\n", b+c); printf(" as integer type would yield %d \n", ); return (0); } WHAT WILL BE ON THE SCREEN?

1-4 © 2010 Pearson Addison-Wesley. All rights reserved. 1-4 Review a = 10 b = 4 c = 4 d = 65 e = 9 b+c = as integer type would yield #include int main() { int a,b,c,d,e; a = 10; b = 4.3; c = 4.8; d = 'A'; e = ; printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d); printf("e = %d\n", e); printf("b+c = %d\n", b+c); printf(" as integer type would yield %d \n", ); return (0); }

1-5 © 2010 Pearson Addison-Wesley. All rights reserved. 1-5 Review Integer division int rslt; rslt = 17 / 5; rslt = 3 rslt = 7 / 4;rslt = 1 Modulus /remainder rslt = 17 % 5;rslt = 2 rslt = 7 % 4;rslt = 3

1-6 © 2010 Pearson Addison-Wesley. All rights reserved. 1-6 Review #include #define PI 3.14 int main() { int i, p1, p2, sin_number, log_number; double x,y,z; printf("Enter an integer:" ); scanf("%d",&i); printf("Absolute value of %d= %5d\n",i,abs(i)); printf("Enter any three numbers. First for square root, others for floor and ceil:" ); scanf("%lf %lf %lf", &x, &y, &z); printf("square root of %f= %7.4lf\n",x,sqrt(x)); printf("ceils of %f and %f are %7.0f and %7.0f\n", y,z,ceil(y),ceil(z)); printf("floors of %f and %f are %7.0f and %7.0f\n", y,z,floor(y),floor(z)); /*Power function*/ printf("Enter two integers for power function: "); scanf("%d %d", &p1, &p2); printf("%d to the power %d= %f \n", p1, p2, pow(p1,p2)); /*Trigonometric and logaritmic functions */ printf("Enter two integers for sinus, in degrees, and log functions: "); scanf("%d %d", &sin_number, &log_number); printf("sin(%d) = %7.4f \n", sin_number, sin(PI*sin_number/180)); printf("log(%d) = %7.4f \n", log_number, log10(log_number)); system ("pause"); return (0); }

1-7 © 2010 Pearson Addison-Wesley. All rights reserved. 1-7 Figure 4.1 Evaluation Tree and Step-by- Step Evaluation for !flag || (y + z >= x - z)

1-8 © 2010 Pearson Addison-Wesley. All rights reserved. 1-8 if Statement Single selection structure  One Alternative if (condition) statement; Double selection structure  Two Alternatives if (condition) first statement; else second statement;

1-9 © 2010 Pearson Addison-Wesley. All rights reserved. 1-9 Figure 4.4 Flowcharts of if Statements with (a) Two Alternatives and (b) One Alternative if (rest_heart_rate > 56) printf(“Your heart is in excellent condition”) else printf(“Keep up your exercise program”) if (x !=0.0) product = product *x;

1-10 © 2010 Pearson Addison-Wesley. All rights reserved Figure 4.5 if Statement to Order x and y

1-11 © 2010 Pearson Addison-Wesley. All rights reserved Nested If double salary, tax; scanf(“%lf” &salary); if (salary < 0.0) tax = -1.0; else if (salary < )/* first range*/ tax = 0.15 * salary; else if (salary < )/* second range*/ tax = (salary ) * ; else if (salary < )/* third range*/ tax = (salary ) * ; else if (salary < )/* fourth range*/ tax = (salary ) * ; else if (salary <= )/* fifth range*/ tax = (salary ) * ; else tax = -1.0;

1-12 © 2010 Pearson Addison-Wesley. All rights reserved Nested if vs Sequence of “if” statements if (x>0) num_of_pos = num_of_pos + 1; else if (x<0) num_of_neg = num_of_neg + 1; else num_of_zero = num_of_zero + 1; if (x>0) num_of_pos = num_of_pos + 1; if (x<0) num_of_neg = num_of_neg + 1; if (x==0) num_of_zero = num_of_zero + 1; versus Both are logically equivalent but sequence is less readable and less efficient

1-13 © 2010 Pearson Addison-Wesley. All rights reserved Figure 4.11 Flowchart of Road Sign Decision Process

1-14 © 2010 Pearson Addison-Wesley. All rights reserved Road Sign Decision if (road_status == ‘S’) if (temp > 0 ){ printf(“Wet roads ahead\n”); }else { printf(“Icy roads ahead\n”); } else printf(“Drive carefully\n”);