Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.

Similar presentations


Presentation on theme: "Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence."— Presentation transcript:

1 Chapter 6

2  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

3  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.

4  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

5  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.

6

7  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’

8  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’

9  EXAMPLES:   int J = 0;  do{  cout<< J;  J = J + 1;  }while( J <= 4);   int J = 0;  while(J<=4)  { cout<< J;  J = J + 1;  } 

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

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

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

13  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 No Print C

14  #include  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;  }

15  Draw a flowchart for computing factorial N(N!) Where N! = 1 x 2 x 3...N start Read N M=1 F=1 F=F*M Is M = N? M=M+1 Print F end No Yes

16  #include  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;  }


Download ppt "Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence."

Similar presentations


Ads by Google