SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von Zambales National High School

Mehr von 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
 

Kürzlich hochgeladen

SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
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
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 

Kürzlich hochgeladen (20)

SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
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
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
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...
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 

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