Вы находитесь на странице: 1из 25

Programming using c++

Presented by: Abhishek Sharma


FLOW OF CONTROL
Introduction

EveryC++ program must have at least onefunction called main(). When we run a C++ program, the
first statement executed will be at the beginningof main() and the last statement at the end of main().
Therefore, the main() is also knownas driver function as it 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 is executed in its orderof occurrence in the program. Someproblems often require That the
normal flow of control bealtered depending ontherequirements.
C++ supports a numberof control structures to perform theprocessing
CONTROL STRUCTURES

CONTROL STRUCTURES

SELECTION LOOP OR ITERATION

SEQUENCE

IF-ELSE SWITCH WHILE,FOR DO-WHILE


STATEMENTS
• These are the instructions givento the computer to perform some action and form the smallest
executable unit within a C++ program. A semicolon (;) terminates astatement.

1. SIMPLE STATEMENT: It is a single statement.

2. COMPOUND STATEMENT: It is a group of statements separated from each otherby 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 singleunit.
CONDITIONAL STATEMENTS
• The statements written within a sequential structure are called selection or conditional statements
because weselect the execution of a particular statement after checking some condition. I f the
condition is proved then a particular action would beperformed and if the condition is false, some
otheraction would be performed.

The conditional statements are as follows:


 if-statement
 if-else statement
 nested if
 switch
IF-STATEMENT
• It tests a condition. The statements associated with if is (are) executed only whenthe condition is true,
otherwise the statements is (are) not executed at all. The syntax of if statement is as shown below:

if(expression)
{
statement;
}

• whereif is the keyword, expression is a booleonexpression within a set of parenthesis and statement can be a
simple orcompoundstatement.
IF-ELSE STATEMENT
• It tests a condition. Statement 1 is executed whenthe condition is true otherwise
Statement 2 is executed.

• The syntax of if-else statement is:


if(expression)
{
statement;
}
else
{
statement;
}
NESTED-IF
• Oneormoreif statements embeddedwithin the if statement are called nested ifs. The following if-else statement is a nested if
statement nested to level two:
ELSE-IF LADDER
• 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 nconditions
become false, then the final else having the default-statement will beexecuted.
SWITCH
• This statement is used whenwehave to select oneoption out of manyalternatives. It is a multi branch
statement that makes the control to jump to oneof the several statements based onthe value of an integer
variable oran expression. Thegeneral Form of the switchis:

switch(expression)
{
case constant 1: statement sequence1;
break;
case constant 2: statement sequence2;
break;
case constant 3: statement sequence3;
break;
.
.
.
case constant n-1: statement sequencen-1;
break;
default: statement
sequence;
}
REPETITIVE EXECUTION
• 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 beexecuted. There are3
types of statements to implement repetative execution (Iteration/Looping).

 while statement (Entry controlledloop-Pretest)


 do while statement (Exit controlled loop-Post test)
 for statement (counter controlled loop or deterministicloop)
WHILE LOOP
• It may not beexecuted evenonceif the condition is false initially. It is executed till the condition
remains true and the control comesout of the loop whenthe condition becomesfalse. There must be
some loop terminatingcondition inside the body of the loop to avoid infinite looping.

The syntax of while statement is as follows:


DO-WHILE LOOP
• This loop is executed at least once.It is executed till the condition remains true and the control comesout of
the loop whencondition becomesfalse. There must Be some loop terminating condition inside the body of the loop
to avoid infinite Looping.

The syntax of do-while loop is as follows:

do
{
statement;
}
while(expression);

where,while is the keyword, expression is the booleon


expression which evaluates either to true orfalse & statement
can be simple orcompoundstatement.
FOR LOOP
• The loop controlled variable is assigned some value. The condition is checked.
• I f the conditionis 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 remainstrue.

• The general syntax of the for loopis:

for(initial assignment;condition;arithmetic expression)


{
body of for loop
}
JUMP STATEMENTS
BREAK STATEMENT: The break statements enable a program to skip overpart of the code. A break
statement terminates the smallest enclosing while, do while, for orswitch 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 inbetween.
GOTO STATEMENT (UNCONDITIONAL BRANCHING)
• A gotostatement can transfer the program control anywherein the program. The target destination of
a gotostatement is marked by a label. Both of these
• i.etarget label and gotomust appear in the same function.

Syntax for goto:

goto label;
THANK YOU
SUBMITTED BY:ABHISHEK SHARMA
SUBMITTED TO:MISS KIMMI MEHTA

Вам также может понравиться