SlideShare a Scribd company logo
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1
'
&
$
%
for Statement
Another iterative construct in C language is the
for-statement (loop). The structure or the
syntax of this statement is,




for (exp1; exp2; exp3) statement
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 2
'

$
%
for Loop
true false
exp1
exp2
statement
exp3
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 3
'

$
%
for Statement
• The expression1 is used for initialization
before entering the loop.
• The expression2 is to control the loop,
whether to enter the loop or to skip it.
• The expression3 executed after the execution
of the statement part of the loop, is used
essentially to update the loop control
condition.
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 4
'

$
%
for Statement
• All three expressions can be omitted. The
omitted control expression, expression2,
makes the condition true.
• The statement can also be null ‘;’.
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 5
'

$
%
Example III
We write program to solve the problem of
example III, the sum of first n natural numbers.
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 6
'

$
%
#include stdio.h
int main() // temp29.c
{
int n, i, sum = 0;
printf(Enter a +ve integer: );
scanf(%d, n);
for(i=1, sum=0; i=n; ++i)
sum += i;
printf(0+ ... + %d = %dn, n, sum);
return 0;
}
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 7
'

$
%
for Statement




for (i=1, sum=0; i=n; ++i)
sum + = i;
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 8
'

$
%
expression1
expression1 is ‘i=1, sum=0’. We have used the
comma operator (,). This is evaluated left to
right and is of lowest precedence among the
operators. The value of the expression is the
value of the rightmost operand. It is sum = 0
in this case.
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 9
'

$
%
while and for Loops
A while-statement can be simulated by a
for-statement.




while(exp) stmt ≡ for(; exp;) stmt
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 10
'

$
%
while andfor Loops
Similarly a for-statement can be simulated by
a while-statement and expression statements.
'

$
%
for(exp1; exp2; exp3) stmt
≡
exp1; while(exp2) {stmt exp3;}
This equivalence is not true if there is a
continue statement in stmt.
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 11
'

$
%
Example IV
Read n int data and print the largest among
them.
The first input is the number of data, n.
Following inputs are a sequence of n int data.
It is essential to print the input.
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 12
'

$
%
Inductive Definition
lrgst(d1, d2, · · · , dn) =



d1 if n = 1,
max(d1, lrgst(d2, · · · , dn)) if n  1.
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 13
'

$
%
Sequence of Operations
• Read the number of data n(≥ 1).
• Read the first data in a variable named
largesta
.
• In a for-loop read the ith
data (i = 2 · · · n).
If it is larger than the data present in
largest, copy it to largest.
aUpto this point of execution, this is the largest data.
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 14
'

$
%
#include stdio.h
int main() // temp30.c
{
int n, largest, i;
printf(Enter a +ve integer: );
scanf(%d, n);
printf(Enter %d integers:n, n);
scanf(%d, largest);
printf(Input data: %d , largest) ;
for(i=2; i=n; ++i) {
int temp ; // local to block
scanf(%d, temp);
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 15
'

$
%
printf(%d , temp) ;
if (temp  largest) largest = temp ;
}
printf(nLargest: %dn, largest);
return 0;
}
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 16
'

$
%
Note
• It is not necessary to know the number of
data a priori.
• We shall use EOF (end-of-file) from the
keyboard (Ctrl+D) to terminate the input.
• Every call to scanf() returns the number of
data read. If the Ctrl+D is pressed, scanf()
returns a special constant EOF (end-of-file,
defined in stdio.h).
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 17
'

$
%
#include stdio.h
int main() // temp31.c
{
int largest, count = 0, temp;
printf(Enter integer datan);
printf(Terminate by Ctrl+Dn);
scanf(%d, largest); // at least one data
printf(Input data: %d , largest) ;
++count ;
for(; scanf(%d, temp) != EOF;) {
printf(%d , temp);
if (temp  largest) largest = temp ;
++count;
Lect 9 Goutam Biswas
PDS: CS 11002 Computer Sc  Engg: IIT Kharagpur 18
'

$
%
}
printf(nLargest among %d data: %dn,
count, largest);
return 0;
}
Lect 9 Goutam Biswas

More Related Content

Similar to C.pdf

C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
khaledahmed316
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdfModule 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Flow of control ppt
Flow of control pptFlow of control ppt
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Chandrakant Divate
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
Looping statements
Looping statementsLooping statements
Looping statements
Chukka Nikhil Chakravarthy
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
Rakesh Roshan
 
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
Priyom Majumder
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Chandrakant Divate
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
Hassaan Rahman
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 

Similar to C.pdf (20)

C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdfModule 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
Looping statements
Looping statementsLooping statements
Looping statements
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
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
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 

Recently uploaded

APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
kalichargn70th171
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 

