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

PPTX
Types of methods in python
PPTX
Static Data Members and Member Functions
PPTX
Python Functions
PPTX
Constructor and Types of Constructors
PPTX
Control Statements in Java
ODP
Anatomy of android application
PPTX
class and objects
Types of methods in python
Static Data Members and Member Functions
Python Functions
Constructor and Types of Constructors
Control Statements in Java
Anatomy of android application
class and objects

What's hot (20)

PPT
FUNCTIONS IN c++ PPT
PDF
Comparison between runtime polymorphism and compile time polymorphism
PPTX
Database connectivity in python
PPTX
Introduction to Array ppt
PPS
Interface
PDF
Java Fundamentals
PDF
Friend function in c++
PDF
Python functions
PPT
Lecture 5 sorting and searching
PPTX
Templates in c++
PPTX
encapsulation
PPTX
Rabin karp string matching algorithm
PPTX
Exception handling c++
PDF
All pairs shortest path algorithm
PPT
16 virtual function
PDF
Lab report for Prolog program in artificial intelligence.
PPT
Oop java
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
If else statement in c++
FUNCTIONS IN c++ PPT
Comparison between runtime polymorphism and compile time polymorphism
Database connectivity in python
Introduction to Array ppt
Interface
Java Fundamentals
Friend function in c++
Python functions
Lecture 5 sorting and searching
Templates in c++
encapsulation
Rabin karp string matching algorithm
Exception handling c++
All pairs shortest path algorithm
16 virtual function
Lab report for Prolog program in artificial intelligence.
Oop java
Greedy Algorithm - Knapsack Problem
If else statement in c++
Ad

Viewers also liked (20)

PDF
4 c# programming constructs
PPS
UNIX - Class3 - Programming Constructs
PDF
CP Handout#2
PPT
Chapter 13 - Inheritance and Polymorphism
PPT
8.3 program structure (1 hour)
PDF
CP Handout#5
DOC
Java programming lab assignments
PPTX
Pf cs102 programming-9 [pointers]
PPT
PPT
Apclass (2)
PPT
Chapter 12 - File Input and Output
PDF
Chapter 2 - Structure of C++ Program
PPTX
C++ lecture 04
PPT
PPTX
Loop c++
PPTX
C++ programming (Array)
PPT
User defined functions in C programmig
PPT
Programming Methodology
PPTX
Array in c++
PDF
C++ ARRAY WITH EXAMPLES
4 c# programming constructs
UNIX - Class3 - Programming Constructs
CP Handout#2
Chapter 13 - Inheritance and Polymorphism
8.3 program structure (1 hour)
CP Handout#5
Java programming lab assignments
Pf cs102 programming-9 [pointers]
Apclass (2)
Chapter 12 - File Input and Output
Chapter 2 - Structure of C++ Program
C++ lecture 04
Loop c++
C++ programming (Array)
User defined functions in C programmig
Programming Methodology
Array in c++
C++ ARRAY WITH EXAMPLES
Ad

Similar to Constructs (Programming Methodology) (20)

PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
PDF
Loops and conditional statements
PPTX
Java Decision Control
PPTX
BSc. III Unit iii VB.NET
PPS
Programming in Arduino (Part 2)
DOCX
Chapter 4(1)
PDF
Unit 2=Decision Control & Looping Statements.pdf
PPTX
Control Statement programming
PPTX
C Programming Control Structures(if,if-else)
PPTX
Unit IV Array in VB.Net.pptx
PPTX
Decision statements in c language
PPTX
Decision statements in c laguage
PPTX
Control statements in java
PPTX
Unit-2 control Structures.pptx.pptx20201
PPTX
DECISION MAKING AND BRANCHING - C Programming
PPTX
Flow of control C ++ By TANUJ
PDF
Controls & Loops in C
PPT
Decision Making and Branching in C
PPTX
control-statements in C Language MH.pptx
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Loops and conditional statements
Java Decision Control
BSc. III Unit iii VB.NET
Programming in Arduino (Part 2)
Chapter 4(1)
Unit 2=Decision Control & Looping Statements.pdf
Control Statement programming
C Programming Control Structures(if,if-else)
Unit IV Array in VB.Net.pptx
Decision statements in c language
Decision statements in c laguage
Control statements in java
Unit-2 control Structures.pptx.pptx20201
DECISION MAKING AND BRANCHING - C Programming
Flow of control C ++ By TANUJ
Controls & Loops in C
Decision Making and Branching in C
control-statements in C Language MH.pptx

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
KodekX | Application Modernization Development
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Electronic commerce courselecture one. Pdf
PDF
Approach and Philosophy of On baking technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Understanding_Digital_Forensics_Presentation.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KodekX | Application Modernization Development
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Reach Out and Touch Someone: Haptics and Empathic Computing
Diabetes mellitus diagnosis method based random forest with bat algorithm
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
MYSQL Presentation for SQL database connectivity
Review of recent advances in non-invasive hemoglobin estimation
GamePlan Trading System Review: Professional Trader's Honest Take
Electronic commerce courselecture one. Pdf
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
cuic standard and advanced reporting.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

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