1 / 14

Understanding Loops in C Programming

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.

Download Presentation

Understanding Loops in C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Agenda Additional C programming Elements • Introduction to looping • Types of loops: • Determinant • Indeterminant • Indeterminant Loops • While() loop • do { } while(); loop

  3. View Homework • REALITY CHECK (Week 3 – Lesson 1) • Questions #1 & #2

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

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

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

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

  8. 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!!!!

  9. Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #1 (word problem) andQuestion #2 (walk-thru)

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

  11. 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…

  12. Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #3 (word problem) andQuestion #4 (walk-thru)

  13. 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….

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

More Related