Week 6 CPS125.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
do - while  while: Execute the body of the loop at least once
Nested LOOPS.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Iterations Very Useful: Ability to repeat a block of code Example:
Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Repetition statements
Lesson #5 Repetition and Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lesson #5 Repetition and Loops.
Chapter 5: Control Structures II
Loop Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
REPETITION STATEMENTS
CS1010 Programming Methodology
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Repetition-Counter control Loop
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming Fundamentals
Control Structures Lecture 7.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
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.
The while Looping Structure
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
Lec 7.
Loops in C.
The while Looping Structure
UMBC CMSC 104 – Section 01, Fall 2016
REPETITION STATEMENTS
A LESSON IN LOOPING What is a loop?
CPS125 Week
Week 6 CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
Lesson #5 Repetition and Loops.
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
The while Looping Structure
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Week 6 CPS125

The while Loop condition is true */ Another type of looping structure Application: when we don't know how many times a loop is going to be repeated. Any FOR LOOP can be re-written as a WHILE LOOP, but the other way around is not necessary true. General Syntax: while ( condition ){ /* BODY OF WHILE LOOP statements executed/repeated if condition is true */ } Explanation: As long as the condition is true, the body of the while loop will be executed. If the condition is false, the statement after the Body of while-loop will be executed.

The while statement The while statement permits the repetition of the statements until the condition becomes false.

Counting loop int n; n = 1; /* initialization */ while (n <= 100) /* condition */ { printf ("%d ", n); /* body */ n = n + 1; /* update */ }

Execution of while In a while loop, the initialization is performed once, then the condition is checked. If the condition is true, the body and update statements are executed and the condition is then checked again. If the condition is false, the loop ends and the program continues to the next statement. init Condition update body

for vs. while The for loop is used when we know, A PRIORI, the number of iterations (counting loops). The while loop is used when we don't. This is a style issue only. We know that the for statement works exactly like the while statement.

Write a Program!!!!!!! TO PRINT INTEGER NUMBERS FROM 1 to 100 USING WHILE TO PRINT INTEGER NUMBER FROM 1 TO 100 USING FOR LOOP

/* PROGRAM # 29 */ /* TO PRINT INTEGER NUMBERS FROM 1 to 100 USING WHILE*/ #include <stdio.h> #define NUMBER 100 main( ){ int counter= 1; /*Print the numbers 1 through 100 */ while(counter <= NUMBER){ printf("Value of counter = %d\n", counter); counter++; } printf("The value of cnt after the while loop is over: %d\n", counter);

/* PROGRAM # 30*/ /*TO PRINT INTEGER NUMBER FROM 1 TO 100 USING FOR LOOP*/ #include <stdio.h> #define NUMBER 100 main( ){ int counter; /* Print the numbers 1 through 100 */ for(counter= 1; counter <= NUMBER; counter = counter + 1) printf("Counter is %d. \n", counter); printf(“The counter after the loop is finished: %d.\n”, counter); } Q) What is the difference between these 2 programs?

Home Work!!! GET INTEGER NUMBERS AND ADD THEM DO THIS AS LONG AS THE NUMBER ENTERED IS POSITIVE

REVIEW Use a loop to repeat steps in a program. two kinds of loops occur frequently in programming: counting loops and sentinel-controlled loops. For a counting loop, the number of iterations required can be determined before the loop is entered. For a sentinel-controlled loop, repetition continues until a special data value is scanned.

The while Loop condition is true */ Another type of looping structure Application: when we don't know how many times a loop is going to be repeated. Any FOR LOOP can be re-written as a WHILE LOOP, but the other way around is not necessary true. General Syntax: while ( condition ){ /* BODY OF WHILE LOOP statements executed/repeated if condition is true */ } Explanation: As long as the condition is true, the body of the while loop will be executed. If the condition is false, the statement after the Body of while-loop will be executed.

The do while loop Similar to while-loop However, The WHILE LOOP tests the loop condition at the top of the loop but The do while loop tests the condition at the end of the loop Therefore, regardless of the condition, the body of the DO WHILE LOOP is executed at least once. General Syntax: do{ statement; … }while (condition); As long as the condition is true, the body of the loop will be repeated.

Input validation loop using do-while int n; do { printf ("Enter a number not between 1 and 5"); scanf ("%d", &n); }while (n < 1 || n > 5); /* rest of program */ ...

Q? TO PRINT INTEGER NUMBERS 1 THROUGH 100 USING DO WHILE LOOP.

/* PROGRAM # 33 */ /* TO PRINT INTEGER NUMBERS 1 THROUGH 100 USING DO WHILE LOOP */ #include <stdio.h> #define NUMBER 100 main( ){ int counter=1; /*Print numbers until number is greater than 100*/ do{ printf("counter is %d\n", counter); counter++; }while(counter <= NUMBER); printf("The value of the counter after the loop is over: %d.\n", counter); } Q) What is the value of counter after the loop is over?

Execution of do-while init Body condition update In a do-while loop, the initialization is performed once, then the body and update statements are executed, and finally the condition is checked. If the condition is true, the body and update are executed once more. If the condition is false, the loop ends and the program continues to the next statement. init Body condition update

/*For while-2 */ #include <stdio.h> void main(void) { int i=4, j=1; do printf("old_i=%2d, ",i); i--; printf("new_i=%2d\n",i); } while(i>0); do ++j; while(j>999); printf("j=%2d\n",j); }

/*For Lesson for-2 */ #include <stdio.h> void main(void) { int i, j, k, m=0; for (i=1; i<=5; i+=2) { for (j=1; j<=4; j++) k = i+j; printf("i=%3d, j=%3d, k=%3d\n", i, j, k); } m=k+i; printf("i=%3d, j=%3d, k=%3d, m=%3d \n", i, j, k,m);

Nested Loops A nested loop is a loop within a loop, an inner loop within the body of an outer one. The inner and outer loops need not be generated by the same control structure (one can be a while loop, the other a for loop).

Nested Loops The inner loop must be completely embedded in the outer loop (no overlaps). Each loop must be controlled by a different index (loop control variable). i and j are often used and sometimes the outer loop (big loop) is called the i-loop and the inner loop (little loop) the j-loop.

A Nested Loop Example Suppose we want to display a 12x12 multiplication table. A nested loop here will make perfect sense. We can use the outer loop for the multiplicand and the inner loop for the multiplicator. The idea is to loop the outer loop 12 times and for each value, loop the inner loop 12 times as well.

A Nested Loop Example #include <stdio.h> int main (void)‏ { int i, j; for (i = 1; i <= 12; ++i)‏ for ( j= 1; j <=12 ; ++j)‏ printf ("%4d", i * j); printf ("\n"); } return (0);

What will be the exact output of this program? #include <stdio.h> int main (void) { int a,b=3; double x=5.0, y; for (a=1; a<b; ++a) { while (b>0){ b=b-a; printf ("%5d%4d\n",a,b); } x=x-b; y=b%a; printf ("%7.2f%8.3lf\n", x,y);} printf ("The sum is: %3.1lf\n",a+b+x+y); return (0); }

1 2 1 1 1 0 5.00 0.000 The sum is: 7.0

Write a complete C program that asks the user for an integer n Write a complete C program that asks the user for an integer n. If the number is positive, the summation of all numbers between 1 and n is displayed on the screen (1+2+3+...+n). If the number is not positive, you must ask the user again for another number.

#include <stdio.h> int main (void){ int n, i, sum=0; do { printf ("Enter a positive number: "); scanf ("%d", &n); } while (n<=0); for (i=1; i<=n; ++i) sum = sum + i; printf ("%d", sum); return (0); }