29
-Made by SHRABANTI PROGRAMING IN

Flow of control ppt

Embed Size (px)

DESCRIPTION

Computer Science

Citation preview

Page 1: Flow of control ppt

-Made bySHRABANTI

PROGRAMING IN

Page 2: Flow of control ppt

Every C++ program must have at least one function called main().When we run a C++ program, the first statement executed will beat the beginning of main() and the last statement at the end of main(). Therefore, the main() is also known as driver function asit drives the program.

The Von-Neumann architecture of computers supports only sequential processing. The normal flow of execution of statements in a high-level language program is also in the sequential order, that is, each

statement isexecuted in its order of occurrence in the program. Some problems

often require That the normal flow of control be altered depending on the

requirements. C++ supports a number of control structures to perform the

processing.

FLOW OF CONTROL

INTRODUCTION

Page 3: Flow of control ppt

CONTROL STRUCTURES

Control Structures

Loop or Iteration

if-else

SequenceSelection

switch while , for do-while

Page 4: Flow of control ppt

These are the instructions given to the computer to perform someaction and form the smallest executable unit within a C++ program.A semicolon (;) terminates a statement.

SIMPLE STATEMENT: It is a single statement.

COMPOUND STATEMENT: It is a group of statements separated from each other by a

semicolon(;). This group of statements is also called a block

of code enclosed within a pair of curly braces { }. The

significance of the block is that the group of statements

enclosed within the curly braces is treated as a single unit.

STATEMENTS

Page 5: Flow of control ppt

The statements written within a sequential structure are called selection or

conditional statements because we select the execution of a particular statement

after checking some condition. If the condition is proved then a particular action

would be performed and if the condition is false, some other action would be

performed.

The conditional statements are as follows:if-statementif-else statementnested ifswitch

CONDITIONAL STATEMENTS (SELECTION)

Page 6: Flow of control ppt

It tests a condition. The statements associated with if is (are) executed

only when the condition is true, otherwise the statements is (are) not

executed at all. The syntax of if statement is as shown below:

If-statement

if(expression) { statement; }

where if is the keyword, expressionis a booleon expression within a set of parenthesis and statement can be

a simple or compound statement.

Page 7: Flow of control ppt

//to print whether a student is passed depending upon the marks#include<iostream.h>#include<conio.h>void main(){ clrscr(); int x,y; cout<<“Enter the two integers”; cin>>x>>y; if(x>y) cout<<x; getch();}

Page 8: Flow of control ppt

It tests a condition. Statement 1 is executed when the condition is true otherwise

Statement 2 is executed. The syntax of if-else statement is:

If-else statement

if(expression) { statement; } else { statement; }

where if is the keyword,expression is the booleonexpression, else is the keyword and statement can be simple or compoundstatement.

Page 9: Flow of control ppt

//to print whether the entered no. is even or odd#include<iostream.h>#include<conio.h>void main(){ clrscr(); int num; cout<<“Enter the number”; cin>>num; if((num%2)==0) cout<<“Number is even”; else cout<<“Number is odd”; getch();}

Page 10: Flow of control ppt

One or more if statements embedded within the if statement are called nested ifs.

The following if-else statement is a nested if statement nested to level two:

Nested if

Page 11: Flow of control ppt

//to print whether the entered no. is positive, negative or equal to zero#include<iostream.h>#include<conio.h>void main(){ clrscr(); int num; cout<<“Enter the number”; cin>>num; if(num==0) cout<<“Number is equal to zero”; else { if(num>0) cout<<“Number is positive”; else cout<<“Number is negative”; }getch();}

Page 12: Flow of control ppt

In if-else-if ladder the different conditions are evaluated from the start and when

a condition is evaluated as true, the following statements are executed and the rest

of statements are skipped. When all the n conditions become false, then the final

else having the default-statement will be executed.

The if-else-if ladder

Page 13: Flow of control ppt

This statement is used when we have to select one option out of many alternatives.

It is a multi branch statement that makes the control to jump to one of the several

statements based on the value of an integer variable or an expression. The general

