2. Control flow statement is a statement whose execution
results in a choice being made as to which of two or more
paths should be followed.
Control flow statements can be categorized by their effect:
a. continuation at a different statement (unconditional
branch or jump)
b. executing a set of statements only if some condition is
met (choice - i.e. conditional branch)
c. executing a set of statements zero or more times, until
some condition is met (i.e. loop - the same as conditional
branch)
d. executing a set of distant statements, after which the flow
of control usually returns (subroutines and continuations),
e. stopping the program, preventing any further execution
(unconditional halt).
9. Start
Chose
Month
switch
(month)
Finish
Januar
y
Februar
y
Mac
Decemb
er
………...
10. #include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int month;
printf("Enter a month :");
scanf("%d",&month);
switch(month)
{
case 1: printf("January");
break;
case 2: printf("February");
break;
case 3: printf("Mac");
break;
case 4: printf("April");
break;
case 5: printf("May");
break;
case 6: printf("Jun");
break;
case 7: printf("July");
break;
case 8: printf("Ogos");
break;
case 9: printf("September");
break;
case 10:printf("October");
break;
case 11:printf("Novenber");
break;
case 12:printf("December");
break;
default:printf("error!");
break;
}
return 0;
}
11. #include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int grade;
printf("Enter a month :");
scanf("%d",&grade);
switch(grade)
{
case 10:
case 9: printf("Grade: A ");
break;
case 8: printf("Grade: B ");
break;
case 7: printf("Grade: C ");
break;
case 6: printf("Grade: D ");
break;
case 5:
case 4:
case 3:
case 2:
case 1: printf("Grade: F ");
break;
default: printf("error!");
break;
}
return 0;
}
12. Score
>=90
Grede
= A
Finish
Score
>=80
Score
>=70
Score
>=60
Grede
= B
Grede
= C
Grede
Grede = D
= F
14. Is a pretest loop
Use an expression to control the loop
It test the expression before ever iteration
of the loop
15. #include<stdio.h>
main()
{
int pembilang;
float purata,jumlah,score;
pembilang=1;
jumlah=0;
while(pembilang<=5)
{
printf("nPlease enter your test score %d:",pembilang);
scanf("%f",&score);
jumlah=score+jumlah;
pembilang++;
printf("njumlah is :%.2f",jumlah);
}
purata=(jumlah/5);
printf("nnAverage test scores are is %.2f",purata);
return 0;
}
16. Is a pretest loop that uses three
expressions
The first expression contains any
initialization statements, the second
contains the terminating expression and
the third contains the updating
expression
17. #include<stdio.h>
main()
{
int counter;
float average,sum,score;
sum=0;
for(counter=1; counter<=5; counter++)
{
printf("nPlease enter your test score %d:", counter);
scanf("%f",&score);
sum=score+sum;
printf("nThe sum is :%.2f",sum);
}
average=(sum/5);
printf("nnAverage test scores are is %.2f",average);
return 0;
}
18. Is a post-test loop
Use an expression to control the loop
It test the expression after the execution
of the body
19. #include<stdio.h>
main()
{
int pembilang;
float purata,jumlah,score;
pembilang=1;
jumlah=0;
do
{
printf("nPlease enter your test score %d:",pembilang);
scanf("%f",&score);
jumlah=score+jumlah;
printf("njumlah is :%.2f",jumlah);
pembilang++;
}
while(pembilang<=5);
purata=(jumlah/5);
printf("nnAverage test scores are is %.2f",purata);
return 0;
}
20. These loops are the loops which contain
another looping statement in a single loop.
These types of loops are used to create
matrix.
Any loop can contain a number of loop
statements in itself.
If we are using loop within loop that is
called nested loop.
In this the outer loop is used
for counting rows and the internal loop is
used for counting column
22. The C language provides a capability that enables the user
to define a set of ordered data items known as an array.
Suppose we had a set of grades that we wished to read
into the computer and suppose we wished to perform some
operations on these grades, we will quickly realize that we
cannot perform such an operation until each and every
grade has been entered since it would be quite a tedious
task to declare each and every student grade as a variable
especially since there may be a very large number.
In C we can define variable called grades, which
represents not a single value of grade but a entire set of
grades. Each element of the set can then be referenced by
means of a number called as index number or subscript.
23. Like any other variable arrays must be
declared before they are used. The general
form of declaration is:
type variable-name[size];
The type specifies the type of the elements
that will be contained in the array, such as
int float or char and the size indicates the
maximum number of elements that can be
stored inside the array for ex:
float height[50];
int week[7];
24. The declaration int values[10]; would reserve
enough space for an array called values that
could hold up to 10 integers. Refer to the below
given picture to conceptualize the reserved
storage space.
values[0]
values[1]
values[2]
values[3]
values[4]
values[5]
values[6]
values[7]
values[8]
values[9]
The array values stored in the memory.
25. We can initialize the elements in the array in the same way as the ordinary variables when
they are declared. The general form of initialization off arrays is:
type array_name[size]={list of values};
The values in the list care separated by commas, for example the statement
int number[3]={0,0,0};
Will declare the array size as a array of size 3 and will assign zero to each element if the
number of values in the list is less than the number of elements, then only that many
elements are initialized. The remaining elements will be set to zero automatically.
In the declaration of an array the size may be omitted, in such cases the compiler
allocates enough space for all initialized elements. For example the statement
int counter[]={1,1,1,1};
Will declare the array to contain four elements with initial values 1. this approach works
fine as long as we initialize every element in the array.
The initialization of arrays in c suffers two draw backs
1. There is no convenient way to initialize only selected elements.
2. There is no shortcut method to initialize large number of elements.
30. Processing Multi-dimensional Arrays
The key to processing all cells of a multi-dimensional array is nested
loops.
12.5.30
[0] [1] [2] [3]
[0]
[1]
[2]
10 11 12 13
14 15 16 17
18 19 20 21
22 23 24 25
26 27 28 29
[3]
[4]
for (int row=0; row!=5; row++) {
for (int col=0; col!=4; col++) {
System.out.println( myArray[row][col] );
}
}
for (int col=0; col!=4; col++) {
for (int row=0; row!=5; row++) {
System.out.println( myArray[row][col] );
}
}
Exercises
1) Write a method to initialize this array with the given values.
2) Write a method to interchange two columns, indexed by two int
parameters.
32. #include<stdio.h>
main( )
{
int bil,data[10]={10,20,-5,36,-95,14,-82,0,1,25};
printf("Please select the value to be viewed [0-9]:");
scanf("%d",&bil);
printf("nValue in data %d is %d",bil,data[bil]);
return 0;
}
output
Please select the value to be viewed [0-9]: 8
Value in data 8 is 1
33. #include<stdio.h>
main( )
{
int data[50],saiz,kira_neg=0,kira_pos=0,count,bil;
printf("Enter the size of the arrayn :");
scanf("%d",&saiz);
for (count=0;count<saiz;count++)
{
for(count=0;count<saiz;count++)
{
bil=count+1;
printf("nEnter the elements of the arrayn %d:",bil);
scanf("%d",&data[count]);
if(data[count]<0)
kira_neg++;
else
kira_pos++;
}
}
printf("nThere are %d positive numbers and %d negative numbers in the arrays",kira_pos,kira_neg);
return 0;
}