SlideShare uma empresa Scribd logo
1 de 20
Control Structures
Lesson 9
MANOLO L. GIRON
RMTU
Structure of Programming Language
Control Structures
• is a control statement and the collection of statements whose execution it controls.
• also known as a construct, depicts the logical order of program instructions.
• Three basic control structures are;
1. Sequence or Assignment
2. Selection
3. Repetition
4. Unconditional Branching
Structure of Programming Language
SEQUENCE OR ASSIGNMENT
CONTROL STRUCTURE
• A sequence control structure shows one or more actions
following each other in order.
• Actions include inputs, processes, and outputs.
• All actions must be executed; that is, none can be skipped.
Structure of Programming Language
• Examples of actions are reading a record, calculating averages or
totals, and printing totals.
Example using C language
average = (2+3+4)/3;
Structure of Programming Language
SELECTION CONTROL STRUCTURE
• A selection control structure tells the program which action to
take, based on a certain condition.
• Selection statements fall into two general categories:
1. two-way
2. n-way, or multiple selection.
Structure of Programming Language
Two-Way Selection Statements
• The general form of a two-way
selector is as follows:
if (expression)
{ Block of statements; }
else
{ Block of statements; }
Example using C language
if (cows > 10)
{ printf("loads of them!n");}
Else
{printf("Executing else part...!n");}
Structure of Programming Language
USING TERNARY OPERATOR
• ? : Operator
• The ? : operator is just like an if ... else statement except that because it is an operator
you can use it within expressions.
• ? : is a ternary operator in that it takes three values, this is the only ternary operator C
has.
• ? : takes the following form:
• if condition is true ? then X return value : otherwise Y value;
Structure of Programming Language
USING TERNARY OPERATOR
Example using C language
#include <stdio.h>
main()
{
int a , b;
a = 10;
printf( "Value of b is %dn", (a == 1) ? 20: 30 );
printf( "Value of b is %dn", (a == 10) ? 20: 30 );
}
Structure of Programming Language
N-WAY, OR MULTIPLE SELECTION
• The multiple-selection statement allows the selection of one of
any number of statements or statement groups. It is, therefore, a
generalization of a selector.
• In fact, two-way selectors can be built with a multiple selector
using ELSEIF.
Structure of Programming Language
N-WAY, OR
MULTIPLE
SELECTION
ELSEIF
Else-if statements are
based on the common
mathematics statement,
the conditional
expression.
General Form;
If (control_expression 1)
{statement 1}
else if(control_expression 2)
{statement 2}
else if(control_expression 3)
{statement 3}
else
{statement}
Structure of Programming Language
N-WAY, OR MULTIPLE SELECTION
• Example using C language
• if (cows == 5 )
• { printf("We have 5 cowsn"); }
• else if (cows == 6 )
• { printf("We have 6 cowsn"); }
• else if (cows == 7 )
• { printf("We have 7 cowsn"); }
Structure of Programming Language
N-WAY, OR MULTIPLE SELECTION
SWITCH
• This allows control to
flow through more
than one selectable
code segment on a
single execution.
General form is
switch( expression )
{
case constant-expression1: statements1;
[case constant-expression2: statements2;]
[case constant-expression3: statements3;]
[default : statements4;]
}
Structure of Programming Language
• char Grade = 'B';
• switch( Grade )
• {
• case 'A' : printf( "Excellentn" );
• break;
• case 'B' : printf( "Goodn" );
• break;
• case 'C' : printf( "OKn" );
• break;
• case 'D' : printf( "Mmmmm....n" );
• break;
• case 'F' : printf( "You must do better than thisn" );
• break;
• default : printf( "What is your grade anyway?n" );
}
Produce result
Good
Structure of Programming Language
Repetition Control Structure
• The repetition control structure enables a program to perform
one or more actions repeatedly as long as a certain condition is
met.
• Many programmers refer to this construct as a loop.
Structure of Programming Language
FOR Statement
• The general form of C’s for statement is
for( expression1; expression2; expression3)
{
Single statement
or
Block of statements;
}
#include <stdio.h>
main()
{
int i;
int j = 10;
for( i = 0; i <= j; i ++ )
{
printf("Hello %dn", i );
}
}Structure of Programming Language
WHILE Statement
• The following forms:
while ( expression )
{
Single statement
or
Block of statements;
}
Example
#include <stdio.h>
main()
{
int i = 10;
while ( i > 0 )
{
printf("Hello %dn", i );
i = i -1;
}
}
Structure of Programming Language
do...while loop statement
• Basic syntax of do...while loop is as follows:
• do
• {
• Single statement
• or
• Block of statements;
• }while(expression);
#include <stdio.h>
main()
{
int i = 10;
do{
printf("Hello %dn", i );
i = i -1;
}while ( i > 0 );
}Structure of Programming Language
Unconditional Branching
• An unconditional branch statement transfers execution control to a specified
location in the program.
• The unconditional branch, or goto, is the most powerful statement for
controlling the flow of execution of a program’s statements.
• A command that transfers control to the location that is the value of the
variable.
Structure of Programming Language
• SYNTAX
GoTo command
Rem Program to demonstrate the
Rem GoTo command.
Rem OUTPUT: text on the screen
Cls
Print “Hello World!”
Print “How are you?”
A GoTo L1
B Print “I’m fine.”
C L1: Print “Goodbye!”
Structure of Programming Language
Assignment
1. Example of code Assignments statement using C#, Python, Ruby language.
2. Example of code programs using if_then_else statement in C#, Python, Ruby
language.
3. Example of code program using ELSEIF statement in C#, Python, Ruby language.
4. Example of program using Switch statement in C#, Python, Ruby language.
5. Example of program using any loop statement in C#, Python, Ruby language.
Structure of Programming Language

