IPC144 Introduction to Programming Using C Week 3 – Lesson 2

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CS0004: Introduction to Programming Repetition – Do Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Repetition statements
Identify the Appropriate Method for Handling Repetition
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
while Repetition Structure
CS 1430: Programming in C++ No time to cover HiC.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 5: Control Structure
Week 4 – Repetition Structures / Loops
Week 4 – Chapter 3 Repetition.
REPETITION STATEMENTS
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Topic 5 for Loops -Arthur Schopenhauer
Programming Fundamentals
Iterations Programming Condition Controlled Loops (WHILE Loop)
Week 8 - Programming II Today – more features: Loop control
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
CS1100 Computational Engineering
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Module 4 Loops.
Visual Basic – Decision Statements
REPETITION STATEMENTS
Control Structures Part 1
Looping III (do … while statement)
Alternate Version of STARTING OUT WITH C++ 4th Edition
A LESSON IN LOOPING What is a loop?
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
EPSII 59:006 Spring 2004.
Loops.
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
ECE 103 Engineering Programming Chapter 18 Iteration
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Relational Operators Logical Operators for Loops.
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
The while Looping Structure
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Week 3 – Repetition (ctd.)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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