The Repetition control structure using while loop.

Slides:



Advertisements
Similar presentations
Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops.
Understanding Loops Using C Language Week 15 Mr.Omer Salih.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Some loop programs 1: Read in 10 integers and output their sum
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
Repetition (Loops) Want to do some repetitive sequence of actions: print vertical line of *s * Corresponding program: printf(“*\n”);
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
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.
 An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Control Statements (Decision Making)
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
1. Agenda for loop Short-handed notation How to use break and continue 2.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
Iterations Very Useful: Ability to repeat a block of code Example:
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;
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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.
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”
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
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.
Algorithm & Flow Charts Decision Making and Looping
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Computer Programming -1-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 9 Repetition.
Control Structures (Repetition structure) Jump Statements
WHILE, DO-WHILE AND FOR LOOPS
REPETITION CONTROL STRUCTURE
ECE Application Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Quick Test What do you mean by pre-test and post-test loops in C?
Iteration statement while do-while
Control Structures Lecture 7.
Looping.
Chapter 5 Repetition.
Lec 7.
Chapter 4 - Program Control
Loops in C.
Chapter 2.1 Repetition.
CMPT 102 Introduction to Scientific Computer Programming
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements (Loops) - 2
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Week 3 – Repetition (ctd.)
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

The Repetition control structure using while loop

Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied, i.e., loops are used in performing repetitive work in programming. Suppose you want to execute some code/s 10 times. You can perform it by writing that code/s only one time and repeat the execution 10 times using loop. There are 3 types of loops in C programming: – for loop – while loop – do...while loop Loop

While loop just as for loop In for loop initialize, condition & inc\dec define in same line But in while loop define separately While loop???

while (test expression) { statement/s to be executed; } Syntax of while loop

Flowchart

\*print 1 st 10 whole number *\ int a=0; while(a<=10) { printf(“%d”,a); a++; } Example

Syntax of do-while loop do { statement/s to be executed; } while (test expression) ;

Flowchart

The type of loop where the number of execution of the loop is unknown, is termed by sentinel controlled loop. In this case, the value of the control variable differs within a limitation and the execution can be terminated at any moment as the value of the variable is not controlled by the loop. The control variable in this case is termed by sentinel variable. Sentinel controlled loop

Example \*print 1 st 10 whole number *\ int a=0; do { printf(“%d”,a); a++; } while(a<=10);

The following do....while loop is an example of sentinel controlled loop. int num =0; do { printf(“Input a number.\n”); scanf("%d", &num); } while(num>0); Example

The differences between the counter and sentinel controlled loops are as follows

No.TopicsCounter controlled loop Sentinel controlled loop 01Number of executionPreviously known number of execution occurs. Unknown number of execution occurs. 02Condition variableCondition variable is known as counter variable. Condition variable is known as sentinel variable. 03Value and limitation of variable The value of the variable and the limitation of the condition for the variable both are strict. The limitation for the condition variable is strict but the value of the variable varies in this case. 04Examples= = = = sum = 0; n = 1; while (n <= 10) { sum = sum + n*n; n = n+ 1; } = = = = = = = do { printf(“Input a number.\n”); scanf("%d", &num); } while(num>0); = = =

Write a program to reverse the number for example if user enter 123 as input then 321 is printed as output. Program task1….

#include int main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d", &n); while (n != 0) { reverse = reverse * 10; reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %d\n", reverse); }

Program task2…. Write a program to find a palindrome number. A palindrome number is a number such that if we reverse it, it will not change For example some palindrome numbers examples are 12321, 121, 212, 12321, -454.

#include void main() { int n, reverse = 0, temp; printf("Enter a number to check if it is a palindrome or not\n"); scanf("%d",&n); temp = n; while( temp != 0 ) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10; } if ( n == reverse ) printf("%d is a palindrome number.\n", n); else printf("%d is not a palindrome number.\n", n); }

Program task3…. Write a program that computes the sum of the following series using the while loop /2 + 1/4 + 1/6 + 1/8 + ……….+ 1/100

#include void main() { float n, s; n=2.0; s=1.0; while(n<=100) { s = s + 1.0/n; n = n + 2; } printf(“sum of series = %d”,s ); getch(); }

Program task4…. Write a program that computes the sum of the following series using the do while loop /3 + 1/5 + ……….+ 1/199

#include void main() { float n, s; n=1.0; s=0.0; do { s = s + 1/n; n = n + 2; } while(n<=99); printf(“sum of series = %d”,s ); getch(); }