Understanding Procedural Programming: Functions, Conditional Statements, and Variables, Summaries of Programming Languages

An introduction to procedural programming, explaining the concepts of functions, conditional statements, and variables in this programming paradigm. It covers the key features of procedural programming, including predefined functions, parameter passing, and the use of printf() and scanf() in C. The document also delves into the syntax and usage of simple if statements, nested if statements, and else if ladders. Additionally, it discusses switch statements and looping structures such as while loops and for loops.

Typology: Summaries

2018/2019

Uploaded on 08/06/2021

apshu-thakuri
apshu-thakuri 🇳🇵

3 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name- Amarawati Banshi
Roll no.- 7
1. Define Procedural Language.
A procedural language is a type of computer programming language that
specifies a series of well-structured steps and procedures within its programming
context to compose a program. Examples of procedural languages are BASIC, C,
Java FORTRAN and Pascal.
Procedural languages are some of the common types of programming languages
used by script and software programmers. They make use of functions, conditional
statements and variables to create programs that allow a computer to calculate and
display a desired output. Procedures or functions are implemented on the data and
variables to perform a task. These procedures can be called/invoked anywhere
between the program hierarchy, and by other procedures as well. It is also known
as imperative language.
Advantage of Procedural Language are:
It is relative simplicity and ease of implementation of compilers and
interpreters
The ability to re-use the same code at different places in the program
without copying it.
An easier way to keep track of program flow.
The ability to be strongly modular or structured.
Needs only less memory.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download Understanding Procedural Programming: Functions, Conditional Statements, and Variables and more Summaries Programming Languages in PDF only on Docsity!

Name- Amarawati Banshi

Roll no.- 7

1. Define Procedural Language.

A procedural language is a type of computer programming language that specifies a series of well-structured steps and procedures within its programming context to compose a program. Examples of procedural languages are BASIC, C, Java FORTRAN and Pascal. Procedural languages are some of the common types of programming languages used by script and software programmers. They make use of functions, conditional statements and variables to create programs that allow a computer to calculate and display a desired output. Procedures or functions are implemented on the data and variables to perform a task. These procedures can be called/invoked anywhere between the program hierarchy, and by other procedures as well. It is also known as imperative language. Advantage of Procedural Language are: ✓ It is relative simplicity and ease of implementation of compilers and interpreters ✓ The ability to re-use the same code at different places in the program without copying it. ✓ An easier way to keep track of program flow. ✓ The ability to be strongly modular or structured. ✓ Needs only less memory.

2. Characteristics and features of Procedural

Programming Language.

Characteristics of Procedural oriented programming is:

  1. It focuses on process rather than data.
  2. It takes a problem as a sequence of things to be done such as reading, calculating and printing. Hence, a number of functions are written to solve a problem.
  3. A program is divided into a number of functions and each function has clearly defined purpose.
  4. Most of the functions share global data.
  5. Data moves openly around the system from function to function. Key Features of Procedural Programming The key features of procedural programming are given below: Predefined functions: A predefined function is typically an instruction identified by a name. Usually, they are built into higher-level programming languages, but they are derived from the library or the registry, rather than the program. Local Variable: A local variable is a variable that is declared in the main structure of a method and is limited to the local scope it is given. It can only be used in the method it is defined in, and if it were to be used outside the defined method, the code will cease to work. Global Variable: A global variable is a variable which is declared outside every other function defined in the code. Due to this, global variables can be used in all functions, unlike a local variable. Modularity: Modularity is when two dissimilar systems have two different tasks at hand but are grouped together to conclude a larger task first. Every group of systems then would have its own tasks finished one after the other until all tasks are complete.

Use of printf() and scanf() in PPL:

  1. printf() is used to display the output and scanf() is used to read the inputs.
  2. printf() and scanf() functions are declared in “stdio.h” header file in C library.
  3. All syntax in C language including printf() and scanf() functions are case sensitive. Note: C language is case sensitive. For example- printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.

