Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CSC099 Week 13 Flexible Learning

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

CSC099 Week 13

(20 – 25 April 2020)


Sir Jasrul Nizam
Flexible Learning
switch Statement

• Occasionally, an algorithm will contain a series of decisions in


which a variable or expression is tested separately for each of
the constant integral values it may assume, and different
actions are taken.

• This is called multiple selection.

• C provides the switch multiple-selection statement to handle


such decision making.

• The switch statement consists of a series of case labels, an


optional default case and statements to execute for each
case.
2
switch Statement

• In switch, statements following the matching case label


are executed until a break statement encountered.

• The break causes an exit from the switch statement

• If break is not used anywhere in a switch statement, then


each time a match occurs in the statement, the statements
for all the remaining cases will be executed.

• If no case label matches the value of the switch statements,


the statements following the default label are executed. If
not, the entire switch statement body is skipped.
3
Switch syntax

break

break

4
Switch Flowchart

true
case 1 Display break;
“ONE”
false
true
Display
case 2
“TWO” break; end

false
true
Display
case 3 break;
“THREE”
false
Display “INVALID
INPUT”

5
Programming Exercise
• Text Book Page 85 1a, b, c
• Exercise Sumbit ilearn : Pg 89 3b
Repetition

•Repetition (Looping) structure do the same processes of a


program instruction(s) repeatedly until a false condition is met.
•C has three repetition , or looping structures that allow us to
repeat a set of statements :
– while loop
– do-while loop
– for loop

7
Categories of Loop
•Common categories of loop:-
1. counter-controlled loop
– Called as definite repetition
– Know the number of iteration to be executed
– A counter controlled loop posses 3 elements:-
1. Initialize a counter/control variable to a starting value
2. Test the counter/control variable. If the counter/control variable
exceeded the maximum value, the iteration will be terminated
3. Update the counter during each iteration.

2. sentinel –controlled loop


– Sentinel is a special value that marks the end of a list
of values
– Called as indefinite repetition
– Executes as long as the condition is met
– Do not know the number of iteration need to be executed
while Loop
•A repetition statement allows you to specify that an action is to
be repeated while some condition remains true.
•For example the pseudocode statement :
While there are more items on my shopping list
Purchase next item and cross it off my list

•describes the repetition that occurs during a shopping trip.


•The condition, “there are more items on my shopping list” may
be true or false.
•If it’s true, then the action, “Purchase next item and cross it off
my list” is performed.
•This action will be performed repeatedly while the condition
remains true.

9
Example 1: while Loop
int i = 0; //the variable is initialized to 0

while (i <= 20) //expression i<=20 is


evaluated

{
printf(“%d ”,i); //print the of i

i = i+5; //update the value of i

}
• Each repetition of looping is known as iteration.

• while loop is a pretest loop – the expression must be evaluated before


iteration.

10
Sentinel-Controlled WHILE loops
• Sentinel-controlled repetition is sometimes called indefinite repetition because
the repetition is not known
• The last entry called sentinel, is a special value.
• A sentinel value is a value that is not a legitimate data value for a particular
problem (but is of proper type) that is used as a “stopping” value.
• The while loop continues to execute as long as the program has not read the
sentinel.
• General structure :
scanf(“%d”,&variable);/*get first variable value
while (variable != -1)
{
……
Sentinel Value
scanf(“%d”,&variable);/*get next variable value

11
do…while Loop

•The do…while loop is a posttest loop

•In do…while loop the expression/condition is tested after


(posttest loop) the loop body is performed.

•In the while statement, the expression/condition is tested at the


beginning (pretest loop) of the loop before the body of the loop
is performed.
Example 2: do…while Loop
int j = 1;

do
++j;
while( j > 100 );
printf("\nj = %2d\n", j); Test expression false at
first iteration

13
for Loop
•for loop is of type counter controlled loop.

– A counter controlled loop posses 3


elements:-
1. Initialize a counter/control variable to a
starting value
2. Test the counter/control variable. If the
counter/control variable exceeded the
maximum value, the iteration will be
terminated
3. Update the counter during each iteration.
for Loop Structure

•Syntax : for (initialization; test condition;


update)
for (initialization; test condition; update) {
statement; statement;
statement;
}
for Loop Structure

•Syntax : for (initialization; test condition;


update)
for (initialization; test condition; update) {
statement; statement;
statement;
}
Programming Exercise
• Text Book Page 102 1a, b , c
• Text Book Page 110 1a, b, c
• Text Book Page 121 1a, b, c
Nested for Loop
• Nested loops consist of an outer loop with one or more inner
loops.

• Each time the outer loop is repeated, the inner loops are re-
entered, their loop control expressions are re-evaluated and
all required iteration are performed.

• When working with nested loops, the outer loop changes only
after the inner loop is completely finished(or is interrupted.).

18
Nested loop – Example 1
// This program prints numbers on lines
#include <stdio.h>
int main ()
{
int i,j;
for (i=1;i<=4;i++)
{
printf(“Line %d :",i);
for (j=3;j>= 1; j--)
{
printf("%d ",j);
}
printf("\n");
} 19
Nested loop – Example 2
Find the output for the loop below.

for (j = 1; j < =4; j++)


{

for ( a = 1; a < = 3; a++)


{
printf(“*”);
}
printf(“\n”);
}
20
Programming Exercise
• Exercise Sumbit ilearn : Pg 131 1 c, d, e
• Exercise Submit ilearn : Pg 134 3b
Other Statement Related to Looping: break

• break
• causes a loop to terminate
• terminate only the inner loop in nested loop

22
Break- Example 1
#include <stdio.h>

int main(void)
{
int x = 5;
while (x)
{
if (x == 10 )
break;
printf("%d",x);
x++;
}
printf("\nThe value of x now is %d",x);
} 23
Continue Statement – Example 1
Use continue statement in a for statement to skip the
printf statement and begin the next iteration of the loop.

24
Continue Statement – Example 2
int i;
i = 0;
while ( i < 8 )
{
i++;
continue;
printf(“This will be printed??\n");
}
THE END

You might also like