SlideShare a Scribd company logo
CONSTRUCTS
(IMPERATIVE PROGRAMMING
LANGUAGE)
Selection Constructs
Conditional Constructs
Looping Constructs
By: Jyoti Bhardwaj
Roll no: 04
F.Y. M.C.A
SELECTION CONSTRUCTS.
THE SELECTION CONSTRUCT:
The selection construct allows you to create a program that will select one of a
number of different alternative courses of action. It is also called the if..then..else
construct.
 A selection statement provides for selection between alternatives. We can identify two
types of selection constructs:
 If statements
 Case statements
IF STATEMENT:
 An if statement, sometimes referred to as a conditional, can be used in two forms:
 If condition then action1
if (condition1) action1;
if (condition2) action2;
.
.
.
if (conditionN) actionN;
 If condition then action1 else action2
if... else example
main()
{
int i;
printf("nEnter a number : ");
scanf("%d",&i);
if( i > 7 )
printf("I is greater than seven");
else
printf("I is not greater than seven");
}
Output:
Enter a number: 5
I is not greater than seven
CASE STATEMENT:
 Switch case statements are a substitute for long if statements that
compare a variable to several values. Case statements allow selection
from many alternatives.
 The switch expression is evaluated once.
 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
Using break keyword:
 If a condition is met in switch case then execution continues on into
the next case clause also if it is not explicitly specified that the
execution should exit the switch statement.This is achieved by
using break keyword.
What is default condition:
 If none of the listed conditions is met then default condition
executed.
The basic format for using switch case is outlined below:
switch(expression)
{
case 1:
code block
break;
case 2:
code block
break;
.
.
case n:
code block
break;
default:
default code block
}
Selection structure vary from language to language,
but they tend to agree on the following:
 Case Selection can appear in any order.
 Case Selections do not have to be consecutive, meaning that it is
valid to have case-1, and case-4 without case-2 and case-3.
 Case Selection must have distinct actions for each case,
otherwise we need to combine the actions to avoid conflict.
To Sum up, the effect of Switch structure is as follows:
 Evaluate the switch expression.
 Go to the case label having a constant value that matches the
value of the expression found in step 1; If a match is found, go
to the default label; if there is no default label; terminate the
switch.
 Terminate the switch when break statement in encountered.
