ECE Application Programming

Slides:



Advertisements
Similar presentations
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Advertisements

UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
ECE Application Programming
The Ohio State University
Branching statements.
Chapter 4 – C Program Control
ECE Application Programming
Chapter 3 Control Statements
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
Chapter 4: Making Decisions.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Chapter 4: Making Decisions.
Week 3 C Program Structures (Selection Structures)
CHAPTER 4 Selection CSEG1003 Introduction to Computing
ECE Application Programming
Week 3 – Selection Structures
DKT121: Fundamental of Computer Programming
Chapter 4: Making Decisions.
JavaScript: Control Statements.
Conditional Statements
More Selections BIS1523 – Lecture 9.
Compound Assignment Operators in C++
EECE.2160 ECE Application Programming
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
EECE.2160 ECE Application Programming
CprE 185: Intro to Problem Solving (using C)
Week 3 – Program Control Structure
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Control Statements Paritosh Srivastava.
Comparing Data & the ‘switch’ Statement
EECE.2160 ECE Application Programming
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
CprE 185: Intro to Problem Solving (using C)
EECE.2160 ECE Application Programming
Switch Case Structures
Presentation transcript:

16.216 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 11: More if examples Conditional statements—switch

ECE Application Programming: Lecture 11 Lecture outline Announcements/reminders Today’s office hours: 2:00-3:00 only (ECE dept. meeting) Assignment 4 posted; due Friday, 10/7 Exam 1: Wednesday, 10/5 Will be allowed one double-sided sheet of notes Old exams on course website Assignment 3 grading to be completed soon Yes, I know … here we go again with this “completed soon” stuff Once graded, should be able to submit regrades via Blackboard Will need to e-mail me when you resubmit Another possible Mac (or Windows/Linux) IDE: NetBeans Today Range checking with if More if examples Conditional statements: switch 2/16/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Review: if statements Form: if (<expression>) <statement> [ else brackets show <statement> ] else is optional Expression frequently uses relational operators to test equality/inequality < > <= >= == != e.g., if (x <= 5) Can combine conditions using logical operators AND : && OR: || e.g., if ((x <= 5) && (x > 0)) Can test if condition is false using logical NOT: ! e.g., if (!(x < 5)) May test multiple conditions using if/else if/.../else Second condition is tested only if first is false; third condition tested only if first two are false, etc. Else case used only if all previous tests false 2/16/2018 ECE Application Programming: Lecture 11

if (range checking - take 1) int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (n > 10) { printf(“That’s not in range!"); } else if (n < 1) { printf(“That’s not in range!"); } else { printf("Good job!"); } 2/16/2018 ECE Application Programming: Lecture 11

if (range checking - take 2) If there is only one statement needed for the true and/or false condition, the {} are not needed int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (n > 10) printf(“That’s not in range!"); else if (n < 1) printf(“That’s not in range!"); else printf("Good job!"); 2/16/2018 ECE Application Programming: Lecture 11

if (range checking - take 3) Use the && or || as needed to check for multiple conditions int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); Note these ( ) are needed if ( (n > 10) || (n < 1) ) printf(“That’s not in range!"); else printf("Good job!"); 2/16/2018 ECE Application Programming: Lecture 11

if (range checking - take 4) Use the && or || as needed to check for multiple conditions int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); Note these ( ) are needed if ( (1 <= n) && (n <= 10) ) printf(“Good job!"); else printf(“That’s not in range!"); 2/16/2018 ECE Application Programming: Lecture 11

if (range checking) (The WRONG WAY) int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (1 <= n <= 10 ) // THIS WILL NOT COMPILE printf("Good job!"); else printf(“That’s not in range!"); 2/16/2018 ECE Application Programming: Lecture 11

Example 1: if statements Write a short code sequence to do each of the following: Given int x, check its value If x is greater than 5 and less than or equal to 10, print x Prompt for and read temperature as input (type double) If temp is 90 or higher, print “It’s too hot!” If temp is 32 or lower, print “It’s freezing!” In all other cases, print “It’s okay” Read 3 int values and print error if input problem Values are separated by a comma If fewer than 3 values read, print error message with number of values Example: Error: only 2 inputs read correctly 2/16/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Example 1 solution Given int x, check its value If x is greater than 5 and less than or equal to 10, print x if ((x > 5) && (x <= 10)) printf(“%d\n”, x); 2/16/2018 ECE Application Programming: Lecture 11

Example 1 solution (cont.) Prompt for and read temperature as input (type double) If temp is 90 or higher, print “It’s too hot!” If temp is 32 or lower, print “It’s freezing!” In all other cases, print “It’s okay” int main() { double temp; printf(“Enter temperature: “); scanf(“%lf”, &temp); if (temp >= 90) printf(“It’s too hot!\n”); else if (temp <= 32) printf(“It’s too cold!\n”); else printf(“It’s okay\n”); return 0; } 2/16/2018 ECE Application Programming: Lecture 11

Example 1 solution (cont.) Read 3 int values and print error if input problem Values are separated by a comma If fewer than 3 values read, print error message with number of values Example: Error: only 2 inputs read correctly int main() { int x, y, z; // Input values int num; // # values read num = scanf(“%d,%d,%d”, &x, &y, &z); if (num < 3) printf(“Error: only %d inputs read correctly”, num); return 0; } 2/16/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 3 switch statements Nesting several if/else if statements can get tedious If each condition is simply checking equality of same variable or expression, can use switch 2/16/2018 ECE Application Programming: Lecture 3

switch/case statement - General form switch ( <expression> ) { case <value1> : <statements> [ break; ] case <value2> : <statements> [ break; ] : [ default: <statements> [ break; ] ] } 2/16/2018 ECE Application Programming: Lecture 3

switch/case statement Check if <expression> matches any value in case statements If <expression> == <value1>, execute <statements> in that case If <expression> == <value2>, execute <statements> in that case If <expression> does not equal any of the values, go to default case (if present) 2/16/2018 ECE Application Programming: Lecture 11

Switch statements and break Each case is just a starting point—switch does not automatically skip other cases! Example: switch (x) { case 0: x = 3; case 1: x = x * 4; default: x = x – 1; } If x == 0: Start at case 0  x = 3; Then, go to case 1  x = x * 4 = 3 * 4 = 12 Then, go to default:  x = x – 1 = 12 – 1 = 11 2/16/2018 ECE Application Programming: Lecture 11

Switch statements and break Use break to exit at end of case You may not always want to use break—will see examples later Rewriting previous example: switch (x) { case 0: x = 3; break; case 1: x = x * 4; default: x = x – 1; } 2/16/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Next time PE2: Conditional statements Exam 1 Review Monday Be prepared to ask questions! 2/16/2018 ECE Application Programming: Lecture 11