Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
CS150 Introduction to Computer Science 1
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
More While Loop Examples CS303E: Elements of Computers and Programming.
How to start Visual Studio 2008 or 2010 (command-line program)
Writing Program Code in BASIC Write a program to prompt for and accept values into TWO variables, numx and numy. The program should square the value stored.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Previously Repetition Structures While, Do-While, For.
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.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Count and add list of numbers From user input and from file.
Iterations Very Useful: Ability to repeat a block of code Example:
Homework Assignment #4 J. H. Wang Dec. 3, 2007.
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.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
For loop. Exercise 1 Write a program to have the user input three (3) numbers: (f)rom, (t)o, and (i)ncrement. Count from f to t in increments of i, inclusive.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Algorithm & Flow Charts Decision Making and Looping
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-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
ECE Application Programming
while Repetition Structure
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Repetition-Counter control Loop
Programming Fundamentals
CS1100 Computational Engineering
Lec 7.
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter (3) - Looping Questions.
Loops in C.
CMPT 102 Introduction to Scientific Computer Programming
Computing Fundamentals
Week 6 CPS125.
Week 6 CPS125.
Repetition (While Loop) LAB 9
REPETITION Why Repetition?
Presentation transcript:

Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1) (2) body (3) (2) body (3) (2) body … body (3) (2) * Example for ( (1); (2); (3) ) { // for-repetition body // {} is not necessary // if there is only one statement in body } (2) body TRUE FALSE (1) (3) int main() { for(counter = 1; counter <= 10; counter++ ) printf(“%d\n”,counter); }

1. Write a program that calculates the squares and cubes of the numbers from 0 to 10 and prints the following table of values. (Be careful of the alignment.) number square cube

#include int main() { int n, i; int sum=0; scanf("%d",&n); for (i=1;i<=n;i++) { sum+=i; } printf("%d\n",sum); return 0; } 2.(a) Given an integer number n as a user input, compute and print Sum(n)=1+2+3+…+n #include int main() { } 2.(b) Given an integer number n as a user input, compute and print the product of odd numbers between 1 and n. Assume n is less than 20. Result : 1*3*…*n (if n is odd) 1*3*…*n-1 (if n is even)

#include int main() { int i, j; for (i=1;i<=5;i++) { for (j=1;j<=i;j++) { printf("%d ",i); } printf("\n"); } return 0; } 3.(a) #include int main() { } 3.(b) * * * * * * * * * * * * * * *

4. Write a C program that calculates and prints the total sum and average of 5 floating point values that are taken as user input. #include int main() { } Input example Total Sum = Average = output example