IPC144 Introduction to Programming Using C Week 3 – Lesson 1 (Pages 26 to 37 in IPC144 Textbook)
Agenda Additional C programming Elements Take-up REALITY CHECK (Q3/4 – Week 2 Lesson1) Take up REALITY CHECK (Q3 – Week 2 Lesson 2) Logical operators (&& and ||) DeMorgan's law using script command (to generate typescript) switch/case statement Examples Introduction to looping
Take-up Homework REALITY CHECK (Week 2 – Lesson 1) Questions #3 & #4
Take-up Homework REALITY CHECK (Week 2 – Lesson 2) Question #3
Logic Relational Operators The simplest kinds of conditions to test in if statements involve the comparison of numeric amounts (using relational operators). Relational Operators > Greater than >= Greater than or equal to < Less than <= Less than or equal to == equal to != Not equal to
Logic Compound Relation Operators Sometimes a simple condition, such as a single comparison, is not sufficient to control an if statement or a while statement. In these situations, it is usually a combination of circumstances that determine what code should be executed. Relational Operators && true if, and only if, both of the sub-conditions are true. || true if either (or both) of the sub-conditions are true.
Logic Compound Relation Operators Example: if ( x < y && x < z ) printf (“x is less then y and z\n\n”); if ( x < y || x < z ) printf (“At least y or z is greater than x\n\n”); if ( x < y || x < z && y < z ) printf (“ x is less than y, or x and y is less than z\n\n”);
Logic Compound Relation Operators && ||
Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #1 and #2 Using the handout, plan and then code your program
DeMorgan's Law To get the opposite of a compound condition, you must reverse all the sub- conditions and, at the same time, change all &&s to ||s, and all ||s to &&s.
Switch / Case Statements The switch is an alternative to a nested if statement in certain circumstances. The syntax for switch is: switch (variable) { case 1: statement(s); break; case 2: statement(s);break; ...and so on (as many cases as you like)... default: statement(s) }
Switch / Case Statements Example: int number; printf ("Pick a number (1 or 2): "); scanf ("%d", &number); switch (number) { case 1: printf ("You picked number 1.\n\n"); break; case 2: printf ("You picked number 2.\n\n"); default: printf("You must select either 1 or 2.\n\n"); } Notice a break statement is used to end the execution for a specific case!
Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #3 and #4 Using the handout, plan and then code your program
Loops So far, our programs when involving logic, may do different things under different situations, but another foundation of programming is the ability to repeat. For Example: - Keep prompting user for data until correct data entered - Repeat calculations (eg. exponents: 2 x 2 x 2 x 2 x 2) - Repeat operation until user enters a zero to end entry of data.
Loops There are 2 category of loops: Determinant Loops (for() statement) The number of repetitions are known. For example, repeating display of "Hello" 4 times.... Indeterminant Loops ( while() do-while() statements) The number of repetitions is unknown. Repetitions depend on user input. For example, repeating display of "Hello" until user (when prompted) enters number 0 to exit...
Homework TASK #1 TASK #2 TASK #3 TASK #4 *** Highly Recommended *** Complete lab #2 since it is due at the beginning of this week’s lab! TASK #2 Study for a quiz to be held in this week’s lab based on material taught last week TASK #3 Complete and code the solutions for today’s REALITY CHECK QUESTIONS, as well as “hide” and repeat the walk-thru questions TASK #4 *** Highly Recommended *** Read ahead in IPC144 Programming Notes (especially looping).