SlideShare a Scribd company logo
CSPC-COMPUTER
SYSTEMS
PROGRAMMING IN ‘C’
ANKUR SRIVASTAVA
DEPARTMENT OF COMPUTER SCIENCE
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
1
CONDITIONAL PROGRAM EXECUTION: APPLYING IF AND SWITCH
STATEMENTS, NESTING IF AND ELSE, USE OF BREAK AND DEFAULT WITH
SWITCH.
PROGRAM LOOPS AND ITERATIONS: USE OF WHILE, DO WHILE AND FOR
LOOPS, MULTIPLE LOOP VARIABLES, USE OF BREAK AND CONTINUE
STATEMENTS.
FUNCTIONS: INTRODUCTION, TYPES OF FUNCTIONS, FUNCTIONS WITH
ARRAY, PASSING VALUES TO FUNCTIONS, RECURSIVE FUNCTIONS.
 UNIT-3 TOPICS
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
2
UNIT-3 CONDITIONAL PROGRAM EXECUTION
 C supports two types of decision control statements.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
3
Selection/Branching
statement
Conditional type
if If-else If-else-if switch
Unconditional
type
CONDITIONAL BRANCHING STATEMENTS:
 These statements helps to jump from one part to another part.
 Whether a particular condition is satisfied or not.
 It includes:-
---- if statement
---- if- else statement
---- if- else- if statement
---- Switch statement
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
4
IF STATEMENTS
 The if statement allows the program to test the state of the program
variables using a Boolean expression.
 Syntax
If (test expression)
{
Statement 1;
……………
Statement n;
}
Statement x;
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
5
Test
exp
Statement block
1
Statement x
FALSE
TRUE
IF-ELSE STATEMENT
 The if-else statement expresses simplest decision making.
 The syntax is
if (expression)
statement1
elseopt
Statement2
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
6
IF –ELSE STATEMENT
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
7
NESTED IF-ELSE
 When the if-else condition exists in another if-else condition, it is nested if-else.
 Syntax
If (test condition-1)
{
if (test condition-2)
{
Statement-1; }
else {
Statement-2; }
}
else
{
Statement-3;
}
Statement- x;
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
8
IF-ELSE-IF STATEMENT
 For testing additional conditions if-else-if statements is
constructed.
If (condition-1)
Statement -1;
Else if (condition-2)
Statement-2;
Else if (condition-3)
Statement-3;
Else if (condition-n)
Statement –n;
Else
Default-statement;
Statement-x;
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
9
SWITCH STATEMENTS
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
10
SWITCH STATEMENT
 The switch statement is used to select one of several alternatives when the
selection is based on the value of a single variable or an expression.
switch (controlling expression) {
case label1:
statement1
break;
case label2:
statement2
break; ……..
case labeln:
statementn
break;
default:
statementd; }
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
11
If the result of this controlling expression
matches label1, execute staement1 and then break
this switch block.
If the result matches none of all labels, execute the
default statementd.
USE OF BREAK AND DEFAULT WITH SWITCH
Switch (exp)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
…………….
…………….
default:
default-block
break; } statement-x;
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
12
PROGRAM LOOPS AND ITERATIONS:
 In looping, a sequence of statements are executed until some
conditions for termination of the loop are satisfied.
 Two segments are:
------- body of the loop
------- control statement
Depending on the position of the control statement in the loop,
A control structure may be either—
-------- entry controlled loop or
-------- exit controlled loop.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
13
LOOP CONTROL STRUCTURES
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
14
True
Test
condition
?
Test
condition
?
Body of the
loop
Body of the
loop
False
False
True
EntryEntry
(b) Exit controlled loop(a) Entry controlled loop
USE OF WHILE:
THE WHILE STATEMENT IS USED TO REPEAT A COURSE OF
ACTION.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
15
THE WHILE STATEMENT
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
16
Format
While (test condition)
{
Body of the loop
Syntax
Statement x;
While (condition)
{
statement block;
}
Statement y;
WAP to calculate the sum
of first 10 nos.
int i= 1, sum= 0;
while(i<=10)
{
sum = sum +1;
i= i+1;
}
printf(“n sum =% d”, sum);
return 0;
}
OUTPUT
Sum= 55
DO WHILE
DO STATEMENT
 The do statement is a variant of the while statement that tests its
