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

9. control statement

  • 1.
    Control Structures Lesson 9 MANOLOL. GIRON RMTU Structure of Programming Language
  • 2.
    Control Structures • isa 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 CONTROLSTRUCTURE • 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 ofactions 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 Exampleusing 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 MULTIPLESELECTION • 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 statementsare 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 MULTIPLESELECTION • 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 MULTIPLESELECTION 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 • Thegeneral 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 • Thefollowing 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 • Anunconditional 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 RemProgram 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 ofcode 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