Mais conteúdo relacionado

Mais procurados

Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming languageVasavi College of Engg
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesVasavi College of Engg
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compilerSudhaa Ravi
 
Phases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingMukesh Tekwani
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diarySHARDA SHARAN
 
Modular programming
Modular programmingModular programming
Modular programmingbhuwanbist1
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprogramsbaran19901990
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programmingRumman Ansari
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questionsakila viji
 
C programming course material
C programming course materialC programming course material
C programming course materialRanjitha Murthy
 
phases of compiler-analysis phase
phases of compiler-analysis phasephases of compiler-analysis phase
phases of compiler-analysis phaseSuyash Srivastava
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Bhavin Darji
 
Qbesic programming class 9
Qbesic programming class 9Qbesic programming class 9
Qbesic programming class 9bhuwanbist1
 

Mais procurados (20)

Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 
Modular programming
Modular programmingModular programming
Modular programming
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 
Phases of the Compiler - Systems Programming
Phases of the Compiler - Systems ProgrammingPhases of the Compiler - Systems Programming
Phases of the Compiler - Systems Programming
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
Modular programming
Modular programmingModular programming
Modular programming
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
 
Programming in c by pkv
Programming in c by pkvProgramming in c by pkv
Programming in c by pkv
 
Unit 2
Unit 2Unit 2
Unit 2
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
 
C programming course material
C programming course materialC programming course material
C programming course material
 
Subprogram
SubprogramSubprogram
Subprogram
 
phases of compiler-analysis phase
phases of compiler-analysis phasephases of compiler-analysis phase
phases of compiler-analysis phase
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Qbesic programming class 9
Qbesic programming class 9Qbesic programming class 9
Qbesic programming class 9
 
Analysis of the source program
Analysis of the source programAnalysis of the source program
Analysis of the source program
 

Semelhante a 9. control statement

C_Language_PS&PC_Notes.ppt
C_Language_PS&PC_Notes.pptC_Language_PS&PC_Notes.ppt
C_Language_PS&PC_Notes.pptganeshkarthy
 
C prog ppt
C prog pptC prog ppt
C prog pptxinoe
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlENGWAU TONNY
 
NRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxNRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxKRIPABHARDWAJ1
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
APP_Unit 1_updated.pptx
APP_Unit 1_updated.pptxAPP_Unit 1_updated.pptx
APP_Unit 1_updated.pptxgogulram2
 
An Overview of Programming C
An Overview of Programming CAn Overview of Programming C
An Overview of Programming CNishargo Nigar
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarPRAVIN GHOLKAR
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxCheriviralaNikhil
 
Basic Information About C language PDF
Basic Information About C language PDFBasic Information About C language PDF
Basic Information About C language PDFSuraj Das
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorialSURBHI SAROHA
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAlpana Gupta
 