Form of the switch is:

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

Page 14: Flow of control ppt
Page 15: Flow of control ppt

//to print the corresponding day of the week#include<iostream.h>#include<conio.h>void main(){ clrscr(); int a,b,c; char op; cout<<“Enter the two operands and the operator(+ - * /)”; cin>>a>>b>>op; switch(op) { case ‘+’:cout<<“The sum is”<<c=a+b; break; case ‘-’:cout<<“\nThe difference is”<<c=a-b; break; case ‘*’:cout<<“\nThe product is”<<c=a*b; break; case ‘/’:cout<<“\nThe div is”<<c=a/b; break; default:cout<<“\nWrong operator entered”; } getch();}

Page 16: Flow of control ppt

Repetitive Execution means a set of statements are executed again and again

till the condition remains true and if the condition is false, the statement next

to those set of statements would be executed. There are3 types of statements

to implement repetative execution (Iteration/Looping).

while statement (Entry controlled loop-Pretest)do while statement (Exit controlled loop-Post test)for statement (counter controlled loop or deterministic loop)

REPETITIVE EXECUTION

Page 17: Flow of control ppt

It may not be executed even once if the condition is false initially. It is executed

till the condition remains true and the control comes out of the loop when the

condition becomes false. There must be some loop terminating condition inside the

body of the loop to avoid infinite looping.

The syntax of while statement is as follows:

while loop

while(expression) { statement; }

where, while is the keyword, expressionis the booleon expression which evaluateseither to true or false & statement can besimple or compound statement.

Page 18: Flow of control ppt

//to find the sum of the digits of a number#include<iostream.h>#include<conio.h>void main(){ clrscr(); int n,rem,sum=0; cout<<“Enter the numbers “; cin>>n; while(n!=0) { rem=n%10; sum+=rem; n/=10; } cout<<“The sum of the digits of the number is “<<sum; getch();}

Page 19: Flow of control ppt

This loop is executed at least once. It is executed till the condition remains true

and the control comes out of the loop when condition becomes false. There must

Be some loop terminating condition inside the body of the loop to avoidinfinite

Looping.

The syntax of do-while loop is as follows:

do-while loop

do { statement; } while(expression); where, while is the keyword, expression

is the booleon expression which evaluateseither to true or false & statement can besimple or compound statement.

Page 20: Flow of control ppt

//to print first n natural numbers and their sum#include<iostream.h>#include<conio.h>void main(){ clrscr(); int n,i=1,sum=0; cout<<“Enter the value of n: ”; cin>>n; cout<<“\nFirst “<<n<<“ natural numbers are:\n\n”; do { cout<<i<<‘ ‘; sum+=I; i++; }while(i<=n); cout<<“\n\nSum = “<<sum; getch();}

Page 21: Flow of control ppt

The loop controlled variable is assigned some value.The condition is checked.If the condition is true, then the body of for loop is executed.Then, the arithmetic expression is evaluated.After the evaluation the condition is checked again and if it is true, then

again the body of for loop is executed and then the arithmetic expression

is checked again.The for loop is executed again and again till the condition remains true.

The general syntax of the for loop is:

for loop

for(initial assignment;condition;arithmetic expression){ body of for loop}

Page 22: Flow of control ppt
Page 23: Flow of control ppt

//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:\n\n”; for(i=1;i<=n;++i) { cout<<i<<‘ ‘; sum+=i; } cout<<“\n\nSum = “<<sum; getch();}

Page 24: Flow of control ppt

Nesting of loops means one or more loops within a loop. In this case

inner loop must terminate before the outer loop.

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

Page 25: Flow of control ppt

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Output screen

Page 26: Flow of control ppt

JUMP STATEMENTS

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.

Page 27: Flow of control ppt

Use of break and continue statement

Page 28: Flow of control ppt

A goto statement can transfer the program control anywhere in the program.

The target destination of a goto statement is marked by a label. Both of these

i.e target label and goto must appear in the same function.

goto statement (unconditional branching)

Syntax for goto:

goto label;

Page 29: Flow of control ppt