condition at the bottom of the loop.
General Form of the do Statement-
do
statement
while (expr);
next statement
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
17
do
{
body of the loop
}
while (test-condition);
EXAMPLE OF DO-WHILE STATEMENT
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
18
int i =1;
do
{
printf(“n % d”, i);
i=i+1;
}
while(i<=10);
return 0;
}
The code will print nos
from 1 to 10.
COMPARISON B/W WHILE, DO, FOR
WHILE DO FOR
while (...) {
...
continue;
...
cont: ;
}
do {
...
continue;
...
cont: ;
}
for (...) {
...
continue;
...
cont: ;
}
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
19
FOR LOOPS
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
20
SYNTAX OF FOR LOOP
 for (initialization ; test-condition ; increment)
{
body of the loop
}
Example
for (i = 1; i<=n ; i++)
{
printf(“n %d”, i);
}
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
21
MULTIPLE LOOP VARIABLES
 MLV are the loops which can be placed inside other loops.
Example:
………..
………..
while(……..)
{
for(………)
{ …….
……..
if (……..) goto end_of_program;
………
}
………
………
} end_of_program
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
22
Jumping out
of loops
USE OF BREAK AND CONTINUE STATEMENTS.
 The break statement causes an exit from the innermost enclosing
loop or switch statement.
 The continue statement causes the current iteration of a loop to
stop and the next iteration to begin immediately.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
23
DIFFERENCE B/W BREAK & CONTINUE
break continue
while (……)
{
if (condition)
break;
………..
}
……….
Transfers control out of the loop while.
Example
int i = 1;
while (i<= 10)
{
if (i==5)
break;
printf(“n % d”, i);
i=i+1; }
return 0; }
while (……)
{
……….
if (condition)
continue;
………..
}
Transfers control to the condition
expression of the while loop.
Example
int i = 1;
for (i=1; i<=10; i++)
{
if (i==5)
continue;
printf(“t % d”, i);
}
return 0; }
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
24
THE GOTO STATEMENT
 GOTO is used to branch unconditionally from one point to another.
 The general forms of goto & label statements are shown below:
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
25
goto label;
………….
………….
………….
label;
Statement;
label;
statement;
………….
………….
………….
goto label;
Forward jump Backward jump
FUNCTIONS:
 A complex problem is often easier to solve by dividing it into several
smaller parts, each of which can be solved by itself.
 This is called structured programming.
 These parts are sometimes made into functions in C.
 main() then uses these functions to solve the original problem.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
26
Main() Function Calls Func1()
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
27
main()
{
…………
func1();
…………
return 0;
}
func1()
{
Statement block;
}
Main
function
Function A Function B
Function B1 Function B2
Function c
DEFINITION OF FUNCTIONS:
A function definition shall include the following elements:
1. Function name;
2. Function type;
3. List of parameters;
4. Local variable declarations;
5. Function statements; and
6. A return statement.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
28
Function header
Function body
FUNCTIONS WITH ARRAY
 Example, the call
largest (a, n)
Will pass the whole array a to the called function.
The largest function header might look like:
float largest (float array [ ], int size)
The declaration of the formal argument array is made as follows:
float array [ ];
The pair of brackets informs the compiler that the argument array is as
array of numbers.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
29
PASSING VALUES TO FUNCTIONS
 Three rules to pass an array to a function:
A. The function must be called by passing only the name of the
array.
B. In the function definition, the formal parameter must be an array
type; the size of the array does not need to be specified.
C. The function prototype must show that the argument is an array.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
30
RECURSIVE FUNCTIONS
Recursion: the ability of a subprogram to call itself.
 Each recursive solution has at least two cases
 base case: the one to which we have an answer
 general case: expresses the solution in terms of a call to itself with a smaller
version of the problem.
 For example, the factorial of a number is defined as the number times the product of all
