1 ICS 2175 Lecture 3 Programming in C To Use2
1 ICS 2175 Lecture 3 Programming in C To Use2
Introduction to C programming
Language
Introduction to C
1. Easy to read
2. Easy to modify
3. Consistent in format
4. Self documenting
Example:
A C program to output a greeting
#include<stdio.h>/*enables us to use
functions defined elsewhere */
void main()
{ printf(“hello world”); //printf is an
output function
}//end
The data item can then be accessed later in the program simply
by referring to the variable name.
Copyrighting
Character Data types
== Equal to
!= Not equal to
|| Logical or
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.
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
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
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.