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

What's hot

C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageRakesh Roshan
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c languagetanmaymodi4
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - BasicsNotes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - BasicsJay Baxi
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c languagetanmaymodi4
 
Compiler Engineering Lab#3
Compiler Engineering Lab#3Compiler Engineering Lab#3
Compiler Engineering Lab#3MashaelQ
 
Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...
Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...
Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...Jay Baxi
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programmingavikdhupar
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILEDipta Saha
 

What's hot (19)

C language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C languageC language Unit 2 Slides, UPTU C language
C language Unit 2 Slides, UPTU C language
 
Storage classess of C progamming
Storage classess of C progamming Storage classess of C progamming
Storage classess of C progamming
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
C language
C languageC language
C language
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
C programming session5
C programming  session5C programming  session5
C programming session5
 
C language ppt
C language pptC language ppt
C language ppt
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
Hd6
Hd6Hd6
Hd6
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - BasicsNotes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
Preprocessor directives in c language
Preprocessor directives in c languagePreprocessor directives in c language
Preprocessor directives in c language
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
Compiler Engineering Lab#3
Compiler Engineering Lab#3Compiler Engineering Lab#3
Compiler Engineering Lab#3
 
Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...
Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...
Notes: Verilog Part 2 - Modules and Ports - Structural Modeling (Gate-Level M...
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
 

Similar to Unit3 cspc

Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in javaTharuniDiddekunta
 
Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoMark John Lado, MIT
 
IRJET- Switch Case Statements in C
IRJET-  	  Switch Case Statements in CIRJET-  	  Switch Case Statements in C
IRJET- Switch Case Statements in CIRJET Journal
 
Presentation of loops
Presentation of loopsPresentation of loops
Presentation of loopsAhmad Kazmi
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in cvampugani
 
Introduction to C Programming
Introduction to C Programming Introduction to C Programming
Introduction to C Programming vampugani
 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYRajeshkumar Reddy
 
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
 
Vlsiexpt 11 12
Vlsiexpt 11 12Vlsiexpt 11 12
Vlsiexpt 11 12JINCY Soju
 
Lecture05 abap on line
Lecture05 abap on lineLecture05 abap on line
Lecture05 abap on lineMilind Patil
 
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...Frank Nielsen
 
Cordova training : Day 3 - Introduction to Javascript
Cordova training : Day 3 - Introduction to JavascriptCordova training : Day 3 - Introduction to Javascript
Cordova training : Day 3 - Introduction to JavascriptBinu Paul
 

Similar to Unit3 cspc (20)

Operators, control statements represented in java
Operators, control statements  represented in javaOperators, control statements  represented in java
Operators, control statements represented in java
 
Calfem34
Calfem34Calfem34
Calfem34
 
Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John Lado
 
IRJET- Switch Case Statements in C
IRJET-  	  Switch Case Statements in CIRJET-  	  Switch Case Statements in C
IRJET- Switch Case Statements in C
 
Presentation of loops
Presentation of loopsPresentation of loops
Presentation of loops
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in c
 
Introduction to C Programming
Introduction to C Programming Introduction to C Programming
Introduction to C Programming
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
 
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
 
Vlsiexpt 11 12
Vlsiexpt 11 12Vlsiexpt 11 12
Vlsiexpt 11 12
 
Lecture05 abap on line
Lecture05 abap on lineLecture05 abap on line
Lecture05 abap on line
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
Unit - 2.ppt
Unit - 2.pptUnit - 2.ppt
Unit - 2.ppt
 
Java 8 streams
Java 8 streams Java 8 streams
Java 8 streams
 
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 3) A Concise and Practical Introduction to Programming Algorithms in...
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Cordova training : Day 3 - Introduction to Javascript
Cordova training : Day 3 - Introduction to JavascriptCordova training : Day 3 - Introduction to Javascript
Cordova training : Day 3 - Introduction to Javascript
 
ES-CH5.ppt
ES-CH5.pptES-CH5.ppt
ES-CH5.ppt
 

More from BBDITM LUCKNOW (17)

Unit 5 cspc
Unit 5 cspcUnit 5 cspc
Unit 5 cspc
 
Unit 4 cspc
Unit 4 cspcUnit 4 cspc
Unit 4 cspc
 
Cse ppt 2018
Cse ppt 2018Cse ppt 2018
Cse ppt 2018
 
Binary system ppt
Binary system pptBinary system ppt
Binary system ppt
 
Unit 4 ca-input-output
Unit 4 ca-input-outputUnit 4 ca-input-output
Unit 4 ca-input-output
 
Unit 3 ca-memory
Unit 3 ca-memoryUnit 3 ca-memory
Unit 3 ca-memory
 
Unit 2 ca- control unit
Unit 2 ca- control unitUnit 2 ca- control unit
Unit 2 ca- control unit
 
Unit 1 ca-introduction
Unit 1 ca-introductionUnit 1 ca-introduction
Unit 1 ca-introduction
 
Yacc
YaccYacc
Yacc
 
Bnf and ambiquity
Bnf and ambiquityBnf and ambiquity
Bnf and ambiquity
 
Lex
LexLex
Lex
 
Minimization of dfa
Minimization of dfaMinimization of dfa
Minimization of dfa
 
Passescd
PassescdPassescd
Passescd
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Compiler unit 5
Compiler  unit 5Compiler  unit 5
Compiler unit 5
 
Cspc final
Cspc finalCspc final
Cspc final
 
Validation based protocol
Validation based protocolValidation based protocol
Validation based protocol
 

Recently uploaded

Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxDenish Jangid
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfbu07226
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXMIRIAMSALINAS13
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFVivekanand Anglo Vedic Academy
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chipsGeoBlogs
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxbennyroshan06
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePedroFerreira53928
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...Nguyen Thanh Tu Collection
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 

Recently uploaded (20)

Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Forest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDFForest and Wildlife Resources Class 10 Free Study Material PDF
Forest and Wildlife Resources Class 10 Free Study Material PDF
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 

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