CS 108 Computing Fundamentals Notes for Thursday, February 12, 2015.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 4: Making Decisions.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
C++ for Engineers and Scientists Third Edition
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
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.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Flow of Control Part 1: Selection
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
CS 108 Computing Fundamentals Notes for Tuesday, February 10, 2015.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
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.
CS 108 Computing Fundamentals Notes for Thursday, September 17, 2015.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CS 108 Computing Fundamentals Notes for Tuesday, September 22, 2015.
CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CS 108 Computing Fundamentals Notes for Tuesday, March 22, 2016.
A First Book of C++ Chapter 4 Selection.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
CNG 140 C Programming (Lecture set 3)
Chapter 3 Control Statements
Chapter 4 C Program Control Part I
Decisions Chapter 4.
Chapter 4 (Conditional Statements)
EGR 2261 Unit 4 Control Structures I: Selection
CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017
CS 108 Computing Fundamentals Notes for Tuesday, September 19, 2017
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Control Statement Examples
Compound Assignment Operators in C++
Chapter 7 Conditional Statements
Switch Case Structures
Presentation transcript:

CS 108 Computing Fundamentals Notes for Thursday, February 12, 2015

switch … case Statement. Simple if has only one action part, and if … else statement has only two action parts... else if can be messy. Often in life three or more cases of actions can be used, based on the condition. Use either nested else if statements or use a switch … case statement

switch … case Statement. switch (decision criteria or condition) { case Label_1 : Action Block 1; // decision criteria must be break; // data type int/char only case Label2 : // Labels must be integer Action Block 2; // constants only… if you break; // remember the ASCII … // table then you'll know that default : // catch-all // characters are included as Action Block else; // integer constants break; }

switch … case Statement.

switch … case Example Complete example at this link:

Relational Operators Operator Meaning = =Equal to ! =Not Equal to <Less Then < =Less Then or Equal to >Greater Then > =Greater Then or Equal to Note difference between = and ==

Relational Operators Example : if ( choice = 2) { answer = perimeter_cal (base, height); printf(" The perimeter is %f ", answer ); } Note the condition above is incorrect… compare: if ( choice = = 2) { answer = perimeter_cal (base, height); printf(" The perimeter is %f ", answer ); } The variable choice is assigned the value of is not 0 therefore this condition is ALWAYS evaluated as TRUE (never FALSE)

Relational Operator Demo (1) Let’s develop a short program that prompts the user to enter two numbers, and once the numbers are entered the program determines (tests) which of the two numbers is larger. After the determination is made (as to which is larger), a message is printed to the screen indicating the result of the test. Start with an algorithm!!

1.Prompt user to provide a number (number_1) 2.Record the user's input for number_1 3.Prompt user to provide another number (number_2) 4.Record user's input for number_2 5.Test to see if the the numbers are unequal a.If test is true i.Test to see if number_1 is greater than number_2 1)If test is true a) Inform the user that number_1 is greater than number_2 b)Go to step 6. 2) If test is false (else) a)Inform the user that number_2 is greater than number _1 b)Go to step 6. b.If test is false (else) i.Inform the user that number_1 and number_2 are equal ii.Go to step 6. 6.Terminate Now Lets Code It!! A Possible Algorithm, Not "The" Algorithm

Relational Operator Demo (2) #include // My file 3.c… also demonstrates "nested" int main (void) // if statements just to do it { float num1, num2; printf("\n Enter a number: "); scanf("%f", &num1); printf("\n Enter a second number: "); scanf("%f", &num2); if (num1 != num2) { if (num1 > num2) printf("\n The first number s greater than the second.\n\n\n"); else printf("\n The second number is greater than the first.\n\n\n"); } else printf("\n The numbers you entered are equal.\n\n\n"); return 0; }

Relational Operator Demo (3) #include // My file 3a.c… condition evaluation issues int main (void) { float num1, num2; printf("\n Enter a number: "); scanf("%f", &num1); printf("\n Enter a second number: "); scanf("%f", &num2); if ( 0 ) // After running add a ! before ( 0 ) and 0 { // and rerun if (num1 > num2) printf("\n The first number s greater than the second.\n\n\n"); else printf("\n The second number is greater than the first.\n\n\n"); } else printf("\n The numbers you entered are equal.\n\n\n"); return 0; }

Relational Operator Demo (4) #include // My file 3b.c… demonstration of // condition evaluation resultant values int main (void) { float answer; answer = 1 < 4 && 4 < 7 ; printf("\n\n Answer: %f ", answer ) ; answer = ! (1 < 3) || ( 2 < 4 ) ; printf("\n\n Answer: %f ", answer ) ; answer = ! ( (1 < 3) || ( 2 < 4 ) ) ; printf("\n\n Answer: %f ", answer ) ; answer = ! (372) ; printf("\n\n Answer: %f \n\n\n", answer ) ; return 0; }

Relational Operator Demo (5) #include // My file 3c.c… demonstration of // condition evaluation resultant values int main (void) // using data type int { // The result of all relational and Boolean int answer; // logical operations is always an int answer = 1 < 4 && 4 < 7 ; printf("\n\n Answer: %d ", answer ) ; answer = ! (1 < 3) || ( 2 < 4 ) ; printf("\n\n Answer: %d ", answer ) ; answer = ! ( (1 < 3) || ( 2 < 4 ) ) ; printf("\n\n Answer: %d ", answer ) ; answer = ! (372) ; printf("\n\n Answer: %d \n\n\n", answer ) ; return 0; }

Logical Operators OperatorMeaning !NOT (Highest Priority) &&AND ||OR (Lowest Priority) Condition A ! (NOT) Condition A Condition B &&(AND) ||(OR) true false truetrue truetrue false true truefalse falsetrue falsetrue falsetrue falsefalse falsefalse