5. Basic I/O.

Program to divide two number :

6. Control Statement

If statement Simple if if else nested if else else if ladder (if else if else)

a) Simple if

if (test-expression) { Statement 1; }

Flowchart:

Entry True T.E Statement- 1

Flowchart of if else: Entry True False

Program of if else:

#include<stdio.h> int main () { int n; printf("Enter a number: "); scanf("%d",&n); if(n>0) { printf("It is positive number"); } else { printf("It is negative number"); } return 0; } T.E Statement- (^1) Statement- 2 End

Output: Enter a number: 9 Enter a number: - 20 It is positive number It is negative number

c) nested if else

Syntax:

if (test-expression 1) { if (test-expression 2) { Statement block-1; } else { Statement block-2; } else { if (test-expression 3) { Statement block-3; } else { Statement block-4; } }

printf("The greater number is %d",n1); } else { printf("The greater number is %d",n3); } } else { if(n2>n3) { printf("The greater number is %d",n2); } else { printf("The greater number is %d",n3); } } return 0; } Output: Enter a b and c: 67 82 25 The greatest value is 82.

d) else if ladder (if else if……else)

Syntax:

if (condition-1) statement-1; else if (condition-2) statement-2; else if (condition-3) statement-3; else if (condition-4)

statement-4; : : : else if (condition-n) statement-n; else default statement;

Flowchart of else if ladder:

Entry True False True False True False True False cond

  • 1 St- 1 End St- 2 St- 3 St-n Default St. cond
  • 2 cond
  • 3 cond
  • n

Flowchart of switch statement:

Entry default exp const 3 exp const 2 exp const 1 exit

program of switch statement:

#include<stdio.h> int main () { char a; printf("Enter any number: "); scanf("%c", &a); switch(a) { case '1': printf("Sunday"); break; Switch exp block- 1 block- 1 block- 1 default block

case '2': printf("Monday"); break; case '3': printf("Tuesday"); break; case '4': printf("Wednesday"); break; case '5': printf("Thursday"); break; case'6': printf("Friday"); break; case '7': printf("Saturday"); break; default: printf("Please enter number 1 to 7"); break; } return 0; } Output: Enter any number: 6 Enter any number: Ans. Friday Ans. Please enter number 1 to 7

a++;

return 0;

Output:

Hello Hello Hello

b) do while loop

syntax:

do

body of loop;

while (test-expression);

Flowchart of do while loop:

Entry False True Exit body of loop T.E

Program of do while loop:

#include<stdio.h> int main () { int a=5; do { printf(“Hello”); a++; } while (a<8); return 0; } Output: Hello Hello Hello

c)for loop

syntax:

for (initialization expression; test expression; update expression) { body of loop; }

Output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

8. Array

Array is the group of relative data item that share a common name. Arrays are derived data types. The use of array allows for the development of smaller and most clear readable program. Types of Array:

  1. One dimensional array
  2. Two- dimensional array

1. One Dimensional Array

Elements of the array can be represented either as a single column or as a single row. Initialization of an array: Datatype name [size] = {List of arguments} Program for One Dimensional array: #include<stdio.h> int main () { int mark [10],i; for (i=0; i<5; i++) { printf("Enter mark"); scanf("%d", &mark[i]); }

for (i=0; i<5; i++) { printf("%d\n", mark[i]); } return 0; } Output: Enter a mark: 5 Enter a mark: 9 Enter a mark: 7 Enter a mark: 3 Enter a mark: 12 Ans. 5 9 7 3 12

b. Two-Dimensional Array

It is the collection of the data item all of the same types structured in two-dimension (refer as rows and columns) Declaration of Two-Dimension array: float marks [20][30] int marks [3][4] = [{1,2,3,4}, {2,4,6,8}, {1,3,5,7}] Program for Two-dimensional array: #include<stdio.h> int main () { int i, j, marks[10][10]; for (i=0; i<3; i++) { for (j=0; j<2; j++) {