Lec 7.

Slides:



Advertisements
Similar presentations
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Advertisements

1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
The Repetition control structure using while loop.
Computer Programming -1-
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.
Repetition statements
Lesson #5 Repetition and Loops.
Control Structures (Repetition structure) Jump Statements
REPETITION CONTROL STRUCTURE
ECE Application Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Lecture 7: Repeating a Known Number of Times
Lesson #5 Repetition and Loops.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
The nested repetition control structures & Continue Statements
Repetition.
Topics Introduction to Repetition Structures
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Control Structures Lecture 7.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Lesson #5 Repetition and Loops.
Lec 5.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Chapter 8 The Loops By: Mr. Baha Hanene.
Lec 6.
Week 6 CPS125.
Looping III (do … while statement)
Alternate Version of STARTING OUT WITH C++ 4th Edition
Week 6 CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
Topics Introduction to Repetition Structures
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
Lesson #5 Repetition and Loops.
Control Statements Loops.
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
Chapter 4 Repetition Structures
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:

Lec 7

The do-while Statement Syntax: do {statements} while (loop repetition condition);

Example /* a program that prints the numbers from 1 to 10 */ #include<stdio.h> int main (void) { int counter=1; do printf("%i\n", counter); counter++; } while (counter<=10); return(0);

Example /* A program that computes the square root of a positive number, the program keeps prompting the user to enter a positive number until he enter it correctly. */ #include<stdio.h> #include<math.h> int main(void) {float num, y; do {printf("Enter a positive number: "); scanf("%f", &num);} while (num<0); printf( "The square root of %.2f is %.2f\n", num, y=sqrt(num)); return(0);}

Types of loops conditional loop counter loop sentinel loop 1.Reading an unknown amount of input from the user. This is the most flexible way to read data from the user. The program will be able to accept any amount of input from the user. 2.Validating input. If it is necessary to verify that a user's input falls within a certain range, then a sentinel loop is required. It is not known ahead of time how many times the user will enter an invalid number. Conditional loops have common traits with sentinel and count loops. They are like sentinel loops in that it is unknown before time how many times they will repeat. They are like counter loops in that they terminate as the result of a calculation, instead of based upon user input. 1.The loop always repeats the same number of times. 2.The program calculates the number of repetitions based upon user input.

Counter Loop /* a program to type the subtraction of two numbers until a condition is false */ #include <stdio.h> int main(void) {int i, j; for(i=1, j=10 ; i<=5 ; i++, j--) printf("%i + %i = %i\n", i, j, i+j); return 0;} 1 + 10 = 11 2 + 9 = 11 3 + 8 = 11 4 + 7 = 11 5 + 6 = 11

Sentinel Loop /* a program to count the even numbers until a condition is false */ #include <stdio.h> int main(void) {int value, totalEven; totalEven=0; do {printf("Enter a list of numbers in the range (0-10)(any other number to quite): "); scanf("%i", &value); if (value%2==0) totalEven++;} while(value>=0 && value<=10); printf("The number of even numbers is %i\n", totalEven); return 0;}

Conditional Loop #include <stdio.h> int main(void) {int year, pop; year=1; pop=9870; /* initialization */ while (pop<=30000) /* testing */ {printf("Year#%i Population = %i\n", year, pop); year++; pop=pop+(pop*.10);} /* updating */ printf("The population will surpass 30,000 during the %ith year\n", year); return(0);}

Review of Syntax The for loop: for ( initialization ; loop condition ; update) {statements} The while loop: intialization; while (loop condition) {statements; update;} The do while loop: do

Loops C provides three statements for implementing loops: while, for, and do-wile. Use the for to implement counting loops. Use the do-while to implement loops that must execute at least once, such as data validation loops for interactive programs. Code other conditional loops using the for or while, using whichever implementation is clearer.

Nested Loops Nested loops consist of an outer loop with one or more inner loops. For each outer loop repeated, all repetitions of the inner loop are repeated.

Nested loops for (initialization;testing;update) Examples { statements; initialization; while (testing) update; } Examples for (initialization;testing;update) { statements; for (initialization;testing;update){ statements }

Exercise Modify this program to display the following output: ***** #include<stdio.h> #define columns 5 int main(void) {int j; for (j=1; j<=columns ; j++) printf("*"); printf("\n"); return(0);} #include<stdio.h> #define rows 4 #define columns 5 int main(void) {int i,j; for (i=1; i<=rows; i++) {for (j=1; j<=columns ; j++) printf("*"); printf("\n");} return(0);}