chapter 6. control structures are statements that are used to perform repetitive tasks and to make...
TRANSCRIPT
![Page 1: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/1.jpg)
DECISIONS AND REPETITIONS
Chapter 6
![Page 2: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/2.jpg)
Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence of statements either as long as or until a certain condition is true.
For…Next Loop is referred to as a
counter-controlled loop
![Page 3: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/3.jpg)
WHAT IS A COUNTER?
A counter is a numeric variable. How is the counter used in the for..next
loop? The counter is initialized or assigned an
initial value. The value of the counter either increases or decreases after each pass through the loop.
![Page 4: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/4.jpg)
SYNTAX: SYNTAX #1 for( j= a; j<= b; j++) { Statement(s);}
SYNTAX #2 for( j= a; j>= b; j--) { Statement(s);}
j is the counter
Statement(s) execute as long as j is lesser than or equal to b
j is increased by 1 in every execution
j decrements by 1 in every execution
![Page 5: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/5.jpg)
Where: j = the loop variable and a = the initial value of the loop variable.
J is initially assigned the value a. Next, the loop test if j is less than or equal to b when S is positive or if j is greater than or equal to b when S is negative. If so, the program executes the statements, replaces the value of j with the value j+s, and repeats the test.
This process continues until the test fails, at which time the program exits the loop and execute the statement after the loop.
![Page 6: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/6.jpg)
DO LOOP
![Page 7: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/7.jpg)
DO...WHILE LOOP The condition must be true in order for the test
to be passed. Syntax:
do{Statements/s;}while(condition);
Example:do{
cout<<"Enter character:"; cin>>x; }while(x!='x');
Cout and cin executes as long as x is not equal to ‘x’
![Page 8: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/8.jpg)
WHILE LOOP The condition must be true in order for the test to
be passed. Syntax:
while(condition){Statements/s;
}
Example:while(x!='x'){
cout<<"Enter character:"; cin>>x; }
Cout and cin executes as long as x is not equal to ‘x’
![Page 9: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/9.jpg)
MORE EXAMPLES ON DO AND WHILE LOOP
EXAMPLES: int J = 0; do{ cout<< J; J = J + 1; }while( J <= 4); int J = 0; while(J<=4) { cout<< J; J = J + 1; }
![Page 10: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/10.jpg)
PROBLEMS:
1) Draw a flowchart to read in and print 5 numbers
![Page 11: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/11.jpg)
2. Draw a flowchart to sum numbers 1 to 5.
![Page 12: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/12.jpg)
1. Given the following input 10, 12, -5, -6, 5, trace the output.
![Page 13: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/13.jpg)
SEATWORK Draw a flowchart to find the largest of
three numbers A, B and C.start
Read A, B, C
IS B>C
?
IS A>B
?
IS A>C
?
Print B Print C Print A
end
No
Yes
Yes Yes
No
Print C
![Page 14: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/14.jpg)
THE PROGRAM #include<iostream>
using namespace std; int main() { int A, B, C; cout<<"Enter 1st #:"; cin>>A; cout<<"Enter 2nd #:"; cin>>B; cout<<"Enter 3rd #:"; cin>>C; if(A>B) { if(A>C)
cout<<"Highest is
"<<A<<endl; else cout<<"HIghest is
"<<C<<endl; } else{ if(B>C) cout<<"Highest is
"<<B<<endl; else cout<<"Highest is
"<<C<<endl; } system("PAUSE"); return 0; }
![Page 15: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/15.jpg)
Draw a flowchart for computing factorial N(N!) Where N! = 1 x 2 x 3...N
start
Read N
M=1F=1
F=F*M
Is M = N?
M=M+1
Print F end
No
Yes
![Page 16: Chapter 6. Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence](https://reader031.vdocuments.site/reader031/viewer/2022013101/56649f315503460f94c4c255/html5/thumbnails/16.jpg)
#include<iostream>
using namespace std; int main() { int N,F,M; M=F=1; cout<<"Enter #:"; cin>>N; for(M=1; M<=N; M++){ F=F*M; if(M==N)cout<<F<<endl; } system("PAUSE"); return 0; }