chapter 6. control structures are statements that are used to perform repetitive tasks and to make...

Post on 05-Jan-2016

215 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

DECISIONS AND REPETITIONS

Chapter 6

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

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.

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

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.

DO LOOP

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’

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’

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; }  

PROBLEMS:

1) Draw a flowchart to read in and print 5 numbers

2. Draw a flowchart to sum numbers 1 to 5.

1. Given the following input 10, 12, -5, -6, 5, trace the output.

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

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; }

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

#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; }

top related