Semelhante a 9. control statement (20)

C_Language_PS&PC_Notes.ppt
C_Language_PS&PC_Notes.pptC_Language_PS&PC_Notes.ppt
C_Language_PS&PC_Notes.ppt
 
C programming
C programmingC programming
C programming
 
C prog ppt
C prog pptC prog ppt
C prog ppt
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
 
NRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxNRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptx
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
APP_Unit 1_updated.pptx
APP_Unit 1_updated.pptxAPP_Unit 1_updated.pptx
APP_Unit 1_updated.pptx
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
An Overview of Programming C
An Overview of Programming CAn Overview of Programming C
An Overview of Programming C
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. Gholkar
 
Session 3
Session 3Session 3
Session 3
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
final pl paper
final pl paperfinal pl paper
final pl paper
 
Basic Information About C language PDF
Basic Information About C language PDFBasic Information About C language PDF
Basic Information About C language PDF
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Loops in c
Loops in cLoops in c
Loops in c
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 

Mais de Zambales National High School

Mais de Zambales National High School (20)

8. digital integrated circuit
8. digital integrated circuit8. digital integrated circuit
8. digital integrated circuit
 
7. transformer and diode
7. transformer and diode7. transformer and diode
7. transformer and diode
 
5. resistor and capacitor application
5. resistor and capacitor application5. resistor and capacitor application
5. resistor and capacitor application
 
6. transistor
6. transistor6. transistor
6. transistor
 
4. resistor and capacitor
4. resistor and capacitor4. resistor and capacitor
4. resistor and capacitor
 
2. Basic Electronics Circuit
2. Basic Electronics Circuit2. Basic Electronics Circuit
2. Basic Electronics Circuit
 
3. basic electrical and electronic symbol
3. basic electrical and electronic symbol3. basic electrical and electronic symbol
3. basic electrical and electronic symbol
 
11. abstraction and capsulation
11. abstraction and capsulation11. abstraction and capsulation
11. abstraction and capsulation
 
8. data types
8. data types8. data types
8. data types
 
7. name binding and scopes
7. name binding and scopes7. name binding and scopes
7. name binding and scopes
 
6. describing syntax and semantics
6. describing syntax and semantics6. describing syntax and semantics
6. describing syntax and semantics
 
5. evolution
5. evolution5. evolution
5. evolution
 
4. processor
4. processor4. processor
4. processor
 
3. criteria
3. criteria3. criteria
3. criteria
 
2. pl domain
2. pl domain2. pl domain
2. pl domain
 
1. reason why study spl
1. reason why study spl1. reason why study spl
1. reason why study spl
 
18. the components of the system unit
18. the components of the system unit18. the components of the system unit
18. the components of the system unit
 
17. software for home, personal, and educational
17. software for home, personal, and educational17. software for home, personal, and educational
17. software for home, personal, and educational
 
16. graphics and multimedia software
16. graphics and multimedia software16. graphics and multimedia software
16. graphics and multimedia software
 
15. business software
15. business software15. business software
15. business software
 

Último

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 