the numbers between itself and 0:
N! = N * (N  1)!
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
31

More Related Content

PDF
Compiler unit 2&3
PPTX
Introduction to C programming
PDF
C programming session8
PPT
Cd2 [autosaved]
PPTX
Overview of c language
PDF
Compiler unit 4
PPTX
C LANGUAGE - BESTECH SOLUTIONS
PDF
Compiler Design File
Compiler unit 2&3
Introduction to C programming
C programming session8
Cd2 [autosaved]
Overview of c language
Compiler unit 4
C LANGUAGE - BESTECH SOLUTIONS
Compiler Design File

What's hot (19)

PPT
C language Unit 2 Slides, UPTU C language
DOC
Storage classess of C progamming
DOCX
Basic structure of c programming
PDF
C language
PDF
C programming session3
PDF
C programming session5
PPTX
C language ppt
PPTX
Storage classes in c language
DOCX
PPTX
Intermediate code- generation
PDF
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
PDF
C programming session7
PPTX
Preprocessor directives in c language
PDF
C programming session10
PPTX
Compiler Engineering Lab#3
PDF
Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...
PPT
Basics of C programming
PPSX
Complete C programming Language Course
PPTX
Programming in C Presentation upto FILE
C language Unit 2 Slides, UPTU C language
Storage classess of C progamming
Basic structure of c programming
C language
C programming session3
C programming session5
C language ppt
Storage classes in c language
Intermediate code- generation
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
C programming session7
Preprocessor directives in c language
C programming session10
Compiler Engineering Lab#3
Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...
Basics of C programming
Complete C programming Language Course
Programming in C Presentation upto FILE
Ad

Similar to Unit3 cspc (20)

PPTX
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PDF
Operators, control statements represented in java
PDF
Introduction to C Language - Version 1.0 by Mark John Lado
PDF
IRJET- Switch Case Statements in C
PDF
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
PPTX
Presentation of loops
PPT
Control statements and functions in c
PPT
Introduction to C Programming
PPTX
C Programming Unit-2
DOCX
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
PDF
Chapter 3 - Flow of Control Part II.pdf
PDF
Vlsiexpt 11 12
PPT
Lecture05 abap on line
PPS
Programming in Arduino (Part 2)
PPT
Section06-Syncopkojiojoijnnjkhuubgfffppt
PDF
C programming session3
PPT
UNIT 2 PY.ppt - CONTROL STATEMENT IN PYTHON
PPTX
Java 8 streams
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
Operators, control statements represented in java
Introduction to C Language - Version 1.0 by Mark John Lado
IRJET- Switch Case Statements in C
TSR CLASS CD-UNIT 5.pdf sddfsfdsfqweqdew
Presentation of loops
Control statements and functions in c
Introduction to C Programming
C Programming Unit-2
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
Chapter 3 - Flow of Control Part II.pdf
Vlsiexpt 11 12
Lecture05 abap on line
Programming in Arduino (Part 2)
Section06-Syncopkojiojoijnnjkhuubgfffppt
C programming session3
UNIT 2 PY.ppt - CONTROL STATEMENT IN PYTHON
Java 8 streams
Ad

More from BBDITM LUCKNOW (17)

PPT
Unit 5 cspc
PPT
Unit 4 cspc
PPT
Cse ppt 2018
PPT
Binary system ppt
PPT
Unit 4 ca-input-output
PPTX
Unit 3 ca-memory
PPT
Unit 2 ca- control unit
PPTX
Unit 1 ca-introduction
PPTX
PPT
Bnf and ambiquity
PPT
Minimization of dfa
PPTX
Passescd
PDF
Compiler unit 1
PDF
Compiler unit 5
PDF
Cspc final
PPTX
Validation based protocol
Unit 5 cspc
Unit 4 cspc
Cse ppt 2018
Binary system ppt
Unit 4 ca-input-output
Unit 3 ca-memory
Unit 2 ca- control unit
Unit 1 ca-introduction
Bnf and ambiquity
Minimization of dfa
Passescd
Compiler unit 1
Compiler unit 5
Cspc final
Validation based protocol

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
master seminar digital applications in india
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Lesson notes of climatology university.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Structure & Organelles in detailed.
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Types and Its function , kingdom of life
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
GDM (1) (1).pptx small presentation for students
master seminar digital applications in india
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Insiders guide to clinical Medicine.pdf
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O5-L3 Freight Transport Ops (International) V1.pdf