Recently uploaded (20)

APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
Why Mobile App Regression Testing is Critical for Sustained Success_ A Detail...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 

C.pdf

  • 1. PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 ' & $ % for Statement Another iterative construct in C language is the for-statement (loop). The structure or the syntax of this statement is, for (exp1; exp2; exp3) statement Lect 9 Goutam Biswas
  • 2. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 2 ' $ % for Loop true false exp1 exp2 statement exp3 Lect 9 Goutam Biswas
  • 3. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 3 ' $ % for Statement • The expression1 is used for initialization before entering the loop. • The expression2 is to control the loop, whether to enter the loop or to skip it. • The expression3 executed after the execution of the statement part of the loop, is used essentially to update the loop control condition. Lect 9 Goutam Biswas
  • 4. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 4 ' $ % for Statement • All three expressions can be omitted. The omitted control expression, expression2, makes the condition true. • The statement can also be null ‘;’. Lect 9 Goutam Biswas
  • 5. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 5 ' $ % Example III We write program to solve the problem of example III, the sum of first n natural numbers. Lect 9 Goutam Biswas
  • 6. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 6 ' $ % #include stdio.h int main() // temp29.c { int n, i, sum = 0; printf(Enter a +ve integer: ); scanf(%d, n); for(i=1, sum=0; i=n; ++i) sum += i; printf(0+ ... + %d = %dn, n, sum); return 0; } Lect 9 Goutam Biswas
  • 7. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 7 ' $ % for Statement for (i=1, sum=0; i=n; ++i) sum + = i; Lect 9 Goutam Biswas
  • 8. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 8 ' $ % expression1 expression1 is ‘i=1, sum=0’. We have used the comma operator (,). This is evaluated left to right and is of lowest precedence among the operators. The value of the expression is the value of the rightmost operand. It is sum = 0 in this case. Lect 9 Goutam Biswas
  • 9. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 9 ' $ % while and for Loops A while-statement can be simulated by a for-statement. while(exp) stmt ≡ for(; exp;) stmt Lect 9 Goutam Biswas
  • 10. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 10 ' $ % while andfor Loops Similarly a for-statement can be simulated by a while-statement and expression statements. ' $ % for(exp1; exp2; exp3) stmt ≡ exp1; while(exp2) {stmt exp3;} This equivalence is not true if there is a continue statement in stmt. Lect 9 Goutam Biswas
  • 11. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 11 ' $ % Example IV Read n int data and print the largest among them. The first input is the number of data, n. Following inputs are a sequence of n int data. It is essential to print the input. Lect 9 Goutam Biswas
  • 12. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 12 ' $ % Inductive Definition lrgst(d1, d2, · · · , dn) =    d1 if n = 1, max(d1, lrgst(d2, · · · , dn)) if n 1. Lect 9 Goutam Biswas
  • 13. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 13 ' $ % Sequence of Operations • Read the number of data n(≥ 1). • Read the first data in a variable named largesta . • In a for-loop read the ith data (i = 2 · · · n). If it is larger than the data present in largest, copy it to largest. aUpto this point of execution, this is the largest data. Lect 9 Goutam Biswas
  • 14. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 14 ' $ % #include stdio.h int main() // temp30.c { int n, largest, i; printf(Enter a +ve integer: ); scanf(%d, n); printf(Enter %d integers:n, n); scanf(%d, largest); printf(Input data: %d , largest) ; for(i=2; i=n; ++i) { int temp ; // local to block scanf(%d, temp); Lect 9 Goutam Biswas
  • 15. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 15 ' $ % printf(%d , temp) ; if (temp largest) largest = temp ; } printf(nLargest: %dn, largest); return 0; } Lect 9 Goutam Biswas
  • 16. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 16 ' $ % Note • It is not necessary to know the number of data a priori. • We shall use EOF (end-of-file) from the keyboard (Ctrl+D) to terminate the input. • Every call to scanf() returns the number of data read. If the Ctrl+D is pressed, scanf() returns a special constant EOF (end-of-file, defined in stdio.h). Lect 9 Goutam Biswas
  • 17. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 17 ' $ % #include stdio.h int main() // temp31.c { int largest, count = 0, temp; printf(Enter integer datan); printf(Terminate by Ctrl+Dn); scanf(%d, largest); // at least one data printf(Input data: %d , largest) ; ++count ; for(; scanf(%d, temp) != EOF;) { printf(%d , temp); if (temp largest) largest = temp ; ++count; Lect 9 Goutam Biswas
  • 18. PDS: CS 11002 Computer Sc Engg: IIT Kharagpur 18 ' $ % } printf(nLargest among %d data: %dn, count, largest); return 0; } Lect 9 Goutam Biswas