Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
CONTROL
STRUCTURES
Chapter 43/8/2016
1
By:-Gourav Kottawar
contents
3/8/2016
2
 4.1 Introduction
 4.2 Selection Statements
If, Nested if, if….else, else if Ladder
ternary operator...
4.3 Iterative Statements
3/8/2016
3
 Looping
 In looping, a sequence of statements are
executed until some condition is ...
4.3 Iterative Statements
3/8/2016
4
 How loop works
3/8/2016
5
 Loops may classified as
entry-controlled loop
exit-controlled loop
Entry controlled loop
 The control conditions
are tested before the
start of the loop
execution.
 If the conditions are
...
Exit Controlled loop
 he Test is performed
at the end of the body
of the loop and there
fore the body is
executed
uncondi...
Types of loop
3/8/2016
8
while loop
3/8/2016
9
while loop
3/8/2016
10
 Example
1.Program to print first 10 numbers
2. Program to add first 10 numbers
3. Program to add ...
for loop
3/8/2016
11
for loop
3/8/2016
12
 Examples
1.WAP to accept number and check whether it is
prime or NOT.
2. WAP to accept number and c...
for loop
3/8/2016
13
 Nested for loop
for loop
3/8/2016
14
 Example
1.WAP to half print pyramid of numbers
2. Print pattern
* * *
* *
*
do while loop
3/8/2016
15
do while loop
3/8/2016
16
 Example
1. Program to print first ten multiples of 5.
Sentinel loops
3/8/2016
17
 Loops may be classified into two categories
1. Counter controlled loops
2. Sentinel controlle...
3/8/2016
18
2. Sentinel controlled loop
- a special value is used to change the loop
control expression from true to false...
Selecting a loop
3/8/2016
19
 Analyze the problem and see whether it required
a pre – test or post – test.
 If it requir...
Jumps in loop
3/8/2016
20
 Sometimes when executing loop it becomes
desirable to skip a part of the loop or to leave
the ...
Jumping out of loop
3/8/2016
21
while (------)
{
-----
----
if (condition)
break;
-----
-----
}
-----
Exit
Fro
m
loop
3/8/2016
22
do
{
---
---
if(condition)
break;
----
----
} while(-----);
-----
Exit
Fro
m
loop
3/8/2016
23
for (----)
{
---------
---------
if (error)
break;
-------
-------
}
--------
Exit
Fro
m
loop
3/8/2016
24
for( -------)
{
--------
for(-------)
{
------
if(condition)
break;
------
}
--------
}
Exit
Fro
m
inne
r
loop
The goto statement
3/8/2016
25
 To branch unconditionally from one point to
another in the program
 goto requires a labe...
goto statement
goto label;
---------
--------
--------
label :
Staement;
forword jump
label :
Statement;
-------
---------...
3/8/2016
27
 Example
