IPC144 Introduction to Programming Using C Week 3 – Lesson 2 (Pages 26 to 37 in IPC144 Textbook)
Agenda Additional C programming Elements Introduction to looping Types of loops: Determinant Indeterminant Indeterminant Loops While() loop do { } while(); loop
View Homework REALITY CHECK (Week 3 – Lesson 1) Questions #1 & #2
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...
Loops In today’s lesson, we will concentrate on in determinant loops: While loops Loop while condition is true. Loop may not execute if first condition of while loop returns FALSE value Do – while loops Loop executes at least once, and end of loop while statement tests condition for repetition unless test condition returns FALSE value
Loops While loop syntax: While ( condition ) { statement(s); } Notice that while statement does not end with semi-colon (like if / else if / else stmts) While loop syntax: While ( condition ) { statement(s); } If condition tests TRUE, then execute statement(s) in code block. If FALSE, exit while statement…
Loops While loop syntax: WARNING int number=42, guess; printf (“Guess the number: “); scanf (“%d”, &guess); While ( guess != number ) { printf (“\nWrong!Try again: “); scanf (“%d”, &guess); } Since this loop depends on input from a user, if this statement is missing the value of guess would never change, the condition would be TRUE, and the program would loop forever!!!!
Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #1 (word problem) and Question #2 (walk-thru)
Loops Do - While loop syntax: do { statement(s); }while (condition); Statement(s) loop at least once, then condition is tested… Useful like prompting user for answer before deciding to loop because data is invalid…. do { statement(s); }while (condition); Notice with do-while loop structure, while appear at the end of the code block and ends this time with a semi-colon….
Loops Do - While loop Example: int number=42, guess; do { printf (“Guess the number: “); scanf (“%d”, &guess); } while ( guess != number ); Compare this approach with just the while statement in slide #8…
Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #3 (word problem) and Question #4 (walk-thru)
Loops Do - While loop (Neat trick): int number=42, guess; do { printf (“Guess the number: “); scanf (“%d”, &guess); } while (( guess != number ) && printf (“Invalid Number: ”)); && compound operator will have “Invalid Number” appear if condition is true. For example, subsequent attempts….
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 for loops).