Unit3 cspc

  • 1. CSPC-COMPUTER SYSTEMS PROGRAMMING IN ‘C’ ANKUR SRIVASTAVA DEPARTMENT OF COMPUTER SCIENCE 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 1
  • 2. CONDITIONAL PROGRAM EXECUTION: APPLYING IF AND SWITCH STATEMENTS, NESTING IF AND ELSE, USE OF BREAK AND DEFAULT WITH SWITCH. PROGRAM LOOPS AND ITERATIONS: USE OF WHILE, DO WHILE AND FOR LOOPS, MULTIPLE LOOP VARIABLES, USE OF BREAK AND CONTINUE STATEMENTS. FUNCTIONS: INTRODUCTION, TYPES OF FUNCTIONS, FUNCTIONS WITH ARRAY, PASSING VALUES TO FUNCTIONS, RECURSIVE FUNCTIONS.  UNIT-3 TOPICS 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 2
  • 3. UNIT-3 CONDITIONAL PROGRAM EXECUTION  C supports two types of decision control statements. 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 3 Selection/Branching statement Conditional type if If-else If-else-if switch Unconditional type
  • 4. CONDITIONAL BRANCHING STATEMENTS:  These statements helps to jump from one part to another part.  Whether a particular condition is satisfied or not.  It includes:- ---- if statement ---- if- else statement ---- if- else- if statement ---- Switch statement 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 4
  • 5. IF STATEMENTS  The if statement allows the program to test the state of the program variables using a Boolean expression.  Syntax If (test expression) { Statement 1; …………… Statement n; } Statement x; 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 5 Test exp Statement block 1 Statement x FALSE TRUE
  • 6. IF-ELSE STATEMENT  The if-else statement expresses simplest decision making.  The syntax is if (expression) statement1 elseopt Statement2 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 6
  • 7. IF –ELSE STATEMENT 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 7
  • 8. NESTED IF-ELSE  When the if-else condition exists in another if-else condition, it is nested if-else.  Syntax If (test condition-1) { if (test condition-2) { Statement-1; } else { Statement-2; } } else { Statement-3; } Statement- x; 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 8
  • 9. IF-ELSE-IF STATEMENT  For testing additional conditions if-else-if statements is constructed. If (condition-1) Statement -1; Else if (condition-2) Statement-2; Else if (condition-3) Statement-3; Else if (condition-n) Statement –n; Else Default-statement; Statement-x; 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 9
  • 10. SWITCH STATEMENTS 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 10
  • 11. SWITCH STATEMENT  The switch statement is used to select one of several alternatives when the selection is based on the value of a single variable or an expression. switch (controlling expression) { case label1: statement1 break; case label2: statement2 break; …….. case labeln: statementn break; default: statementd; } 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 11 If the result of this controlling expression matches label1, execute staement1 and then break this switch block. If the result matches none of all labels, execute the default statementd.
  • 12. USE OF BREAK AND DEFAULT WITH SWITCH Switch (exp) { case value-1: block-1 break; case value-2: block-2 break; ……………. ……………. default: default-block break; } statement-x; 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 12
  • 13. PROGRAM LOOPS AND ITERATIONS:  In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied.  Two segments are: ------- body of the loop ------- control statement Depending on the position of the control statement in the loop, A control structure may be either— -------- entry controlled loop or -------- exit controlled loop. 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 13
  • 14. LOOP CONTROL STRUCTURES 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 14 True Test condition ? Test condition ? Body of the loop Body of the loop False False True EntryEntry (b) Exit controlled loop(a) Entry controlled loop
  • 15. USE OF WHILE: THE WHILE STATEMENT IS USED TO REPEAT A COURSE OF ACTION. 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 15
  • 16. THE WHILE STATEMENT 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 16 Format While (test condition) { Body of the loop Syntax Statement x; While (condition) { statement block; } Statement y; WAP to calculate the sum of first 10 nos. int i= 1, sum= 0; while(i<=10) { sum = sum +1; i= i+1; } printf(“n sum =% d”, sum); return 0; } OUTPUT Sum= 55
  • 17. DO WHILE DO STATEMENT  The do statement is a variant of the while statement that tests its condition at the bottom of the loop. General Form of the do Statement- do statement while (expr); next statement 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 17 do { body of the loop } while (test-condition);
  • 18. EXAMPLE OF DO-WHILE STATEMENT 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 18 int i =1; do { printf(“n % d”, i); i=i+1; } while(i<=10); return 0; } The code will print nos from 1 to 10.
  • 19. COMPARISON B/W WHILE, DO, FOR WHILE DO FOR while (...) { ... continue; ... cont: ; } do { ... continue; ... cont: ; } for (...) { ... continue; ... cont: ; } 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 19
  • 20. FOR LOOPS 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 20
  • 21. SYNTAX OF FOR LOOP  for (initialization ; test-condition ; increment) { body of the loop } Example for (i = 1; i<=n ; i++) { printf(“n %d”, i); } 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 21
  • 22. MULTIPLE LOOP VARIABLES  MLV are the loops which can be placed inside other loops. Example: ……….. ……….. while(……..) { for(………) { ……. …….. if (……..) goto end_of_program; ……… } ……… ……… } end_of_program 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 22 Jumping out of loops
  • 23. USE OF BREAK AND CONTINUE STATEMENTS.  The break statement causes an exit from the innermost enclosing loop or switch statement.  The continue statement causes the current iteration of a loop to stop and the next iteration to begin immediately. 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 23
  • 24. DIFFERENCE B/W BREAK & CONTINUE break continue while (……) { if (condition) break; ……….. } ………. Transfers control out of the loop while. Example int i = 1; while (i<= 10) { if (i==5) break; printf(“n % d”, i); i=i+1; } return 0; } while (……) { ………. if (condition) continue; ……….. } Transfers control to the condition expression of the while loop. Example int i = 1; for (i=1; i<=10; i++) { if (i==5) continue; printf(“t % d”, i); } return 0; } 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 24
  • 25. THE GOTO STATEMENT  GOTO is used to branch unconditionally from one point to another.  The general forms of goto & label statements are shown below: 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 25 goto label; …………. …………. …………. label; Statement; label; statement; …………. …………. …………. goto label; Forward jump Backward jump
  • 26. FUNCTIONS:  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.  This is called structured programming.  These parts are sometimes made into functions in C.  main() then uses these functions to solve the original problem. 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 26
  • 27. Main() Function Calls Func1() 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 27 main() { ………… func1(); ………… return 0; } func1() { Statement block; } Main function Function A Function B Function B1 Function B2 Function c
  • 28. DEFINITION OF FUNCTIONS: A function definition shall include the following elements: 1. Function name; 2. Function type; 3. List of parameters; 4. Local variable declarations; 5. Function statements; and 6. A return statement. 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 28 Function header Function body
  • 29. FUNCTIONS WITH ARRAY  Example, the call largest (a, n) Will pass the whole array a to the called function. The largest function header might look like: float largest (float array [ ], int size) The declaration of the formal argument array is made as follows: float array [ ]; The pair of brackets informs the compiler that the argument array is as array of numbers. 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 29
  • 30. PASSING VALUES TO FUNCTIONS  Three rules to pass an array to a function: A. The function must be called by passing only the name of the array. B. In the function definition, the formal parameter must be an array type; the size of the array does not need to be specified. C. The function prototype must show that the argument is an array. 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 30
  • 31. RECURSIVE FUNCTIONS Recursion: the ability of a subprogram to call itself.  Each recursive solution has at least two cases  base case: the one to which we have an answer  general case: expresses the solution in terms of a call to itself with a smaller version of the problem.  For example, the factorial of a number is defined as the number times the product of all the numbers between itself and 0: N! = N * (N  1)! 8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI 31