Compound Logical Expressions Write an if statement that doubles a number if the number is greater than 0 yet does not exceed 25, triples the number if it’s greater than 25 but less than 50, and assigns the value 0 if the previous conditions aren’t met. Hint: use the if … else if … else construction

Compound Logical Expressions if ( (entry > 0) && (entry <= 25) ) { entry = entry * 2; } else if ( (entry > 25) && (entry < 50) ) { entry = entry * 3; { else { entry = 0; }

Compound Logical Expression Demo (1) Let’s develop a program that transforms a numeric grades entered by the user into a letter grade and then displays that letter grade to the screen. What do we do first?

1.Prompt user to enter a test score 2.Record user's input number for test score 3.Test the test score to see if it is less than 65 i.If test is true a.Display a message indicating the grade earned is F b.Go to step 8 4.Test the test score to see if it is greater than or equal to 65 but less than 70 i.If test is true a.Display a message indicating the grade earned is D b.Go to step 8 5.Test the test score to see if it is greater than or equal to 70 but less than 80 i.If test is true a.Display a message indicating the grade earned is C b.Go to step 8 6.Test the test score to see if it is greater than or equal to 80 but less than 90 i.If test is true a.Display a message indicating the grade earned is B b.Go to step 8 7.Display a message indicating the grade earned is A 8.Terminate Now Lets Code It!! A Possible Algorithm, Not "The" Algorithm

Compound Logical Expression Demo (2) #include //My file 4.c int main (void) { float score; printf("\n Enter your test score :"); scanf("%f",&score); if (score < 65) { printf("\n You earned an F.\n\n"); // Braces } else if ( (score >= 65) && (score < 70) ) // No Braces… why printf("\n You earned a D.\n\n"); else if ( (score >= 70) && (score < 80) ) printf("\n You earned a C.\n\n"); else if ( (score >= 80) && (score < 90) ) printf("\n You earned a B.\n\n"); else printf("\n You earned an A.\n\n"); return 0; }

Compound Logical Expression Demo (3) Move the code below, found in the previous program/slide, to a PCF named calc_grade( ) … the letter grade itself shall be printed in the main function, not in calc_grade( ) if (score < 65) else if ( (score >= 65) && (score < 70) ) else if ( (score >= 70) && (score < 80) ) else if ( (score >= 80) && (score < 90) ) else

Let’s Use a Programmer-Created Function #include // My file 5.c char calc_grade (float); // Function prototype int main(void) { float score; char grade; printf("\n Enter your test score: ") ; scanf("%f",&score); grade = calc_grade (score) ; //Function call printf("\n\nYou received a letter grade of %c. \n\n", grade); return ( 0 ); } /* Continued on next slide… read as one contiguous file */

/* Continued from previous slide… read as one contiguous file */ char calc_grade (float in_score) // Function declaration { char out_grade = ' '; // Local variable initialized to if (in_score <65) // a "space" (see ASCII table) out_grade = 'F' ; else if ((in_score >= 65) && (in_score < 70)) out_grade = 'D' ; else if ((in_score >= 70) && (in_score < 80)) out_grade = 'C' ; else if ((in_score >= 80) && (in_score < 90)) out_grade = 'B' ; else out_grade = 'A' ; return ( out_grade ); }

Let’s Use a Programmer-Created Function /* The action of the previous slide accomplished differently */ if (in_score <65) // Notice multiple return statements return ( 'F' ); else if ((in_score >= 65) && (in_score < 70)) return ( 'D' ); else if ((in_score >= 70) && (in_score < 80)) return ( 'C' ); else if ((in_score >= 80) && (in_score < 90)) return ( 'B' ); else //Only one return statement can execute return ( 'A' ); }

Let’s See a switch Statement Example #include /* Let’s calculate a worker’s pay… 6.c*/ int main( void ) { int status = 0; double hours = 0.0; double pay = 0.0; printf("\n Enter the hours worked : "); scanf("%lf",&hours); printf("\n Enter the status (7 for full-timers and 9 for part-timers): "); scanf("%d",&status); switch (status) { case 7: pay = hours * 15.0; // full-time rate is $15 per hour break; case 9: pay = hours * 10.0; // part-time rate is $10 per hour break; default: printf("\n\n\n >>>Wrong Employment Status<<<\n"); break; } printf("\n The payment amount is: %4.2lf\n\n\n", pay); return ( 0 ); }

Remember: Steps to Build a Program What does it need to do (functionality): algorithm Can it be broken down into simpler pieces Code the simpler pieces – Code and test one piece at a time Integrate, test and fix Finished Start with a complete algorithm before writing one line of code Comment as you go "Urban Inside-Out Method"…one step at a time

Pico Shortcuts ctrl-w followed by ctrl-t produces a prompt that asks you on which line you like the cursor to be positioned ctrl-c produces the number of the line on which the cursor is currently positioned ctrl-y moves the cursor up one page ctrl-v moves the cursor down one page

Exam #1 (1) Tuesday, February 17 Chapters 2, 3, Alison video tutorials, Web page tutorials listed in Syllabus, and UNIX tutorial only for closed-book part… up through and including programmer-created functions (PCFs) and GHP #3 for open-book part Use "Quick Check Exercises" and "Review Questions" Use Online Interactive Tests at the following link to prepare: bbrown/onlinet.htm bbrown/onlinet.htm

Exam #1 (2) If you need to take the exam in the Learning Center –Send me an reminding me –Visit the Learning Center ASAP to make sure they are ready for you Let's review the remainder of Exam #1 from Fall 2014 –Do not make the mistake of thinking that your Exam #1 will be a copy of last semester's Exam #1  I'm showing you last semester's exam to give you a flavor of what could be on the exam and to give you an idea of what an exam looks like and what to expect (generally)