Download presentation
Presentation is loading. Please wait.
1
Lec 6
2
Lecture Outline Compound Assignment Operators
Increment and Decrement Operators Prefix and Postfix Increments Repetition in programs While Loop
3
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;
4
Increment and Decrement Operators
++ is an increment operator that adds 1. -- is a decrement operator that subtracts 1.
5
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
6
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.
7
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
8
While Statement Syntax while (loop repetition condition) statement
while (loop repetition condition) {statements}
9
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
10
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.
11
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
12
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);
13
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; } } }
14
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);
15
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);}
16
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);}
17
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
18
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);}
19
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);}
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.