SlideShare uma empresa Scribd logo
1 de 19
(1.1) INTRODUCTION
Generally a program executes its statements from beginning to
the end, but all programs don't follow this. This presentation will
tell us in detail about the program control statements.

                 (1.2) STATEMENTS
Statements are the instructions given to the computer to perform
any kind of data movements, be it making decisions or repeating
actions. They form the smallest executable unit of the program.
They are terminated by a semicolon(;). The different types of
statements are:-
(1.2.1) Compound statements(Block)
A block is a group of statements that are enclosed in a pair of
curly brackets:-
{
        statement:-1
        statement:-2
}
It may appear anywhere in a program as a single statement of the
program. A block is many times known as compound statements.
(1.3)STATEMENT FLOW CONTROL
In a program, statements may be executed sequentially , selectively
or iteratively:-

Sequence:- This is the default flow of the statements and also
               known as sequence construct and it also means that
               the statements are being executed sequentially.
Selection:-    The selection construct means that the execution of
               the statements depends on a condition-test, if the
               condition is true then the statements would be
               executed else they would be skipped. An else-if
               statement is a good example of selection statement.
Iteration:-    The iteration means repetition of a certain set of
               statements based on a certain condition-test.
               Looping statements can be used to perform the
               repetition of a certain set of statements.
               They are of three types:- for loop, Do-while loop
      (1.4)   SELECTION STATEMENTS
               and while loop.
They allow to choose a certain set of statements for execution only if
a condition is true. There are two types of selection statements
provided by c++ namely if statement and switch statement.

     (1.4.1) THE IF STATEMENT OF C++
An if statement tests for a particular condition, if the statement is
true then the particular set of statements is executed else they are
skipped.
The syntax for the if statement is :-
 If(condition)
 statement;
 The statement can either be a single statement or a block. We can
 also use nested if statements or a ladder of if statements if required
 in our program.

        (1.4.2)THE SWITCH STATEMENT
C++ provides another type of selection statement i.e. switch statement.
The selection statement successively tests the value of an expression
against a list of integer or character constants. When a match is found,
the statements associated with that constant are executed.
The syntax for a switch statement is :-
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 n;
}
(1.5) ITERATION STATEMENTS
Iteration statements in c++ allows a certain number instructions to
be performed repeatedly until a certain condition becomes false.
Iteration statements are also called loops or looping statements.
C++ allows the use of three loops namely:-for loop, while loop, do-
while loop.
(1.5.1)   ELEMNTS THAT CONTROL A LOOP
Every loop has its elements that control and govern the execution.
Generally, a loop has 4 elements that have different purposes. These
elements are as given below:-

1. Initialization Expression(s):
 Before entering the loop the initialization expression(s) initializes
 the loop control variables with their first values.
2. Test Expression:
The test expression is that expression whose truth value decides
whether the loop-body will be executed or not. If the value is true
then the body gets executed else it gets terminated.
3. Update expression(s): The update expression(s) change the
value(s) of loop variable(s). The update expression execute at the
end of the loop.
4. The Body-of-the-loop:          The statements that are to be
executed form the body-of-the-loop.
               (1.5.2) THE FOR LOOP
The for loop is the easiest to understand of all because all its loop-
control elements are gathered in one place i.e. at the top. Its syntax
is:-
For(initialization expression(s);test—expression; update expression)
        body-of-the-loop;
The following slide explains the working of the for loop with an e.g:
#include<iostream.h>
int main()
{
   int i;
   for(i=1;i<=10;i++)
         cout<<“n”<<i;
   return 0;
}
 Firstly, initialization expression is executed i.e., i=1 which
   gives the first value 1 to variable i.
 Then, the test-expression is executed i.e., i<=10 which results
   into true i.e., 1.
 Since, the test expression is true, the body-of-the-loop i.e.,
   cout<<“n”<<i is executed which prints the current value of i
   on the next line.
 After executing the loop-body, the update expression i.e., i++
  is executed which increments the value of i.
 After the update expression is executed, the test-expression is
  again evaluated. If it is true the sequence is repeated from
  step no. 3, otherwise the loop terminates.

            (1.5.3) THE WHILE LOOP
The second loop provided by c++ is the while loop. It is an entry
controlled loop. The syntax for a while loop is:-
while (expression)
        loop-body
Where the loop may contain a single statement, a compound
statement or an empty statement. The loop iterates while the
expression is true. When the expression becomes false, the program
control terminates the loop.
In a while loop, a variable should be initialized before the loop as
uninitialized variables cannot be used in the loop. The loop variable
should be updated inside the body-of-the-loop. Following example
explains the working of a while loop:-
int i,fact=1,n;
cout<<“Enter a number.”;
cin>>n;
i=1;
while(i<=n)
{
        fact=fact×I;
        i=i++;
}
cout<<“The factorial of the num.=“<<fact;
The program segment in the previous slide would input a number
 from the user and print the factorial of the number.

         (1.5.4) THE DO-WHILE LOOP
