Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Introduction to Computing Science and Programming I
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 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.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Introduction to Computer Programming in c
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 6 Repetition Richard Gesick.
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Chapter 2.2 Control Structures (Iteration)
( Iteration / Repetition / Looping )
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Looping.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
Outline Altering flow of control Boolean expressions
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
The while Looping Structure
Presentation transcript:

Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each uses the four parts of a loop a little differently, giving different loop options to the programmer.

Think Possibility 2 The for Statement The for-loop has the following form. for (initialization; condition; update) Statement where expression1 is an assignment expression (called the initial condition), expression2 is a relational, equality, or logical expression (called the test condition), and expression3 is another assignment expression (called the loop operation). All three expressions usually have a common variable.

Think Possibility 3 Everytime C encounters a for statement, it 1.executes the initial condition. 2.If the test condition is false, the for loop ends (it does not execute statement). 3.If the test condition is true, it executes statement. 4.It then executes the loop operation and repeats step 2. Example: for (j = 1; j <= 10; ++j) printf (“Hello!\n”);

Think Possibility 4 PROGRAMMING EXERCISES: 1.Write a C program that prompts the user to enter several integers and calculates the average. 2.Write a C program that prints all even numbers between 1 and Write a C program that prints all odd numbers between 1 and 50.

Think Possibility 5 INFINITE LOOPS Infinite loops are also possible in using for statements. Examples: 1.for (j = 1; ; ++j) printf (“Hello!\n”); In this example, the for statement does not have a test condition. Therefore, there is no condition for the loop to terminate. 2.for (j = 1; j <= 10; ) printf (“Hello!\n”); In this example, the for statement does not have a loop operation. Therefore, the test condition will never become false.

Think Possibility 6 The while Statement The while-loop has the following form. initialization while (condition) { body update } where expression is the conditional expression which represents a relational, equality, or logical expression.

Think Possibility 7 Everytime C encounters a while statement, it 1.determines whether expression is true or false. 2.If expression is true, then C executes statement and control passes back to the beginning of the while loop. If expression is still true, then C executes statement again. This process is repeated while the expression is true. 3.If expression is initially false, then C ignores the remainder of the while statement. Example: sum = 0; counter = 1; while (counter <= 10) { sum += 1; ++counter }

Think Possibility 8 Counting, adding, searching, sorting, and other tasks often involve doing something over and over. The while statement in C is very useful in performing repetitive actions. Example: Write a program to compute 25 using the while statement. Algorithm: 25 = 2 * 2 * 2 * 2 * 2= = multiply 2 to itself 5 times Let i be a counter to monitor how many times the multiplication has taken place. y = 1 (y will eventually hold the final answer)

Think Possibility 9 i = 1 y = y * 2 = 1 * 2 = 2 i = i + 1 = = 2 i = 2 y = y * 2 = 2 * 2 = 4 i = i + 1 = = 3 i = 3 y = y * 2 = 4 * 2 = 8 i = i + 1 = = 4 i = 4 y = y * 2 = 8 * 2 = 16 i = i + 1 = = 5 i = 5 y = y * 2 = 16 * 2 = 32 i = i + 1 = = 6

Think Possibility 10 The algorithm requires that after initializing variables y and i to 1, the statements y = y * 2 i = i + 1 be repeated 5 times or while i is less than or equal to 5.

Think Possibility 11 INFINITE LOOPS The programmer must always make sure that the conditional expression in a while loop must sooner or later become false. Otherwise, the program will enter in an infinite loop. In other words, the loop will not terminate. Example: sum = 0; counter = 1; while (counter <= 10) { sum += 1; } In this example, the conditional expression of the while loop (counter <= 10) will never become false since counter will always be equal to 1.

Think Possibility 12 Exercises: 1.PROGRAM TO COMPUTE PROGRAM TO COMPUTE base exp 3.Write a C program that prompts the user to enter an integer between 5 and 10. The program will then output the number typed in by the user. However, the program should make sure that the number typed in is indeed between 5 and Repeat the previous exercise except that the user is limited to three (3) trials only. If the user exceeds this limit, the program terminates. 5.Write a C program that prompts the user to enter several integers and then outputs the one with the highest value.

Think Possibility 13 The do-while Statement The do statement is a variant of the while statement. Instead of making its test at the top of the loop, it makes it at the bottom. The general format of the do statement is: do statement while (expression) where expression is the conditional expression which represents a relational, equality, or logical expression.

Think Possibility 14 Everytime C encounters a do statement, it 1.executes statement. 2.The program then evaluates expression. If expression is true, then control passes back to the beginning of the do loop and the process repeats itself. 3.If expression is false, then C executes the statement after the do loop.

Think Possibility 15 Example: # include main() { int num; do { printf ("\n\nEnter an integer between 5 and 10 > "); scanf ("%d", &num); if (num 10) { printf ("\n\nInvalid number!!! Please try again."); printf ("\n\nEnter an integer between 5 and 10 > "); scanf ("%d", &num); } } while (num 10); printf ("The number you entered is %d.", num); }

Think Possibility 16 THE break AND continue STATEMENTS To interrupt the normal flow of control within a loop (a while or for loop), the programmer can use either the break or continue statement. The break statement causes an exit from an enclosing loop. In other words, encountering the break statement causes immediate termination of the loop. Program control picks up at the code following the loop. Example: for (ctr = 1; ctr <= 100; ++ctr) { printf (“%d\n”, ctr); if (crt = = 50) break; } This program segment will normally print all numbers between 1 to 100. But because of the break statement, only the numbers between 1 to 50 will be printed.

Think Possibility 17 #include main () { int x; for ( ; ; ) { printf ("\nGuess the secret number > "); scanf (“%d”, &x); if (x = = 5) break; else printf ("\nSorry!!!"); } printf("\n\nCongratulations!!!"); } This program will simply ask the user to continuously enter a number until the input is equal to 5.

Think Possibility 18 The continue statement works in a somewhat similar way to the break statement. But instead of forcing loop termination, continue forces the next iteration for the loop to take place, skipping any code in between. Example: for (ctr = 1; ctr <= 100; ++ctr) { if (x%2 ! = 0) continue; printf (“%d\n”, ctr); } This program segment will print all even numbers between 1 to 100.