3 Programming
3 Programming
== Equal to
!= Not equal to
|| Logical or
2
Control Structures
These are statements used to control the sequence in which
instructions of a program are executed.
A. Selection Statements
A realistic C program may require that a logical test be carried
out at some point within the program.
One of several actions will then be carried out depending on the
outcome of the test.
This is known as branching.
This may be done using several control structure included in C.
1. The if–else statement
Used to carryout a logical test then take one of two possible
actions. The else part of the statement is optional. Thus, in its
most general form it is written: if (expression) statement.
3
1. The if–else statement
Example: The following example asks the user to enter the
total marks of the student. The program then prints proceed if
the marks are greater than 40.
#include <stdio.h>
void main()
{
float marks;
printf(“Enter the student’s marks::”);
scanf(“%f”,&marks);
if (marks>40)
{
printf(“Proceed”);
}//end if
} //end main
4
The other form of the if–else statement includes the else
statement. Its form is: if (expression) statement1 else
statement 2.
The Example above can be written to output the statement
rewind if the marks is less than 40.
#include <stdio.h>
void main()
{
float marks;
printf(“Enter the student’s marks::”);
scanf(“%f”,&marks);
if (marks>=40)
{
printf(“Proceed”);
}
else
{
printf(“Rewind”);
} 5
} //end main
The if–else statement can also take the following form:
if(expression1)
statement1
else if(expression2)
statement2
else if(expression3)
statement3
.
.
.
else if(expression n)
statement n
else
statement n+1
Example: We can have a program that allocates the
grade depending on the students score in an exam: 70 and
above is A, 60-69 is B, 50-59 is, 40-49 is D and below 40 is
6
F (for fail)
#include<stdio.h>
void main()
{
float marks=0;
printf(“Enter the student’s marks::”);
scanf(“%f”,&marks);
if (marks>=70)
{
printf(“The grade is an A”);
}
else if (marks>=60 && marks <=69)
{
printf(“The grade is a B”);
}
else if (marks>=50 && marks <=59)
{
printf(“The grade is a C”);
}
else if (marks>=40 && marks <=49)
{
printf(“The grade is a D”);
}
else
{
printf(“The grade is an F”);
}
}//end main 7
2. The Switch statement
The switch statement is another selection statement provided
in C.
It causes a group of statements to be chosen from one of
several groups. The selection is based on one of several values
of an expression
It has the following form:
switch (expression) statement.
NB: expression should evaluate into an integer value.
A further elaboration of the switch statement is shown next.
8
The Switch statement
switch(expression)
{
case expression 1:
statement(s)
break;
case expression 2:
statement(s);
break;
.
.
.
case expression n:
statement(s)
break;
default:
statement(s);
break;
}//end switch 9
Example: The following program prints the name of an input number if
it’s a digit. If it’s not it informs the user that the number is not a digit.
10
#include <stdio.h>
void main()
{
int number ;
printf(“Enter a digit::”);
scanf(“%d”,&number);
switch(number)
{
case 0:
printf(“The digit is called ZERO”);
break;
case 1:
printf(“The digit is called ONE”);
break;
case 2:
printf(“The digit is called TWO”);
break;
case 3:
printf(“The digit is called THREE”);
break;
case 4:
printf(“The digit is called FOUR”);
break; 11
case 5:
printf(“The digit is called FIVE”);
break;
case 6:
printf(“The digit is called SIX”);
break;
case 7:
printf(“The digit is called SEVEN”);
break;
case 8:
printf(“The digit is called EIGHT”);
break;
case 9:
printf(“The digit is called NINE”);
break;
default:
printf(“The number you have entered is not a
digit”);
}//end switch
}//end main
12
B. Looping / Repetition
In writing a computer program, it is often necessary to
repeat a part of a program a number of times.
One of the ways of achieving this would be to write out that
part of the program as many times as it was needed.
This, however, is a very impractical method since it would
produce a very lengthy computer program and the number
of times that part of the program should be repeated might
not known in advance.
In this section, we introduce 3 methods for running a part of
a program repeatedly also known as looping.
13
1. The While Statement
The while statement is used to carryout looping operations
in which a group of statements is executed repeatedly until
some condition as been satisfied.
The general form of the while statement is:
While (logical expression) statement
The statement will be executed repeatedly while the logical
expression is true.
14
Example: The following program prints the digits 0 – 9 using a
while loop.
#include <stdio.h>
void main()
{
int digit =0;
while (digit<=9)
{
printf(“%d ”,digit);
digit=digit+1;
}
}
Exercise: Write a program using a loop that gets the sum of the
first 20 integers i.e. the sum if 1 – 20.
15
2. The Do while loop
This looping statement is similar to the while statement
except that the statement is executed at least once.
If you look closely at the while statement above you’ll
see that there is a possibility that the statement will not be
executed if the expression is false the first time the
program tries to enter the loop.
The general form of the do while statement is:
do statement while(expression)
Using the do while statement, the above program can be
written as shown below:
16
#include <stdio.h>
void main()
{
int digit =0;
do
{
printf(“%d ”,digit);
digit=digit+1;
} while (digit<=9);
}//end main
17
The Do while loop
We can write a program to read all the input from the
keyboard using the getchar() function until we encounter
an end of line function.
We store each of the characters we read in an array.
After we get to the end of line we will output the string we
have read using printf:
NB: We have to put a null character as the last character of
the string
18
#include <stdio.h>
void main()
{
char c ; /* we will store the characters we
read here.*/
char my_string[100]; /* we will store the
characters we read in this array*/
int counter ;/*This will help us to keep
track of the point in the array where we
are */
printf("Enter your string of characters and
terminate by pressing enter ");
counter=0;
19
do
{
c=getchar();
my_string[counter]=c;
counter=counter+1;
}while (c!='\n'); /* we continue reading
while the character
we read is not an end of line character*/
my_string[counter]='\0'; /* put a string
terminating character at the end of the
string (null character)*/
printf("%s",my_string);
}//end main
20
3. The for statement
This statement includes an expression that specifies an initial
value for an index, another statement that specifies whether
or not the loop is continued and another statement that allows
the index to be modified at the end of each pass.
The general form of the for statement is shown below:
for (expression 1; expression 2; expression 3) statement
Expression 1 is used to initialize some variable;
Expression 2 is used to represents a logical expression related
to the parameter in expression one. Expression 2 must be
true for the loop to continue.
Expression 3 makes a change to the variable in expression 1.
This change is what will eventually make expression 2 to be
false to end the loop.
We can rewrite the program to print digits 0–9 using a for
loop. 21
#include <stdio.h>
void main()
{
for(int index=0;index<=9; index++)
{
printf(“%d ”,index);
} //end for
}//end main
Notice that the statement index++. This is equivalent/short form for
index=index+1.
Exercise: write the same program using a for loop but now the digits
should be written from 9 to 0. Thus the index should be decreasing i.e.
should be initialized to 9.
22