Unlike the for and while loop do-while loop is an exit controlled loop
i.e., it evaluates the test-expression at the bottom of the loop after
the body-of-the-loop. This means that the do-while loop always
executes at least once. The syntax for do-while loop is:-
do
{
          statement;
}
while(test-expression)
The brackets { } are not necessary when there is only one statement in
the loop-body.
An example for do-while loop is:-
int i=1,n=10002;
do
{
        cout<<n;
        n++;
        i++;
}
while(i==1);
In the above example the test expression is never true but still
every time the loop is executed the loop would execute only once
and print “10002”.
(1.5.5) NESTED LOOPS

C++ allows its users to use a number of loops inside a single loop,
hence it means that c++ allows the use of nested loops. A user can
use nested loops according to his own requirement. The following
example explains the use of nested loops:-
for(i=1;i<=4;i++)
{
        cout<<“n”;
        for(j=1;j<=i;j++)
               cout<<“*”;
}
In the above program the inner loop will execute for i number of
times and i has values 1,2,3,4.
The output of the above program segment would be:-
 *
 **
 ***
 ****

            (1.6) JUMP STATEMENTS
Jump statements unconditionally transfer program control within
a function. C++ has four jump statements that perform an
unconditional branch: return, goto, break, and continue. Of these
return and goto maybe used anywhere in the program but break
and continue can be used only inside brackets { } .
(1.6.1) THE GOTO STATEMENT
A goto statement can statement can transfer the program control
anywhere in the program. The target destination of a goto
statement is marked by a label. The target label and goto must
appear in the same function.

       (1.6.2) THE BREAK STATEMENT
The break statement enables the user to skip over a part of the
program. The break statement is used inside brackets { }. It brings
the program control directly outside the brackets { }.
    (1.6.3) THE CONTINUE STATEMENT
 The continue statement skips a certain part of the code but does
 not terminate the loop but skips a certain part of the code.
(1.6.4) THE EXIT() FUNCTION
The library function exit() takes u directly outside the program. It
does not even stop for the getch() function. It terminates the
program as soon as it is encountered no matter where it appears.

Mais conteúdo relacionado

Mais procurados (20)

Loops
LoopsLoops
Loops
 
C++ loop
C++ loop C++ loop
C++ loop
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
The Loops
The LoopsThe Loops
The Loops
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Loops in C
Loops in CLoops in C
Loops in C
 
Iteration
IterationIteration
Iteration
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
While loop
While loopWhile loop
While loop
 
Loops in c
Loops in cLoops in c
Loops in c
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Java loops
Java loopsJava loops
Java loops
 

Semelhante a Comp ppt (1)

Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsRai University
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsRai University
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statementsRai University
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statementsRai University
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statementsRai University
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notesmuhammadFaheem656405
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition StructureShahzu2
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?AnuragSrivastava272
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdfKirubelWondwoson1
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 

Semelhante a Comp ppt (1) (20)

C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control statements
Control statementsControl statements
Control statements
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
What is loops? What is For loop?
What is loops? What is For loop?What is loops? What is For loop?
What is loops? What is For loop?
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
C language 2
C language 2C language 2
C language 2
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 

