Presentation is loading. Please wait.

Presentation is loading. Please wait.

IPC144 Introduction to Programming Using C Week 3 – Lesson 1

Similar presentations


Presentation on theme: "IPC144 Introduction to Programming Using C Week 3 – Lesson 1"— Presentation transcript:

1 IPC144 Introduction to Programming Using C Week 3 – Lesson 1
(Pages 26 to 37 in IPC144 Textbook)

2 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

3 Take-up Homework REALITY CHECK (Week 2 – Lesson 1) Questions #3 & #4

4 Take-up Homework REALITY CHECK (Week 2 – Lesson 2) Question #3

5 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

6 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.

7 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”);

8 Logic Compound Relation Operators && ||

9 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

10 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.

11 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)‏ }

12 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!

13 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

14 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.

15 Loops There are 2 category of loops:
Determinant Loops (for() statement)‏ The number of repetitions are known. For example, repeating display of "Hello" 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...

16 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).


Download ppt "IPC144 Introduction to Programming Using C Week 3 – Lesson 1"

Similar presentations


Ads by Google