140 likes | 155 Views
Learn about looping concepts in C programming, including types of loops and their syntax. Practice using determinant and indeterminant loops with examples. Explore while and do-while loop structures. Get ready for coding tasks and quiz preparation.
E N D
Introduction to Programming Using C Week 3 – Lesson 2 (Pages 26 to 37 in IPC144 Textbook) IPC144
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 Notice that while statementdoes 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 inputfrom a user, if this statement is missingthe value of guess would never change,the condition would be TRUE, and theprogram would loop forever!!!!
Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #1 (word problem) andQuestion #2 (walk-thru)
Loops Do - While loop syntax: Statement(s) loop at leastonce, then condition is tested… Useful like promptinguser for answer beforedeciding to loop because data isinvalid…. do{ statement(s);}while (condition); Notice with do-while loopstructure, while appear at theend of the code block andends 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 approachwith just the while statementin slide #8…
Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #3 (word problem) andQuestion #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 conditionis true. For example, subsequentattempts….
Homework • TASK #1 • 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).