Comp ppt (1)

  • 1.
  • 2. (1.1) INTRODUCTION Generally a program executes its statements from beginning to the end, but all programs don't follow this. This presentation will tell us in detail about the program control statements. (1.2) STATEMENTS Statements are the instructions given to the computer to perform any kind of data movements, be it making decisions or repeating actions. They form the smallest executable unit of the program. They are terminated by a semicolon(;). The different types of statements are:-
  • 3. (1.2.1) Compound statements(Block) A block is a group of statements that are enclosed in a pair of curly brackets:- { statement:-1 statement:-2 } It may appear anywhere in a program as a single statement of the program. A block is many times known as compound statements.
  • 4. (1.3)STATEMENT FLOW CONTROL In a program, statements may be executed sequentially , selectively or iteratively:- Sequence:- This is the default flow of the statements and also known as sequence construct and it also means that the statements are being executed sequentially. Selection:- The selection construct means that the execution of the statements depends on a condition-test, if the condition is true then the statements would be executed else they would be skipped. An else-if statement is a good example of selection statement.
  • 5. Iteration:- The iteration means repetition of a certain set of statements based on a certain condition-test. Looping statements can be used to perform the repetition of a certain set of statements. They are of three types:- for loop, Do-while loop (1.4) SELECTION STATEMENTS and while loop. They allow to choose a certain set of statements for execution only if a condition is true. There are two types of selection statements provided by c++ namely if statement and switch statement. (1.4.1) THE IF STATEMENT OF C++ An if statement tests for a particular condition, if the statement is true then the particular set of statements is executed else they are skipped.
  • 6. The syntax for the if statement is :- If(condition) statement; The statement can either be a single statement or a block. We can also use nested if statements or a ladder of if statements if required in our program. (1.4.2)THE SWITCH STATEMENT C++ provides another type of selection statement i.e. switch statement. The selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed.
  • 7. The syntax for a switch statement is :- 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 n; }
  • 8. (1.5) ITERATION STATEMENTS Iteration statements in c++ allows a certain number instructions to be performed repeatedly until a certain condition becomes false. Iteration statements are also called loops or looping statements. C++ allows the use of three loops namely:-for loop, while loop, do- while loop.
  • 9. (1.5.1) ELEMNTS THAT CONTROL A LOOP Every loop has its elements that control and govern the execution. Generally, a loop has 4 elements that have different purposes. These elements are as given below:- 1. Initialization Expression(s): Before entering the loop the initialization expression(s) initializes the loop control variables with their first values. 2. Test Expression: The test expression is that expression whose truth value decides whether the loop-body will be executed or not. If the value is true then the body gets executed else it gets terminated.
  • 10. 3. Update expression(s): The update expression(s) change the value(s) of loop variable(s). The update expression execute at the end of the loop. 4. The Body-of-the-loop: The statements that are to be executed form the body-of-the-loop. (1.5.2) THE FOR LOOP The for loop is the easiest to understand of all because all its loop- control elements are gathered in one place i.e. at the top. Its syntax is:- For(initialization expression(s);test—expression; update expression) body-of-the-loop; The following slide explains the working of the for loop with an e.g:
  • 11. #include<iostream.h> int main() { int i; for(i=1;i<=10;i++) cout<<“n”<<i; return 0; }  Firstly, initialization expression is executed i.e., i=1 which gives the first value 1 to variable i.  Then, the test-expression is executed i.e., i<=10 which results into true i.e., 1.  Since, the test expression is true, the body-of-the-loop i.e., cout<<“n”<<i is executed which prints the current value of i on the next line.
  • 12.  After executing the loop-body, the update expression i.e., i++ is executed which increments the value of i.  After the update expression is executed, the test-expression is again evaluated. If it is true the sequence is repeated from step no. 3, otherwise the loop terminates. (1.5.3) THE WHILE LOOP The second loop provided by c++ is the while loop. It is an entry controlled loop. The syntax for a while loop is:- while (expression) loop-body Where the loop may contain a single statement, a compound statement or an empty statement. The loop iterates while the expression is true. When the expression becomes false, the program control terminates the loop.
  • 13. In a while loop, a variable should be initialized before the loop as uninitialized variables cannot be used in the loop. The loop variable should be updated inside the body-of-the-loop. Following example explains the working of a while loop:- int i,fact=1,n; cout<<“Enter a number.”; cin>>n; i=1; while(i<=n) { fact=fact×I; i=i++; } cout<<“The factorial of the num.=“<<fact;
  • 14. The program segment in the previous slide would input a number from the user and print the factorial of the number. (1.5.4) THE DO-WHILE LOOP Unlike the for and while loop do-while loop is an exit controlled loop i.e., it evaluates the test-expression at the bottom of the loop after the body-of-the-loop. This means that the do-while loop always executes at least once. The syntax for do-while loop is:- do { statement; } while(test-expression) The brackets { } are not necessary when there is only one statement in the loop-body.
  • 15. An example for do-while loop is:- int i=1,n=10002; do { cout<<n; n++; i++; } while(i==1); In the above example the test expression is never true but still every time the loop is executed the loop would execute only once and print “10002”.
  • 16. (1.5.5) NESTED LOOPS C++ allows its users to use a number of loops inside a single loop, hence it means that c++ allows the use of nested loops. A user can use nested loops according to his own requirement. The following example explains the use of nested loops:- for(i=1;i<=4;i++) { cout<<“n”; for(j=1;j<=i;j++) cout<<“*”; } In the above program the inner loop will execute for i number of times and i has values 1,2,3,4.
  • 17. The output of the above program segment would be:- * ** *** **** (1.6) JUMP STATEMENTS Jump statements unconditionally transfer program control within a function. C++ has four jump statements that perform an unconditional branch: return, goto, break, and continue. Of these return and goto maybe used anywhere in the program but break and continue can be used only inside brackets { } .
  • 18. (1.6.1) THE GOTO STATEMENT A goto statement can statement can transfer the program control anywhere in the program. The target destination of a goto statement is marked by a label. The target label and goto must appear in the same function. (1.6.2) THE BREAK STATEMENT The break statement enables the user to skip over a part of the program. The break statement is used inside brackets { }. It brings the program control directly outside the brackets { }. (1.6.3) THE CONTINUE STATEMENT The continue statement skips a certain part of the code but does not terminate the loop but skips a certain part of the code.
  • 19. (1.6.4) THE EXIT() FUNCTION The library function exit() takes u directly outside the program. It does not even stop for the getch() function. It terminates the program as soon as it is encountered no matter where it appears.