Sei sulla pagina 1di 28

Lesson 5

CONTROL
STRUCTURES
Relational and Logical Operators
Relational – used to perform comparisons in most
conditions designed to control the structures of C.
Logical – used to form more complicated conditions
or logical expressions.
Relational /Logical Meaning
== Exactly equal to
!== Not equal to Shorthand
Operator:
< Less than
<= Less than equal to Use of + =
> Greater than
Ex: x=x+10
>= Greater than equal to
x+=10
&& AND
|| OR
! NOT
The If Condition
If statement
-is an example of a control structure. A control
structure controls the flow of execution of a
program or function.
If condition – selects the statement following
the condition, if the condition is evaluated as
true, it allows the program to execute the
statement.
Simple If Statement

If statements with a single alternative executes only


when the condition is true. Otherwise the statement is
not performed if it is false.

General Form: Example:

if (condition) if (x > 0)
statement; printf (“ x is a positive
number”);
Rules in using If Statement

Statements may either a single statement or a


compound statement.
 If a single statement is used, there is no need to
put a beginning and an ending braces
 If a compound statement follows the condition, it
is necessary to enclose the statements in a
beginning and ending brace ({ }).
If… Else Statement

General Form: Example 1:

if (condition) if (rest_heartrate > 56)


statement; printf (“Keep up your
else exercise program !\n”);
statement; else
printf (“Your heart is in
excellent health!\n”);
If…Else Statement

Example 2: Example 3:

if (years > 5) if (x >= 0.0)


printf (“there is printf
bonus\n”) (*positive\n*);
else else
printf(“There is no printf
bonus”) (*negative\n*);
General form if condition with a
compound statement:
if (condition) {
statement1;
..................
statementN;
}
else {
statement1;
..................
statementN;
}
If-Else-If Using Compound Expressions

If the statement is obeyed the program continues with the


next instruction following the if statement,
if not the statement is ignored and the program continues
with the next statement.
General Form:
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
else
statement 3;
If-Else –if Using Compound
Expressions
Example
if (score>=80) {
printf (“grade is A”);
printf (“You’ve passed”) }
else if (score>=75) {
printf (“grade is B”);
printf (“You’ve passed”)}
else if (score>=60)
printf (“grade is C”);
else
printf (“grade is D”);
Sample Program 1
Sample Program 2
The SWITCH Statement
The switch construction is another way of
making a program path branch into lots of
different limbs. It can be used as a different
way of writing a string of if .. else statements,
but it is more versatile than that and it only
works for integers and character type values.
The switch statement is where the value of a
variable is successively compared with
different values.
Switch Diagram
3 Important Things To Know
About Switch Statement

The switch statement differs from the if statement


in that the switch can only test for equality whereas
the if can evaluate a relational or logical
expressions.
No two case constants in the same switch can have
identical values
If character constants are used in the switch, they
are automatically converted to their integer values.
General Form of Switch Statement

switch (integer value)


{ case 1: statement1; break; /* optional line */
case 2: statement2; break; /* optional line */
....
case n: statement2; break; /* optional line */
default: default statement break; /* optional line */
}
Break and Exit Functions

Break () – has two uses; first is to terminate a case in


the switch statement and second, to terminate loop
and bypass the normal loop conditional tests.

Exit() – found in the standard library (stdlib.h)


causes immediate termination of the entire program.
Sample Switch Program

int digit;
{
switch (digit)
{ case 0 : printf (“zero"); break;
case 1 : printf (“one"); break;
case 2 : printf (“two"); break;
case 3 : printf (“three"); break;
default : printf (“wala lang"); break; }
}
The Looping Statements
The For Loop Statement - normally has the
characteristic feature of controlling one particular
variable, called the control variable. It allows a set of
instructions to be performed until a certain condition
is reached. initializes the control variable
The general form of the for loop is:
for (initialization; loop condition; incr or decr)
{
statement sequence Increment or decrement the
variable
} condition to be evaluated
Sample 1

#include <stdio.h>
int main()
{
  int i;
   for (i = 1; i <= 10; i++)
{
printf("%d\n", i);
}
  }
Sample 2
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("\n Sum = %d", sum);
return 0;
}
Loop Exercise #1

Revise Sample 2 Program to generate the ff output:

A) B)
The While Statement

The while statement gives control in executing a


large amount of data repeatedly.

General Form:
while(condition)
{statement sequence};
next statement;
While Statement Example
Write a program that will get the sum of ten
consecutive integers.
#include <stdio.h>
int main()
{
int i=0;
while(i<=10)
{
++i;
printf("\n %d",i); }
return 0;
}
While Statement Example
Write a program that will get the sum of ten
consecutive integers.

#include <stdio.h>
int main()
{ int sum=0;
int i=1;
while(i<=10)
{ sum+=i;
printf("\n %d",sum);
++i;
}
return 0;
}
The Do-While Statement

The Do-While Statement is a variation of the while


statement. It is an interaction structure of C that
continuously executes a statement sequence as long
as the condition after the while statement is true.
General Statement:
do {
{statement sequence}
while(condition);
next statement;
Do-While Statement Example

sum=0;
x=0;
do
{
sum+=1;
++x;
}
while (x<=10);
While Loop Sample

Potrebbero piacerti anche