Professional Documents
Culture Documents
C Programming:decision Making, Control Structures and Arrays
C Programming:decision Making, Control Structures and Arrays
C Programming:decision Making, Control Structures and Arrays
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:
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
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:
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:
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
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
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
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
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
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
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
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 45
ARRAYS
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 47
Declaration of one dimensional array
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
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
Syntax:
Datatype arr_name[row_size][col_size];
Example:
int matrix[3][3];
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-2022 57
Initialization
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
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
Mrs.RakshithaP_AssistantProfessor_Dept_MCA 07-05-
62
2022