Jumping within and exiting from the
loops with goto statement
while(--------)
{
if(error)
goto stop;
-------
if (condition...
3/8/2016
29
Note –
Avoid goto – because
1. Many compilers may generate less efficient
code.
2. Makes program logic complec...
Prochain SlideShare
Chargement dans…5
×

control structures in c if switch for

383 vues

Publié le

control structures in c like for switch if etc.

Publié dans : Formation
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

control structures in c if switch for

  1. 1. CONTROL STRUCTURES Chapter 43/8/2016 1 By:-Gourav Kottawar
  2. 2. contents 3/8/2016 2  4.1 Introduction  4.2 Selection Statements If, Nested if, if….else, else if Ladder ternary operator, switch, Nested switch, conditional expression  4.3 Iterative Statements while loop, do-while loop, for loop  4.4 Jump Statements Goto & label, break & continue, exit() function  4.5 Compound Statements  4.6 Null Statements
  3. 3. 4.3 Iterative Statements 3/8/2016 3  Looping  In looping, a sequence of statements are executed until some condition is satisfied which is placed for termination of the loop  A program loop consists of two segments a. body of the loop b. the control statement  The control is tested always for execution of the body of the loop
  4. 4. 4.3 Iterative Statements 3/8/2016 4  How loop works
  5. 5. 3/8/2016 5  Loops may classified as entry-controlled loop exit-controlled loop
  6. 6. Entry controlled loop  The control conditions are tested before the start of the loop execution.  If the conditions are not satisfied , then the body of the loop will not be executed.  Entry controlled loop is also known as pre- test loop.  Example: – While Loop for Loop 3/8/2016 6
  7. 7. Exit Controlled loop  he Test is performed at the end of the body of the loop and there fore the body is executed unconditionally for the first time.  Exit controlled loop is also known as post- test loop.  Ex- ->Do-While 3/8/2016 7
  8. 8. Types of loop 3/8/2016 8
  9. 9. while loop 3/8/2016 9
  10. 10. while loop 3/8/2016 10  Example 1.Program to print first 10 numbers 2. Program to add first 10 numbers 3. Program to add first 10 even numbers 4. Program to add first 10 odd numbers
  11. 11. for loop 3/8/2016 11
  12. 12. for loop 3/8/2016 12  Examples 1.WAP to accept number and check whether it is prime or NOT. 2. WAP to accept number and check whether it is Armstrong or NOT.
  13. 13. for loop 3/8/2016 13  Nested for loop
  14. 14. for loop 3/8/2016 14  Example 1.WAP to half print pyramid of numbers 2. Print pattern * * * * * *
  15. 15. do while loop 3/8/2016 15
  16. 16. do while loop 3/8/2016 16  Example 1. Program to print first ten multiples of 5.
  17. 17. Sentinel loops 3/8/2016 17  Loops may be classified into two categories 1. Counter controlled loops 2. Sentinel controlled loops 1. Counter controlled loops - - we know in advance how many times the loop will be executed. - use control variable known as counter. - counter must be initialized, tested and updated properly - also known as repetition loop
  18. 18. 3/8/2016 18 2. Sentinel controlled loop - a special value is used to change the loop control expression from true to false. - the control variable is called as sentinel variable. - also called as indefinite repetition loop because of number of repetition loop because the number of repetitions is not known before the loop begins executing
  19. 19. Selecting a loop 3/8/2016 19  Analyze the problem and see whether it required a pre – test or post – test.  If it requires a post – test loop, then we can use only one loop, do while  If it requires a pre – test loop, then we have two choices : for and while loop  Decide whether the loop termination requires counter based control or sentinel – based control.  Use for loop if the counter based control is necessary  Use while loop if the sentinel based control is required.
  20. 20. Jumps in loop 3/8/2016 20  Sometimes when executing loop it becomes desirable to skip a part of the loop or to leave the loop as soon as certain condition occurs.  C permits jumps from one statement to another within a loop as well as a jump out of loop.
  21. 21. Jumping out of loop 3/8/2016 21 while (------) { ----- ---- if (condition) break; ----- ----- } ----- Exit Fro m loop
  22. 22. 3/8/2016 22 do { --- --- if(condition) break; ---- ---- } while(-----); ----- Exit Fro m loop
  23. 23. 3/8/2016 23 for (----) { --------- --------- if (error) break; ------- ------- } -------- Exit Fro m loop
  24. 24. 3/8/2016 24 for( -------) { -------- for(-------) { ------ if(condition) break; ------ } -------- } Exit Fro m inne r loop
  25. 25. The goto statement 3/8/2016 25  To branch unconditionally from one point to another in the program  goto requires a label to identify the palce where the branch is to be made. label is variable name followed by colon label is placed immediately before the statement where the control is to be transferred .
  26. 26. goto statement goto label; --------- -------- -------- label : Staement; forword jump label : Statement; ------- --------- ------- goto label; backward jump 3/8/2016 26
  27. 27. 3/8/2016 27  Example
  28. 28. Jumping within and exiting from the loops with goto statement while(--------) { if(error) goto stop; ------- if (condition) goto abc; ------- ------- abc: } Stop: ------ ------ for(-------) { ------ for( -------) { ------ if(error) goto error; ------ } ------- } error: --------- -------- 3/8/2016 28 Exit Fro m loop Jump Within loop Exit Fro m two loop s
  29. 29. 3/8/2016 29 Note – Avoid goto – because 1. Many compilers may generate less efficient code. 2. Makes program logic complecated 3. Renders program unreadable

×