Flow of control ppt

7,769 views
7,348 views

Published on

Computer Science

Published in: Education, Technology, Business
2 Comments
10 Likes
Statistics
Notes
No Downloads
Views
Total views
7,769
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
582
Comments
2
Likes
10
Embeds 0
No embeds

No notes for slide

Flow of control ppt

  1. 1. - Made by SHRABANTI PROGRAMING IN
  2. 2. <ul><li>Every C++ program must have at least one function called main(). </li></ul><ul><li>When we run a C++ program, the first statement executed will be </li></ul><ul><li>at the beginning of main() and the last statement at the end of </li></ul><ul><li>main(). Therefore, the main() is also known as driver function as </li></ul><ul><li>it drives the program. </li></ul><ul><li>The Von-Neumann architecture of computers supports only sequential </li></ul><ul><li>processing. The normal flow of execution of statements in a high-level </li></ul><ul><li>language program is also in the sequential order, that is, each statement is </li></ul><ul><li>executed in its order of occurrence in the program. Some problems often require </li></ul><ul><li>That the normal flow of control be altered depending on the requirements. </li></ul><ul><li>C++ supports a number of control structures to perform the processing. </li></ul>FLOW OF CONTROL <ul><li>INTRODUCTION </li></ul>
  3. 3. <ul><li>CONTROL STRUCTURES </li></ul>Control Structures Loop or Iteration if-else Sequence Selection switch while , for do-while
  4. 4. <ul><li>These are the instructions given to the computer to perform some </li></ul><ul><li>action and form the smallest executable unit within a C++ program. </li></ul><ul><li>A semicolon (;) terminates a statement. </li></ul><ul><li>SIMPLE STATEMENT : It is a single statement. </li></ul><ul><li>COMPOUND STATEMENT : It is a group of statements separated from each other by a </li></ul><ul><li>semicolon(;). This group of statements is also called a block </li></ul><ul><li>of code enclosed within a pair of curly braces { }. The </li></ul><ul><li>significance of the block is that the group of statements </li></ul><ul><li>enclosed within the curly braces is treated as a single unit. </li></ul><ul><li>STATEMENTS </li></ul>
  5. 5. <ul><li>The statements written within a sequential structure are called selection or </li></ul><ul><li>conditional statements because we select the execution of a particular statement </li></ul><ul><li>after checking some condition. If the condition is proved then a particular action </li></ul><ul><li>would be performed and if the condition is false, some other action would be </li></ul><ul><li>performed. </li></ul><ul><li>The conditional statements are as follows: </li></ul><ul><li>if-statement </li></ul><ul><li>if-else statement </li></ul><ul><li>nested if </li></ul><ul><li>switch </li></ul><ul><li>CONDITIONAL STATEMENTS (SELECTION) </li></ul>
  6. 6. <ul><li>It tests a condition. The statements associated with if is (are) executed </li></ul><ul><li>only when the condition is true, otherwise the statements is (are) not </li></ul><ul><li>executed at all. The syntax of if statement is as shown below: </li></ul>If-statement if(expression) { statement; } where if is the keyword, expression is a booleon expression within a set of parenthesis and statement can be a simple or compound statement.
  7. 7. <ul><li>//to print whether a student is passed depending upon the marks </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>clrscr (); </li></ul><ul><li>int x , y ; </li></ul><ul><li>cout << “Enter the two integers” ; </li></ul><ul><li>cin >> x >> y ; </li></ul><ul><li>if ( x > y ) </li></ul><ul><li>cout << x ; </li></ul><ul><li>getch (); </li></ul><ul><li>} </li></ul>
  8. 8. <ul><li>It tests a condition. Statement 1 is executed when the condition is true otherwise </li></ul><ul><li>Statement 2 is executed. The syntax of if-else statement is: </li></ul>If-else statement if(expression) { statement; } else { statement; } where if is the keyword, expression is the booleon expression, else is the keyword and statement can be simple or compound statement.
  9. 9. <ul><li>//to print whether the entered no. is even or odd </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>clrscr (); </li></ul><ul><li>int num ; </li></ul><ul><li>cout << “Enter the number” ; </li></ul><ul><li>cin >> num; </li></ul><ul><li>if (( num % 2 )== 0 ) </li></ul><ul><li>cout << “Number is even” ; </li></ul><ul><li>else </li></ul><ul><li>cout << “Number is odd” ; </li></ul><ul><li>getch (); </li></ul><ul><li>} </li></ul>
  10. 10. <ul><li>One or more if statements embedded within the if statement are called nested ifs. </li></ul><ul><li>The following if-else statement is a nested if statement nested to level two: </li></ul>Nested if
  11. 11. <ul><li>//to print whether the entered no. is positive, negative or equal to zero </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>clrscr (); </li></ul><ul><li>int num ; </li></ul><ul><li>cout << “Enter the number” ; </li></ul><ul><li>cin >> num ; </li></ul><ul><li>if ( num == 0 ) </li></ul><ul><li>cout << “Number is equal to zero” ; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>if ( num > 0 ) </li></ul><ul><li>cout << “Number is positive” ; </li></ul><ul><li>else </li></ul><ul><li>cout << “Number is negative” ; </li></ul><ul><li>} </li></ul><ul><li>getch (); </li></ul><ul><li>} </li></ul>
  12. 12. <ul><li>In if-else-if ladder the different conditions are evaluated from the start and when </li></ul><ul><li>a condition is evaluated as true, the following statements are executed and the rest </li></ul><ul><li>of statements are skipped. When all the n conditions become false, then the final </li></ul><ul><li>else having the default-statement will be executed. </li></ul>The if-else-if ladder
  13. 13. <ul><li>This statement is used when we have to select one option out of many alternatives. </li></ul><ul><li>It is a multi branch statement that makes the control to jump to one of the several </li></ul><ul><li>statements based on the value of an integer variable or an expression. The general </li></ul><ul><li>Form of the switch is: </li></ul>switch switch(expression) { case constant 1: statement sequence 1; break; case constant 2: statement sequence 2; break; case constant 3: statement sequence 3; break; . . . case constant n-1: statement sequence n-1; break; default: statement sequence; }
  14. 15. <ul><li>//to print the corresponding day of the week </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>clrscr (); </li></ul><ul><li>int a,b,c ; </li></ul><ul><li>char op ; </li></ul><ul><li>cout << “Enter the two operands and the operator(+ - * /)” ; </li></ul><ul><li>cin >> a >> b >> op ; </li></ul><ul><li>switch ( op ) </li></ul><ul><li>{ </li></ul><ul><li>case ‘+’ : cout << “The sum is” << c = a + b ; </li></ul><ul><li>break ; </li></ul><ul><li>case ‘-’ : cout << “nThe difference is” << c = a - b ; </li></ul><ul><li>break ; </li></ul><ul><li>case ‘*’ : cout << “nThe product is” << c = a * b ; </li></ul><ul><li>break ; </li></ul><ul><li>case ‘/’ : cout << “nThe div is” << c = a / b ; </li></ul><ul><li>break ; </li></ul><ul><li>default : cout << “nWrong operator entered” ; </li></ul><ul><li>} </li></ul><ul><li>getch (); </li></ul><ul><li>} </li></ul>
  15. 16. <ul><li>Repetitive Execution means a set of statements are executed again and again </li></ul><ul><li>till the condition remains true and if the condition is false, the statement next </li></ul><ul><li>to those set of statements would be executed. There are3 types of statements </li></ul><ul><li>to implement repetative execution (Iteration/Looping). </li></ul><ul><li>while statement (Entry controlled loop-Pretest) </li></ul><ul><li>do while statement (Exit controlled loop-Post test) </li></ul><ul><li>for statement (counter controlled loop or deterministic loop) </li></ul><ul><li>REPETITIVE EXECUTION </li></ul>
  16. 17. <ul><li>It may not be executed even once if the condition is false initially. It is executed </li></ul><ul><li>till the condition remains true and the control comes out of the loop when the </li></ul><ul><li>condition becomes false. There must be some loop terminating condition inside the </li></ul><ul><li>body of the loop to avoid infinite looping. </li></ul><ul><li>The syntax of while statement is as follows: </li></ul>while loop while(expression) { statement; } where, while is the keyword, expression is the booleon expression which evaluates either to true or false & statement can be simple or compound statement.
  17. 18. <ul><li>//to find the sum of the digits of a number </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>clrscr (); </li></ul><ul><li>int n , rem , sum = 0 ; </li></ul><ul><li>cout << “Enter the numbers “ ; </li></ul><ul><li>cin >> n ; </li></ul><ul><li>while ( n != 0 ) </li></ul><ul><li>{ </li></ul><ul><li>rem = n % 10 ; </li></ul><ul><li>sum += rem ; </li></ul><ul><li>n /= 10 ; </li></ul><ul><li>} </li></ul><ul><li>cout << “The sum of the digits of the number is “ << sum ; </li></ul><ul><li>getch (); </li></ul><ul><li>} </li></ul>
  18. 19. <ul><li>This loop is executed at least once. It is executed till the condition remains true </li></ul><ul><li>and the control comes out of the loop when condition becomes false. There must </li></ul><ul><li>Be some loop terminating condition inside the body of the loop to avoidinfinite </li></ul><ul><li>Looping. </li></ul><ul><li>The syntax of do-while loop is as follows: </li></ul>do-while loop do { statement; } while(expression); where, while is the keyword, expression is the booleon expression which evaluates either to true or false & statement can be simple or compound statement.
  19. 20. <ul><li>//to print first n natural numbers and their sum </li></ul><ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main () </li></ul><ul><li>{ </li></ul><ul><li>clrscr (); </li></ul><ul><li>int n , i = 1 ,sum =0 ; </li></ul><ul><li>cout << “Enter the value of n: ” ; </li></ul><ul><li>cin >> n ; </li></ul><ul><li>cout << “nFirst “ << n << “ natural numbers are:nn” ; </li></ul><ul><li>do </li></ul><ul><li>{ </li></ul><ul><li>cout << i << ‘ ‘ ; </li></ul><ul><li>sum += I ; </li></ul><ul><li>i ++; </li></ul><ul><li>} while ( i <= n ); </li></ul><ul><li>cout << “nnSum = “ << sum ; </li></ul><ul><li>getch (); </li></ul><ul><li>} </li></ul>
  20. 21. <ul><li>The loop controlled variable is assigned some value. </li></ul><ul><li>The condition is checked. </li></ul><ul><li>If the condition is true, then the body of for loop is executed. </li></ul><ul><li>Then, the arithmetic expression is evaluated. </li></ul><ul><li>After the evaluation the condition is checked again and if it is true, then </li></ul><ul><li>again the body of for loop is executed and then the arithmetic expression </li></ul><ul><li>is checked again. </li></ul><ul><li>The for loop is executed again and again till the condition remains true. </li></ul><ul><li>The general syntax of the for loop is: </li></ul>for loop for(initial assignment;condition;arithmetic expression) { body of for loop }
  21. 23. //to print first n natural numbers and their sum #include<iostream.h> #include<conio.h> void main () { clrscr (); int n , sum =0 ; cout << “Enter the value of n: ” ; cin >> n ; cout << “nFirst “ << n << “ natural numbers are:nn” ; for ( i = 1 ; i <= n ;++ i ) { cout << i << ‘ ‘ ; sum += i ; } cout << “nnSum = “ << sum ; getch (); }
  22. 24. <ul><li>Nesting of loops means one or more loops within a loop. In this case </li></ul><ul><li>inner loop must terminate before the outer loop. </li></ul>Nested loop //to print number pattern #include<iostream.h> #include<conio.h> void main () { clrscr (); int i , j ; for ( i = 1 ; i <= 20 ;++ i ) { for ( j = 1 ; j <= 20 - i ;++ j ) cout << ‘ ‘ ; for ( j = 1 ; j <= i ;++ j ) cout << “* “ ; } cout << endl ; getch (); }
  23. 25. <ul><li>* </li></ul><ul><li>* * </li></ul><ul><li>* * * </li></ul><ul><li>* * * * </li></ul><ul><li>* * * * * </li></ul><ul><li>* * * * * * </li></ul><ul><li>* * * * * * * </li></ul><ul><li>* * * * * * * * </li></ul><ul><li>* * * * * * * * * </li></ul><ul><li>* * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * * * * * * * * </li></ul><ul><li>* * * * * * * * * * * * * * * * * * * * </li></ul>Output screen
  24. 26. <ul><li>JUMP STATEMENTS </li></ul>break statement : The break statements enable a program to skip over part of the code. A break statement terminates the smallest enclosing while, do while, for or switch statement. Execution resumes at the statement immediately following the body of the terminated statement. continue : continue is another jump statement like the break statement but the continue statement is somewhat different from break. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. exit function : It is a pre-defined function defined in header file process.h and stdlib.h. This function is used to terminate the execution of the program.
  25. 27. Use of break and continue statement
  26. 28. <ul><li>A goto statement can transfer the program control anywhere in the program. </li></ul><ul><li>The target destination of a goto statement is marked by a label. Both of these </li></ul><ul><li>i.e target label and goto must appear in the same function. </li></ul>goto statement (unconditional branching) Syntax for goto: goto label;
  27. 29. END

×