Switch… case example:
main()
{
int Grade = 'B';
switch(Grade )
{
case 'A' : printf( "Excellentn" );
break;
case 'B' : printf( “Very Goodn" );
break;
case 'C' : printf( "Good n" );
break;
case 'D' : printf( “O.K. n" );
break;
case 'F' : printf( “Satisfactory n" );
break;
default : printf( “Fair n" );
break;
}
}
This will produce following result:
Good
CONDITIONALCONSTRUCTS.
 The conditional construct allows you to create a program that
will select one of a number of different alternative courses of
action. It is also called the if..then..else construct.
 The initial if statement checks a condition (in this case >= 50).
If the condition is true, then the series of commands following
it are executed. If the condition is false (i.e. the user enters a
mark less than 50), then the series of commands following the
else statement are executed.
Note:
 An if statement must check a condition and then be followed
by the word then.
 An if statement must have an end if to finish it off.
 An if statement does not have to have an else statement.
If…else Ladder:
if (expression)
{
Block of statements;
}
else
{
Block of statements;
}
if (expression)
{
Block of
statements;
}
else if(expression)
{
Block of
statements;
}
else
{
Block of
statements;
}
If…else Ladder:
if... else example
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
LOOPINGCONSTRUCTS:
The loop constructs is
used to execute a block
of code until the
condition becomes
expired.
Loop constructs saves
the programmer from
writing code multiple
times that has
repetitive in nature
Types of loop to handle looping requirements
LoopType Description
 while loop Repeats a statement or group of
statements while a given condition
is true. It tests the condition before
executing the loop body.
 for loop Execute a sequence of statements
multiple times.
 do...while loop Like a while statement, except that it
tests the condition at the end of the
loop body
 nested loops You can use one or more loop inside
any another while, for or do..while
loop.
The Infinite Loop:
 A loop becomes infinite loop if a condition never
becomes false.The for loop is traditionally used for this
purpose. Since none of the three expressions that form
the for loop are required, you can make an endless
loop by leaving the conditional expression empty.
 int main ()
{
for( ; ; )
{
printf("This loop will run forever.n");
}
return 0;
}
While loop example..
 In while loop control statement, loop is executed until condition becomes false.
 Syntax:
while( condition )
body;
 int main()
{
int i=3;
while(i<10)
{
printf("%dn",i); i++;
}
}
Output:
3 4 5 6 7 8 9
do...while loop examples
 In do..while loop control statement, while loop is executed irrespective of
the condition for first time. Then 2nd time onwards, loop is executed until
condition becomes false.
 int main()
{
int i=1;
do
{
printf("Value of i is %dn",i);
i++;
}
while(i<=4 && i>=2);
}
Output:
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4
For loop example.
In for loop control statement, loop is executed until condition becomes false.
 int main()
{
int i;
for(i=0;i<10;i++)
{
printf("%d ",i);
}
}
Output:
0 1 2 3 4 5 6 7 8 9
WhentoUseWhichLoop?
Ifyouknow(orcancalculate)
howmanyiterationsyouneed,
thenuseacounter-controlled(for
)loop.
Otherwise,ifitisimportantthat
theloopcompleteatleastonce
beforecheckingforthestopping
condition,
orifitisnotpossibleor
meaningfultocheckthestopping
conditionbeforetheloophas
executedatleastonce,
thenuseado-whileloop.
Otherwiseuseawhileloop.
Constructs (Programming Methodology)

More Related Content

PPSX
Break and continue
Frijo Francis
 
PPT
Java layoutmanager
Arati Gadgil
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPTX
Object oriented programming
Amit Soni (CTFL)
 
PPT
JDBC – Java Database Connectivity
Information Technology
 
PPTX
Introduction to php
shanmukhareddy dasi
 
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
PDF
Basic JavaScript Tutorial
DHTMLExtreme
 
Break and continue
Frijo Francis
 
Java layoutmanager
Arati Gadgil
 
Java exception handling
BHUVIJAYAVELU
 
Object oriented programming
Amit Soni (CTFL)
 
JDBC – Java Database Connectivity
Information Technology
 
Introduction to php
shanmukhareddy dasi
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Basic JavaScript Tutorial
DHTMLExtreme
 

What's hot (20)

PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPT
Methods in C#
Prasanna Kumar SM
 
PPTX
Divide and conquer - Quick sort
Madhu Bala
 
PPTX
Java string handling
Salman Khan
 
PPT
State Diagrams
Vaidik Trivedi
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
Nested loops
Neeru Mittal
 
PDF
Refactoring
Ricardo Terra
 
PPT
Java Servlets
BG Java EE Course
 
PPTX
7-NFA to Minimized DFA.pptx
SLekshmiNair
 
PDF
Android appwidget
Krazy Koder
 
PPT
3.8 quick sort
Krish_ver2
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
PPTX
Java servlets
yuvarani p
 
PPS
String and string buffer
kamal kotecha
 
PDF
Servlet and servlet life cycle
Dhruvin Nakrani
 
PPTX
Inheritance in java
Tech_MX
 
PPT
Java interfaces
Raja Sekhar
 
PPT
Data structures & algorithms lecture 3
Poojith Chowdhary
 
PPTX
Type casting
Ruchika Sinha
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
Methods in C#
Prasanna Kumar SM
 
Divide and conquer - Quick sort
Madhu Bala
 
Java string handling
Salman Khan
 
State Diagrams
Vaidik Trivedi
 
Applets in java
Wani Zahoor
 
Nested loops
Neeru Mittal
 
Refactoring
Ricardo Terra
 
Java Servlets
BG Java EE Course
 
7-NFA to Minimized DFA.pptx
SLekshmiNair
 
Android appwidget
Krazy Koder
 
3.8 quick sort
Krish_ver2
 
Java Server Pages(jsp)
Manisha Keim
 
Java servlets
yuvarani p
 
String and string buffer
kamal kotecha
 
Servlet and servlet life cycle
Dhruvin Nakrani
 
Inheritance in java
Tech_MX
 
Java interfaces
Raja Sekhar
 
Data structures & algorithms lecture 3
Poojith Chowdhary
 
Type casting
Ruchika Sinha
 
Ad

Viewers also liked (20)

PDF
4 c# programming constructs
Tuan Ngo
 
PPS
UNIX - Class3 - Programming Constructs
Nihar Ranjan Paital
 
PDF
CP Handout#2
trupti1976
 
PPT
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
PPT
8.3 program structure (1 hour)
akmalfahmi
 
PDF
CP Handout#5
trupti1976
 
DOC
Java programming lab assignments
rajni kaushal
 
PPTX
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
PPT
Apclass
geishaannealagos
 
PPT
Apclass (2)
geishaannealagos
 
PPT
Chapter 12 - File Input and Output
Eduardo Bergavera
 
PDF
Chapter 2 - Structure of C++ Program
Deepak Singh
 
PPTX
C++ lecture 04
HNDE Labuduwa Galle
 
PPT
Programming Methodology
Kulachi Hansraj Model School Ashok Vihar
 
PPTX
Loop c++
Mood Mood
 
PPTX
C++ programming (Array)
طارق بالحارث
 
PPT
User defined functions in C programmig
Appili Vamsi Krishna
 
PPT
Programming Methodology
archikabhatia
 
PPTX
Array in c++
Mahesha Mano
 
PDF
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
4 c# programming constructs
Tuan Ngo
 
UNIX - Class3 - Programming Constructs
Nihar Ranjan Paital
 
CP Handout#2
trupti1976
 
Chapter 13 - Inheritance and Polymorphism
Eduardo Bergavera
 
8.3 program structure (1 hour)
akmalfahmi
 
CP Handout#5
trupti1976
 
Java programming lab assignments
rajni kaushal
 
Pf cs102 programming-9 [pointers]
Abdullah khawar
 
Apclass (2)
geishaannealagos
 
Chapter 12 - File Input and Output
Eduardo Bergavera
 
Chapter 2 - Structure of C++ Program
Deepak Singh
 
C++ lecture 04
HNDE Labuduwa Galle
 
Loop c++
Mood Mood
 
C++ programming (Array)
طارق بالحارث
 
User defined functions in C programmig
Appili Vamsi Krishna
 
Programming Methodology
archikabhatia
 
Array in c++
Mahesha Mano
 
C++ ARRAY WITH EXAMPLES
Farhan Ab Rahman
 
Ad

Similar to Constructs (Programming Methodology) (20)

PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
PDF
Loops and conditional statements
Saad Sheikh
 
PPTX
Java Decision Control
Jayfee Ramos
 
PPTX
BSc. III Unit iii VB.NET
Ujwala Junghare
 
PPS
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
DOCX
Chapter 4(1)
TejaswiB4
 
PDF
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
PPTX
Control Statement programming
University of Potsdam
 
PPTX
C Programming Control Structures(if,if-else)
poonambhagat36
 
PPTX
Unit IV Array in VB.Net.pptx
Ujwala Junghare
 
PPTX
Decision statements in c laguage
Tanmay Modi
 
PPTX
Decision statements in c language
tanmaymodi4
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
Unit-2 control Structures.pptx.pptx20201
vpenubot
 
PPTX
DECISION MAKING AND BRANCHING - C Programming
MSridhar18
 
PPTX
Flow of control C ++ By TANUJ
TANUJ ⠀
 
PDF
Controls & Loops in C
Thesis Scientist Private Limited
 
PPT
Decision Making and Branching in C
RAJ KUMAR
 
PPTX
control-statements in C Language MH.pptx
mehedi_hasan
 
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
Loops and conditional statements
Saad Sheikh
 
Java Decision Control
Jayfee Ramos
 
BSc. III Unit iii VB.NET
Ujwala Junghare
 
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Chapter 4(1)
TejaswiB4
 
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
Control Statement programming
University of Potsdam
 
C Programming Control Structures(if,if-else)
poonambhagat36
 
Unit IV Array in VB.Net.pptx
Ujwala Junghare
 
Decision statements in c laguage
Tanmay Modi
 
Decision statements in c language
tanmaymodi4
 
Control statements in java
Madishetty Prathibha
 
Unit-2 control Structures.pptx.pptx20201
vpenubot
 
DECISION MAKING AND BRANCHING - C Programming
MSridhar18
 
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Controls & Loops in C
Thesis Scientist Private Limited
 
Decision Making and Branching in C
RAJ KUMAR
 
control-statements in C Language MH.pptx
mehedi_hasan
 

Recently uploaded (20)

PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Software Development Methodologies in 2025
KodekX
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Doc9.....................................
SofiaCollazos
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
This slide provides an overview Technology
mineshkharadi333
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 

Constructs (Programming Methodology)

  • 1. CONSTRUCTS (IMPERATIVE PROGRAMMING LANGUAGE) Selection Constructs Conditional Constructs Looping Constructs By: Jyoti Bhardwaj Roll no: 04 F.Y. M.C.A
  • 2. SELECTION CONSTRUCTS. THE SELECTION CONSTRUCT: The selection construct allows you to create a program that will select one of a number of different alternative courses of action. It is also called the if..then..else construct.  A selection statement provides for selection between alternatives. We can identify two types of selection constructs:  If statements  Case statements IF STATEMENT:  An if statement, sometimes referred to as a conditional, can be used in two forms:  If condition then action1 if (condition1) action1; if (condition2) action2; . . . if (conditionN) actionN;  If condition then action1 else action2
  • 3. if... else example main() { int i; printf("nEnter a number : "); scanf("%d",&i); if( i > 7 ) printf("I is greater than seven"); else printf("I is not greater than seven"); } Output: Enter a number: 5 I is not greater than seven
  • 4. CASE STATEMENT:  Switch case statements are a substitute for long if statements that compare a variable to several values. Case statements allow selection from many alternatives.  The switch expression is evaluated once.  The value of the expression is compared with the values of each case.  If there is a match, the associated block of code is executed. Using break keyword:  If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement.This is achieved by using break keyword. What is default condition:  If none of the listed conditions is met then default condition executed.
  • 5. The basic format for using switch case is outlined below: switch(expression) { case 1: code block break; case 2: code block break; . . case n: code block break; default: default code block }
  • 6. Selection structure vary from language to language, but they tend to agree on the following:  Case Selection can appear in any order.  Case Selections do not have to be consecutive, meaning that it is valid to have case-1, and case-4 without case-2 and case-3.  Case Selection must have distinct actions for each case, otherwise we need to combine the actions to avoid conflict. To Sum up, the effect of Switch structure is as follows:  Evaluate the switch expression.  Go to the case label having a constant value that matches the value of the expression found in step 1; If a match is found, go to the default label; if there is no default label; terminate the switch.  Terminate the switch when break statement in encountered.
  • 7. Switch… case example: main() { int Grade = 'B'; switch(Grade ) { case 'A' : printf( "Excellentn" ); break; case 'B' : printf( “Very Goodn" ); break; case 'C' : printf( "Good n" ); break; case 'D' : printf( “O.K. n" ); break; case 'F' : printf( “Satisfactory n" ); break; default : printf( “Fair n" ); break; } } This will produce following result: Good
  • 8. CONDITIONALCONSTRUCTS.  The conditional construct allows you to create a program that will select one of a number of different alternative courses of action. It is also called the if..then..else construct.  The initial if statement checks a condition (in this case >= 50). If the condition is true, then the series of commands following it are executed. If the condition is false (i.e. the user enters a mark less than 50), then the series of commands following the else statement are executed. Note:  An if statement must check a condition and then be followed by the word then.  An if statement must have an end if to finish it off.  An if statement does not have to have an else statement.
  • 9. If…else Ladder: if (expression) { Block of statements; } else { Block of statements; } if (expression) { Block of statements; } else if(expression) { Block of statements; } else { Block of statements; }
  • 11. if... else example int main() { int year; printf("Enter a year: "); scanf("%d",&year); if(year%4 == 0) { if( year%100 == 0) { if ( year%400 == 0) printf("%d is a leap year.", year); else printf("%d is not a leap year.", year); } else printf("%d is a leap year.", year ); } else printf("%d is not a leap year.", year); return 0; }
  • 12. LOOPINGCONSTRUCTS: The loop constructs is used to execute a block of code until the condition becomes expired. Loop constructs saves the programmer from writing code multiple times that has repetitive in nature
  • 13. Types of loop to handle looping requirements LoopType Description  while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.  for loop Execute a sequence of statements multiple times.  do...while loop Like a while statement, except that it tests the condition at the end of the loop body  nested loops You can use one or more loop inside any another while, for or do..while loop.
  • 14. The Infinite Loop:  A loop becomes infinite loop if a condition never becomes false.The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.  int main () { for( ; ; ) { printf("This loop will run forever.n"); } return 0; }
  • 15. While loop example..  In while loop control statement, loop is executed until condition becomes false.  Syntax: while( condition ) body;  int main() { int i=3; while(i<10) { printf("%dn",i); i++; } } Output: 3 4 5 6 7 8 9
  • 16. do...while loop examples  In do..while loop control statement, while loop is executed irrespective of the condition for first time. Then 2nd time onwards, loop is executed until condition becomes false.  int main() { int i=1; do { printf("Value of i is %dn",i); i++; } while(i<=4 && i>=2); } Output: Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4
  • 17. For loop example. In for loop control statement, loop is executed until condition becomes false.  int main() { int i; for(i=0;i<10;i++) { printf("%d ",i); } } Output: 0 1 2 3 4 5 6 7 8 9