Lec 6.

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Chapter 5 Repetition and Loop Statements Instructor: Alkar & Demirer.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS150 Introduction to Computer Science 1
CS 201 Selection Structures (2) and Repetition
Chapter 5 (Loop Statements)
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
1 ICS103 Programming in C Ch5: Repetition and Loop Statements.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Overview Go over parts of quiz? Another iteration structure for loop.
Program Looping Why we need loops in our code –Make code concise for repetitive processes When to use loops –Run a block of code repetitively –Process.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 5: Repetition and Loop Statements By: Suraya Alias.
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.
CISC105 – General Computer Science Class 4 – 06/14/2006.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
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.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
CHAPTER 4 REPETITION STRUCTURES 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Repetition statements
Lecture2.
Lesson #5 Repetition and Loops.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
CHAPTER 6: REPETITION AND LOOP STATEMENTS
ICS103 Programming in C Lecture 7: Repetition Structures
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
Lesson #5 Repetition and Loops.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
REPETITION STATEMENTS
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.
Chapter 5: Loops and Files.
Repetition.
Chapter 4 Repetition Structures
Chapter 4 Repetition Structures
Lesson #5 Repetition and Loops.
Lec 7.
Lec 5.
Repetition and Loop Statements
Chapter 8 The Loops By: Mr. Baha Hanene.
Control Statements Loops.
Chapter 6: Repetition Statements
Repetition and Loop Statements
Lecture3.
Week 6 CPS125.
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Computer programming Lecture 3.
CPS125 Week
Week 6 CPS125.
Lesson #5 Repetition and Loops.
Repetition Statements (Loops) - 2
Control Statements Loops.
Lec 6 Loop Statements Introduction to Computer Programming
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Lec 6

Lecture Outline Compound Assignment Operators Increment and Decrement Operators Prefix and Postfix Increments Repetition in programs While Loop

Compound Assignment Operators counter=counter+1; time=time-1; t_time=t_time+time; sum=sum+next; n=n*(x+1); r=r/10; counter+=1; time-=1; t_time+=time; sum+=next; n*=x+1; r/=10;

Increment and Decrement Operators ++ is an increment operator that adds 1. -- is a decrement operator that subtracts 1.

Prefix and Postfix Increments ++n First add then do command. n++ First do command then add. --n First subtract then do command. n-- First do command then subtract. Example n = 4 • printf("%i", ++n); printf("%i", n++); printf("%i", --n); printf("%i", n--); 5 4 3 4

Repetition in programs What’s a loop? A control structure that repeats a group of steps in a program. What’s a loop body? The statements that are repeated in the loop. What’s a loop control variable? The variable whose value controls loop repetition. Its value determines whether the loop body is repeated.

Do you need to repeat any steps? Repetition in Programs no Do you need to repeat any steps? No loop required yes Do you Know how many times to repeat? Use one of the following : sentinel-controlled loop endfile-controlled loop input validation loop general conditional loop no yes Use a counting loop

While Statement Syntax while (loop repetition condition) statement while (loop repetition condition) {statements}

Example /* A program that print the numbers from 1 to 10*/ #include<stdio.h> int main(void) { int counter; counter=1; while (counter<=10) printf("%i\n", counter); counter++; } return(0); loop control variable loop repetition condition loop body

Loop Control Variables The loop control variable (i.e counter) MUST BE: 1. Initialized before entering the loop. 2. Tested. 3. Updated inside the loop.

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

Example /* A program that prints 5 stars*/ #include<stdio.h> int main(void) { int counter; counter=0; while (counter<=4) printf("*"); counter++; } return(0); /* A program that prints 5 stars*/ #include<stdio.h> int main(void) { int counter; counter=1; while (counter<=5) printf("*"); counter++; } return(0);

Three Loops ( Each Print 3 Stars) c = 0; c = 1; c = 0; while (c <= 2) while (c <= 3) while (c < 6) { { { printf("*"); printf("*"); printf("*"); c++; c++; c = c + 2; } } }

Computing The Sum of Three Numbers #include<stdio.h> int main(void) { int next, sum; sum=0; scanf("%i", &next); sum+=next; printf("%i\n", sum); return(0); } #include<stdio.h> int main(void) { int next, sum, count; sum=0; count=1; while (count<=3) scanf("%i", &next); sum+=next; count++; } printf("%i\n", sum); return(0);

Example /* A program that prints the payroll of 8 employees */ #include<stdio.h> int main(void) {int count_emp=0; float hours, rate, pay; while (count_emp<=7) {printf("Please enter the hours: "); scanf("%f", &hours); printf("Please enter the rate: "); scanf("%f", &rate); pay=hours*rate; printf("The pay is $%.2f\n", pay); count_emp++;} printf("\nAll employees processed\n\n"); return(0);}

Exercise 1 Write a program that displays the even numbers from 0 to 10: #include<stdio.h> int main(void) {int even=0; while (even<=10) {printf("%i \n", even); even+=2;} return(0);}

Exercise 2 Write a program that produces the following output: 0 1 1 2 0 1 1 2 2 4 3 8 4 16 5 32 6 64

Solution /* a program to display the output as it appears on the right*/ #include<stdio.h> int main (void) {int i=0, product=1; while (i <=6) {printf("%i %i\n", i, product); product*=2; i++;} return(0);}

Example (While Statement) /* a program that displays the multiple of 5 from 0 to 20 */ #include<stdio.h> int main(void) {int mult; mult=5; while (mult<=20) {printf("%i\n", mult); mult+=5;} return(0);}