Último (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 

9. control statement

  • 1. Control Structures Lesson 9 MANOLO L. GIRON RMTU Structure of Programming Language
  • 2. Control Structures • is a control statement and the collection of statements whose execution it controls. • also known as a construct, depicts the logical order of program instructions. • Three basic control structures are; 1. Sequence or Assignment 2. Selection 3. Repetition 4. Unconditional Branching Structure of Programming Language
  • 3. SEQUENCE OR ASSIGNMENT CONTROL STRUCTURE • A sequence control structure shows one or more actions following each other in order. • Actions include inputs, processes, and outputs. • All actions must be executed; that is, none can be skipped. Structure of Programming Language
  • 4. • Examples of actions are reading a record, calculating averages or totals, and printing totals. Example using C language average = (2+3+4)/3; Structure of Programming Language
  • 5. SELECTION CONTROL STRUCTURE • A selection control structure tells the program which action to take, based on a certain condition. • Selection statements fall into two general categories: 1. two-way 2. n-way, or multiple selection. Structure of Programming Language
  • 6. Two-Way Selection Statements • The general form of a two-way selector is as follows: if (expression) { Block of statements; } else { Block of statements; } Example using C language if (cows > 10) { printf("loads of them!n");} Else {printf("Executing else part...!n");} Structure of Programming Language
  • 7. USING TERNARY OPERATOR • ? : Operator • The ? : operator is just like an if ... else statement except that because it is an operator you can use it within expressions. • ? : is a ternary operator in that it takes three values, this is the only ternary operator C has. • ? : takes the following form: • if condition is true ? then X return value : otherwise Y value; Structure of Programming Language
  • 8. USING TERNARY OPERATOR Example using C language #include <stdio.h> main() { int a , b; a = 10; printf( "Value of b is %dn", (a == 1) ? 20: 30 ); printf( "Value of b is %dn", (a == 10) ? 20: 30 ); } Structure of Programming Language
  • 9. N-WAY, OR MULTIPLE SELECTION • The multiple-selection statement allows the selection of one of any number of statements or statement groups. It is, therefore, a generalization of a selector. • In fact, two-way selectors can be built with a multiple selector using ELSEIF. Structure of Programming Language
  • 10. N-WAY, OR MULTIPLE SELECTION ELSEIF Else-if statements are based on the common mathematics statement, the conditional expression. General Form; If (control_expression 1) {statement 1} else if(control_expression 2) {statement 2} else if(control_expression 3) {statement 3} else {statement} Structure of Programming Language
  • 11. N-WAY, OR MULTIPLE SELECTION • Example using C language • if (cows == 5 ) • { printf("We have 5 cowsn"); } • else if (cows == 6 ) • { printf("We have 6 cowsn"); } • else if (cows == 7 ) • { printf("We have 7 cowsn"); } Structure of Programming Language
  • 12. N-WAY, OR MULTIPLE SELECTION SWITCH • This allows control to flow through more than one selectable code segment on a single execution. General form is switch( expression ) { case constant-expression1: statements1; [case constant-expression2: statements2;] [case constant-expression3: statements3;] [default : statements4;] } Structure of Programming Language
  • 13. • char Grade = 'B'; • switch( Grade ) • { • case 'A' : printf( "Excellentn" ); • break; • case 'B' : printf( "Goodn" ); • break; • case 'C' : printf( "OKn" ); • break; • case 'D' : printf( "Mmmmm....n" ); • break; • case 'F' : printf( "You must do better than thisn" ); • break; • default : printf( "What is your grade anyway?n" ); } Produce result Good Structure of Programming Language
  • 14. Repetition Control Structure • The repetition control structure enables a program to perform one or more actions repeatedly as long as a certain condition is met. • Many programmers refer to this construct as a loop. Structure of Programming Language
  • 15. FOR Statement • The general form of C’s for statement is for( expression1; expression2; expression3) { Single statement or Block of statements; } #include <stdio.h> main() { int i; int j = 10; for( i = 0; i <= j; i ++ ) { printf("Hello %dn", i ); } }Structure of Programming Language
  • 16. WHILE Statement • The following forms: while ( expression ) { Single statement or Block of statements; } Example #include <stdio.h> main() { int i = 10; while ( i > 0 ) { printf("Hello %dn", i ); i = i -1; } } Structure of Programming Language
  • 17. do...while loop statement • Basic syntax of do...while loop is as follows: • do • { • Single statement • or • Block of statements; • }while(expression); #include <stdio.h> main() { int i = 10; do{ printf("Hello %dn", i ); i = i -1; }while ( i > 0 ); }Structure of Programming Language
  • 18. Unconditional Branching • An unconditional branch statement transfers execution control to a specified location in the program. • The unconditional branch, or goto, is the most powerful statement for controlling the flow of execution of a program’s statements. • A command that transfers control to the location that is the value of the variable. Structure of Programming Language
  • 19. • SYNTAX GoTo command Rem Program to demonstrate the Rem GoTo command. Rem OUTPUT: text on the screen Cls Print “Hello World!” Print “How are you?” A GoTo L1 B Print “I’m fine.” C L1: Print “Goodbye!” Structure of Programming Language
  • 20. Assignment 1. Example of code Assignments statement using C#, Python, Ruby language. 2. Example of code programs using if_then_else statement in C#, Python, Ruby language. 3. Example of code program using ELSEIF statement in C#, Python, Ruby language. 4. Example of program using Switch statement in C#, Python, Ruby language. 5. Example of program using any loop statement in C#, Python, Ruby language. Structure of Programming Language