One Shot
One Shot
Unit 2
ONE SHOT
Unit-2 Syllabus
Control structures: Decision statements; if and switch statement; Loop
control statements: while, for and do while loops, jump statements,
break, continue, goto statements.
Arrays: Concepts, One dimensional array, declaration and initialization
of one dimensional arrays, two dimensional arrays, initialization and
accessing, multi dimensional arrays.
Functions: User defined and built-in Functions, storage classes,
Parameter passing in functions, call by value, Passing arrays to functions:
idea of call by reference, Recursion.
Strings: Arrays of characters, variable length character strings, inputting
character strings, character library functions, string handling functions.
A Basic C Preprocessor :-
#include
Program Preprocessors are programs that process the source
code before compilation
Header File :-
<stdio.h>
It allows us to conduct input and output operations in C.
printf() and scanf()
Main Function :-
The main function serves as the starting point for program
execution.
Decession Control Statements
If statement
If – else statement
Switch statement
If statement
Example:- Syntax:-
#include <stdio.h>
if(condition)
int main()
{
{
int age= 9;
// Statements to execute if condition is true
if (age > 18) {
printf("You are an adult"); }
}
if (age < 18) {
printf("You are not an adult");
}
return 0;
}
Flowchart
of
If statement
#include <stdio.h>
void main(){
int scount = 0;
int scount = 0;
Program for a
mass bunk if (scount == 0){
printf("Mass Bunk Successful");
(More Correct }
One) else{
printf("Mass Bunk not succesful");
}
}
If- else statement
Example:- Syntax:-
if(condition)
#include <stdio.h>
int main()
{
{
int age= 9; // Statements to execute if condition
is true
if (age > 18) {
printf("You are an adult"); }
} else {
else { // Statements to execute if condition
printf("You are not an adult"); is false
} }
return 0;
}
Flowchart of
If- else
statement
Lets consider you are a teacher and you have
to give grades to students
Conditions :
if (marks>= 90){
printf("Grade A");}
The Correct else if (marks >= 80){
Switch if else if
It executes the different cases It executes the different blocks
on the basis of the value of the based on the condition
switch variable. specified.
It can only evaluate the int or It can evaluate any type of
char type expressions. expression.
Faster and easier to read for It can get messy when there
the large number of conditions. are lots of conditions.
The one line for
Ternary Operator if-else
Syntax expression 1 ? expression 2 : expression 3
return 0;
}
Write a program
to print all even
numbers between
1 to 100 using for
loop
Flowchart of
for loop
While Loop
Example: Syntax:-
#include <stdio.h> while (test expression)
int main() {
// body consisting of multiple statements
{ int i = 1;
}
while (i<= 5)
{
printf("%d\n", i);
i++;
}
return 0;
}
Write a program
to print all even
numbers between
1 to 100 using
while loop
Flowchart of
while loop
Do-while Loop
Example:
Syntax:-
#include <stdio.h> do {
int main()
// body of do-while loop
{ int i = 1;
do { } while (condition);
printf("%d\n", i);
i++;
}
return 0;
}
Flowchart of
do-while loop
While Loop V/S Do-while Loop
While Do-while
The test condition is The test condition is checked after
checked before the loop body executing the body.
is executed.
The body of the do…while loop is
When the condition is false, executed at least once even
the body is not executed not when the condition is false.
even once.
It is a type of post-tested or exit-
It is a type of pre-tested or controlled loop.
entry-controlled loop.
Semicolon is required at the end.
Semicolon is not required.
Jump Statements
Continue statement
Break statement
Goto statement
Continue Statement
The use of goto makes tracing the flow of the program very difficult.
or
Array
Declaration data_type array_name
[size1] [size2]...[sizeN];
Array Initialization
With Declaration
data_type array_name [size] = {value1, value2, ... valueN};
Using loops
for (int i = 0; i < N; i++) {
array_name[i] = valuei;
}
Accessing
Elements
of Array
array_name [index];
WAP in which you take marks
of 5 subjects from the user and
print the maximum marks.
Program for
maximum
mark
Types of Arrays
1-D Array
2-D Array
Multi-Dimensional Array
A Two-Dimensional array or 2D array
in C is an array that has exactly two
dimensions. They can be visualized in
2-D Array in C the form of rows and columns
organized in a two-dimensional
plane.
2-D Array
array_name[rows] [columns];
3-D Array
Declaration
Defination
Calling
Declaration
of Function
return_type
name_of_the_function
(parameter_1,
parameter_2);
Defining a
Function
return_type function_name
(para1_type para1_name,
para2_type para2_name)
{
// body of the function
}
Calling a
Function
Write a program to give sum
of two numbers using
function
Built-in Vs User Defined Functions
Output
Call By Reference
Output
Call By Value VS Call By Reference
scanf("%[^\n]s", str);
Function Name Description
strcmp(str1, str2)
Copies the contents of string s2 to string s1.
<String.h>
strings are the same it returns 0.
Storage: Memory.
Default value: An unpredictable value, often called a
garbage value.
Scope: Local to the block in which the variable is
defined.
Life: Till the control remains within the block in which the
variable is defined.
Register Storage Class
Storage: Memory.
Default value: Zero.
Scope: Local to the block in which the variable is
defined.
Life: Value of the variable persists between different
function calls.
How Static
presisits
between
different
Functions
External Storage Class
Storage: Memory.
Default value: Zero.
Scope: Global.
Life: As long as the program’s execution doesn’t come to
an end.
RECURSION
Recursion
=> Addition
=> Subtraction
Self Practice => Multiplication
=> Division
Solution of self
Practice 1
2. Write a program to print
Self Practice all the numbers divisible by
7 using do-while loop.
Solution of self
Practice 2
1. WAP in which you