C Programming:decision Making, Control Structures and Arrays

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Module1

C Programming :decision making ,


control structures and arrays
History of C

 C is a programming language developed by Dennis Ritchie at AT & T Bell labs


in 1972.
 It is a middle level language.
 C is a procedural oriented language.
 C is machine independent programming language.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 2
C Structure

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 3
 Example code:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 4
Datatypes:

 ANSCII C provides three Classes of datatypes.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 5
1. Built in or Primary data type:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 6
Input and Output Statement

 Input Statement/read statement


 scanf() method:
 It reads the value from the console as per the type specified.
Syntax:

scanf(“format
specifier”,&variable);

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 7
 Output/write statement
 printf() method:
It prints or write the value on the console Screen.
Syntax:

printf(“statement with format


specifier”,variable);

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 8
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 9
Decision making in C

1. if statement
2. switch statement
3. conditional operator statement
4. goto statement

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 10
 IF
 if statements is a powerful decision making
statements and used to control the flow of execution
of statements.

if(test expression)
statement;

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 11
 The if statement implemented depending on the complexity of conditions to be tested.
 simple if statement
 if……else statement
 nested if….else statement
 else if ladder

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 12
 Simple if
 General form:

if(test expression)
{
statement-block;
}
Statement-x;

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 13
 If ….else
 General form:

if(test expression)
{
True block- statement;
}
else
{
False block- statement;
}
Statement-x;
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 14
 Nested if else statement:
 General form

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 15
 Else if ladder
 General form:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 16
SWITCH:
C has a multi way decision statement known
as a switch.
The keyword is switch.
The switch statement tests the value of a
given variable or expression against a list of
case values and when a match is found , a
block of statements associated with that case
is executed.
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 17
 General Form:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 18
 Flowchart:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 19
?: OPERATOR  THE
 It is two way decisions.
 This operator commonly called as conditional
operator.
 This operator combination of ? and :, and takes three
operands.
 The conditional expression evaluated first , if the
conditional expression is true then first statement is
executed .otherwise second statement is executed.
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 20
 General form:

Conditional expression? expression1: expression2

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 21
GOTO
 c supports the goto statement to branch unconditionally
from one point to another in the program.
 Goto requires a label in order to identify the place where
the branch is to be made.
 A label is any valid variable name, and must be followed
by a colon(:).
 Goto have two variations
Forwardjump.
Backward jump.
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 22
 General Form:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 23
 LOOPING

 A loop allows a program to repeat a group of statements,


either any number of times or until some loop condition
occurs
 • Convenient if the exact number of repetitions are
known.
• Loop Consists of
• Body of the loop.
• Control Statement.
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 24
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 25
 Steps in looping:
Initialization of condition variable.
Execution of body of the loop.
Test the control statement.
Updating the condition variable.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 26
Common Loops

 While Loop
 Do while Loop
 For Loop

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 27
While Loop

 The while is an entry controlled statement.


 The test condition evaluated first, then
executes the body of the statement until
condition becomes false.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 28
 General form:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 29
 Example:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 30
THE DO….WHILE STATEMENT

 The do…while is a exit controlled loop.


 Here, body of the loop evaluated first, then
the condition will be checked.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 31
 General form:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 32
 Example:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 33
FOR LOOP

 It is entry controlled loop.


 It checks the condition, then executes the
body of the loop.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 34
 General form:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 35
 Example:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 36
Nested for loop

 Nesting of loops nothing but one for


statement within another for statement.
 Inner loop executes first then the outer loop
executes.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 37
 General form:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 38
 Example

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 39
Jump in loops

 Jumping out of a loop


Break
goto

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 40
Break in loops

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 41
Goto in loops

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 42
The continue statement

 The loop to be continued with the next


iteration after skipping any statements in
between.
 The continue statement tells the compiler,
”SKIP THE FOLLOWING STATEMENT
AND CONTINUE WITH THE NEXT
ITERATION”.
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 43
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 44
Program:

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 45
ARRAYS

 Array is a fixed size sequenced collection


of elements of the same data type.
 Types of array
One dimensional array
Two dimensional array
Multi dimensional array
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 46
One dimensional array

 A list of items can be given one variable


name using only one subscript and such a
variable is called a single dimensional
array.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 47
Declaration of one dimensional array

 General form: data-type variable-name[size];


 Example:
 int a[5];
float array1[10];
char name[10];
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 48
Initialization of one dimensional array

 There are two stages:


 At Compile time
At run time

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 49
Compile time initialization

 Initializing
the variable in ordinary way.
 General form:
data_type variable_name[size]={list of
values};

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 50
 Example:
int array1[2]={4,2};
int array2[]={1,2,3};
char array3[]={‘j’,’o’,’h’,’n’,’\0’};
char name[]=“john”;
int array4[5]={20,30};
int array5[2]={1,2,3,4}//error

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 51
Example code

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 52
Run time initialization

• Usually applied for initializing large arrays.


• It uses scanf() function to access one dimension
array.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 53
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 54
Print/display onedimensional array

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 55
Two dimensional array

 A table of items can be given one variable


name using two subscript and such a
variable is called a two dimensional array.
 Used to represent tabular data.
 Having ROW and COLUMN
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 56
Declaration

 Syntax:
 Datatype arr_name[row_size][col_size];
 Example:
 int matrix[3][3];

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 57
Initialization

 There are two stages:


 At Compile time
At run time

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 58
Compile time

 data_type
variable_name[row_size][col_size]={list of
values};
 Example:
 int matrix[3][3]={{1,2,3},{4,5,6},{7,8,9}};
 int matrix[3][3]={1,2,3,4,5,6,7,8,9}
 int matrix[3][3]={0};
 int matrix[][]={1,2,3,4};
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 59
Runtime initialization

 Takes the value in the runtime using scanf()


function.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 60
Accessing/reading one dimensional array

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 61
Writing two dimensional array

 Printing the array element in the monitor.

Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-
62
2022

You might also like