Some loop programs 1: Read in 10 integers and output their sum

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
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.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
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.
Chapter 5: Structured Programming
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
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;
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: 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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
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”
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
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.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
 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.
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.
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.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 5: Structured Programming
CSE 220 – C Programming Loops.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Control Structures.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Ch 7: JavaScript Control Statements I.
Iteration statement while do-while
Programming Fundamentals
Control Structures Lecture 7.
Arrays, For loop While loop Do while loop
For & do/while Loops.
Outline Altering flow of control Boolean expressions
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
More Loops Topics Counter-Controlled (Definite) Repetition
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Some loop programs 1: Read in 10 integers and output their sum 2: Read in 10 integers and output the largest of them 3: Keep reading in integers until one is input which is larger than 100

Sum of 10 Integers #Include <stdio.h> main() { int i, a,sum; for (i=0;I < 10; i++) { printf(“enter number \n”); scanf(“%d”,&a); sum = sum + a; }; printf(“total is %d”,sum); }

Largest of 10 Integers #Include <stdio.h> main() { int i, a,max; printf(“enter number \n”); scanf(“%d”,&a); max = a; for (i=1;I < 10; i++) { printf(“enter number \n”); If (a > max) max = a; }; printf(“largest is %d”,max); }

Loop until #Include <stdio.h> main() { int a, do { printf(“enter number \n”); scanf(“%d”,&a); }; while (a < 100) printf(“The number which ended the loop is %d”,a); }

While #Include <stdio.h> main() { int a, a= 0; while (a < 100) { printf(“enter number \n”); scanf(“%d”,&a); }; printf(“The number which ended the loop is %d”,a); }

Iterations and Loops The purpose of a loop is to repeat the same action a number of times We refer to each time an action is repeated as an iteration. We continue repeating the action until a terminating condition is satisfied. This terminating condition is called a loop guard The loop guard is represented by a boolean expression in the same way as the condition of an IF statement. Before the loop starts we initialise variables involved in the loop In C there are a number of ways to represent loops

For loops For loops are a common form of loop particularly for repeating a task a fixed number of times( counting loops) The syntax of a for loop is For (loop initialization, loop guard, increment) statement

Example of for loop for (i = 0; i < 10; i = i + 1) printf("i is %d\n", i); i=0 is the initialization of the loop counter i < 10 is the loop guard i.e. repeat until i is greater or equal to 10 i = i + 1; this is the counter increment ; it is more commonly written i ++ printf("i is %d\n", i); is the loop statement

While loops The most basic loop in C is the while loop. The general syntax of a while loop is while( expression ) statement The expression represents the loop guard. While it is true repeat the statement. Terminate when the expression evaluates to false

Example of a while loop #include <stdio.h> main() {int x x = 2; while(x < 1000) { printf("%d\n", x); x = x * 2; } } This program repeatedly prints out powers of two until we generate a number greater than or equal to 1000 The terminating condition is thus when the power of two equals or exceeds 1000

While Loops Will execute only if condition is true. Condition is tested before execution Do while loops will execute once before testing the condition.

Do While Loop do { } while ( condition ); Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is almost the same as a while loop except that the loop body is guaranteed to execute at least once. A while loop says "Loop while the condition is true, and execute this block of code", a do..while loop says "Execute this block of code, and then continue to loop while the condition is true".

Example #include <stdio.h> main() { int x; x = 0; do { /* "Hello, world!" is printed at least one time even though the condition is false */ printf( "Hello, world!\n" ); } while ( x != 0 ); getchar(); } Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition.

Sample loop questions 1: Write a program to calculate the integer square root of a number e.g. 7 integer square is 2 etc 2: Write a program which continually reads in integers until 45 is read in 3: Use a Do while loop to calculate the minimum of 100 integers being read in.

Read until 45 #include <stdio.h> main() {int x; x =1; while(x != 45) { printf(“enter no”); scanf(“%d", &x); }

Integer Square Root #Include <stdio.h> main() { int i,a; printf(“enter number \n”); scanf(“%d”,&a); If (a < 0) {printf(“No Square Root available”)} else { i= 0; while (i*i < a) {i++}; printf(“The integer square root of %d is %d”,a,i); }

Read until 45 #include <stdio.h> main() {int x; do { printf(“enter no”); scanf(“%d", &x); } while(x != 45) ;

Minimum #include <stdio.h> main() {int x,min,counter; do { printf(“enter no”); scanf(“%d", &x); if (counter= = 0) {min = x;} else {If (x < min) min = x; } counter++;} While (counter < 100) ; printf(“minimum is %d”,min); }

Minimum with while loop #include <stdio.h> main() {int x,min,counter; counter = 1; printf(“enter no”); scanf(“%d", &x); min = x; While (counter < 100) { printf(“enter no”); If (x < min) min = x; counter++;} printf(“minimum is %d”,min); }

More questions 4: Read in 20 integers and count the even numbers 5: Write a program to calculate

Write a program to calculate e = 1/1! + 1/2! + 1/3! Etc Do it for 6 terms

No of even Integers #Include <stdio.h> main() { int i, a,sum; for (i=0;I < 20; i++) { printf(“enter number \n”); scanf(“%d”,&a); If ((a%2)==0)sum = sum + +; }; printf(“total even nos is %d”,sum); }

Sum of function #Include <stdio.h> #Include <math.h> main() { int n, x; float enum,denom,sum; sum = 0.0; printf(“enter number \n”); scanf(“%d”,&X); If (x == 0) printf(“sum not defined”) else { for (n=1;n <= 100; n++) enum = pow(x,n); denom = pow(x,2*n); sum = sum + (enum/denom); }; printf(“total %f”,sum); }

e #Include <stdio.h> #Include <math.h> main() { int fac, n, x; float enum,denom,sum; sum = 0.0; for (n=1;n <= 6; n++) { fac = 1; For (x =1; x <= n; x++){ fac = fac*x; } enum = 1; denom = 1.0/fac; sum = sum + (enum/denom); }; printf(“total %f”,sum);

Sum of function Example #Include <stdio.h> #Include <math.h> main() { int n,i, fac; float enum,denom,sum; sum = 0.0; for (n=1;n <= 100; n++) { fac = 1; for (i=1, i <=n, i++) { fac = fac*i; } enum = n-1; denom = fac*pow(n,2); If (denom == 0) printf(“sum not defined”) sum = sum + (enum/denom); }; printf(“total %f”,sum);

Remember Loops Consist of 1: Initialization 2: Terminating Condition (Guard) 3: Loop Body 4: Terminating Action

For Counted Loops Iterations use a For loop Otherwise use a while or a do while loop