0% found this document useful (0 votes)
7 views

Control Structure C Language

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Control Structure C Language

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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- Example using C language
way selector is as follows:
if (expression) if (cows > 10)
{ printf("loads of them!\
{ Block of statements; } n");}
else Else
{printf("Executing else
{ Block of statements; }
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 %d\n", (a == 1) ?
20: 30 );

printf( "Value of b is %d\n", (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 General Form;

ELSEIF If (control_expression 1)
{statement 1}
Else-if statements else if(control_expression 2)
are based on the {statement 2}
common else if(control_expression 3)
mathematics {statement 3}
statement, the else
{statement}
conditional
expression.
Structure of Programming Language
N-WAY, OR MULTIPLE
SELECTION
• Example using C language
• if (cows == 5 )
• { printf("We have 5 cows\n"); }
• else if (cows == 6 )
• { printf("We have 6 cows\n"); }
• else if (cows == 7 )
• { printf("We have 7 cows\n"); }
Structure of Programming Language
N-WAY, OR MULTIPLE
SELECTION
SWITCH General form is
switch( expression )
• This allows {
control to flow
case constant-expression1:
through more
statements1;
than one
[case constant-expression2:
selectable code
statements2;]
segment on a
[case constant-expression3:
single execution.
Structure of Programming Language statements3;]
[default : statements4;]
• char Grade = 'B';
• switch( Grade )
• {
• case 'A' : printf( "Excellent\n" );
• break;
• case 'B' : printf( "Good\n" ); Produce
• break; result
• case 'C' : printf( "OK\n" );
• break; Good
• case 'D' : printf( "Mmmmm....\n" );
• break;
• case 'F' : printf( "You must do better
than this\n" );
• break;
• default : printf( "What is your grade
Structure of Programming Language
anyway?\n" );
}
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 #include <stdio.h>
is main()
for( expression1; expression2; {
expression3) int i;
{ int j = 10;
Single statement for( i = 0; i <= j; i ++ )
or {
Block of statements;
printf("Hello %d\n", i );
}
}
Structure of Programming Language
}
WHILE Statement

• The following forms: Example


while ( expression ) #include <stdio.h>
main()
{ {
Single statement int i = 10;
while ( i > 0 )
or
{
Block of statements; printf("Hello %d\n", i );
} i = i -1;
}
}
Structure of Programming Language
do...while loop statement

• Basic syntax of do...while loop is #include <stdio.h>


as follows:
• do main()
{
• { int i = 10;
• Single statement
do{
• or
printf("Hello %d\
• Block of statements; n", i );
• }while(expression); i = i -1;
Structure of Programming Language
}while ( i > 0 );
}
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 Rem Program to demonstrate the
GoTo command 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,
Structure of Programming Language

Ruby language.

You might also like