[go: up one dir, main page]

0% found this document useful (0 votes)
34 views121 pages

PPS Unit-2

The document outlines the programming concepts covered in a course on 'Programming for Problem Solving', focusing on conditional control statements, looping control statements, and arrays in C programming. It includes examples of simple if statements, nested if-else statements, and pointer manipulation with arrays. Additionally, it discusses the use of pointers for character strings and the advantages of using pointers for handling arrays with varying lengths.

Uploaded by

Femi Prince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views121 pages

PPS Unit-2

The document outlines the programming concepts covered in a course on 'Programming for Problem Solving', focusing on conditional control statements, looping control statements, and arrays in C programming. It includes examples of simple if statements, nested if-else statements, and pointer manipulation with arrays. Additionally, it discusses the use of pointers for character strings and the advantages of using pointers for handling arrays with varying lengths.

Uploaded by

Femi Prince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 121

21CSS101J

PROGRAMMING FOR PROBLEM


SOLVING

Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-2
Conditional Control -Statements :Simple if, if...else -
Conditional Statements : else if and nested if -
Conditional Statements : Switch case - Un-conditional
Control Statements : break, continue, goto - Looping
Control Statements: for, while, do.while - Looping
Control Statements: nested for, nested while -
Introduction to Arrays -One Dimensional (1D) Array
Declaration and initialization - Accessing, Indexing and
operations with 1D Arrays - Array Programs – 1D -
Initializing and Accessing 2D Array, Array Programs – 2D
- Pointer and address-of operators -Pointer Declaration
and dereferencing, Void Pointers, Null pointers Pointer
based Array manipulation
Conditional Control -Statements
Types of Conditional Statements in C
/* Simple if – Program to check whether a number is Odd*/
#include<stdio.h>
int main( )
{
int number;
printf(“Enter the Number: ”);
scanf(“%d, &number);
if(number%2==0)
{
printf(“The Number is Even”);
}
return 0;
}

Output
Enter a value :
10342
The number is Even
Flowchart of if-else Statement
/* if else –To check whether a number is Odd or Even*/

#include<stdio.h>

int main( )
{
int number;
printf(“Enter the Number: ”);
scanf(“%d, &number);
if(number%2==0)
{
printf(“The Number is Even”);
}
else
{
printf(“The Number is Odd”);
}
return 0;
Output 1
Enter the Number : 10341
The number is Odd

Output 2
Enter the Number : 10342
The number is Even
Flowchart of Nested if-else
iNested if else statement
❑ Used when a series of decisions
are involved
❑ Makes a choice between
several alternatives
❑ New if else statement block is
used within existing if else
statement block
/*Program for Nested if else
#include <stdio.h>
void main( ) */
{
char username;
int password;

printf("Username:");

scanf("%c",&username);

printf("Password:");

scanf("%d",&password)

;
if(username=='a')
{
if(password==12345)
{
printf("Login successful");
}
else
{
printf("Password is incorrect, Try
again.");
}
}
else
{
printf("Username is incorrect, Try again.");
}
return 0;
}
Output 1 Username: a
Password:
12345 Login Successful

Output 2 Username: a
Password:
54321

Password is incorrect, Try again.

Output 3 Username: b
Password:
21CSS101J

PROGRAMMING FOR PROBLEM


SOLVING

Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-2
Conditional Control -Statements :Simple if, if...else -
Conditional Statements : else if and nested if -
Conditional Statements : Switch case - Un-conditional
Control Statements : break, continue, goto - Looping
Control Statements: for, while, do.while - Looping
Control Statements: nested for, nested while -
Introduction to Arrays -One Dimensional (1D) Array
Declaration and initialization - Accessing, Indexing and
operations with 1D Arrays - Array Programs – 1D -
Initializing and Accessing 2D Array, Array Programs – 2D
- Pointer and address-of operators -Pointer Declaration
and dereferencing, Void Pointers, Null pointers Pointer
based Array manipulation
Conditional Control -Statements
Types of Conditional Statements in C
4. if-else-if
Un-conditional Control Statements :
break, continue, goto
Looping Control Statements: for, while,
do.while
21CSS101J

PROGRAMMING FOR PROBLEM


SOLVING

Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-2
Conditional Control -Statements :Simple if, if...else -
Conditional Statements : else if and nested if -
Conditional Statements : Switch case - Un-conditional
Control Statements : break, continue, goto - Looping
Control Statements: for, while, do.while - Looping
Control Statements: nested for, nested while -
Introduction to Arrays -One Dimensional (1D) Array
Declaration and initialization - Accessing, Indexing and
operations with 1D Arrays - Array Programs – 1D -
Initializing and Accessing 2D Array, Array Programs – 2D
- Pointer and address-of operators -Pointer Declaration
and dereferencing, Void Pointers, Null pointers, Pointer
based Array manipulation
Looping Control Statements: nested
for, nested while
Introduction to Arrays
Pointer
void Pointer
NULL Pointer
21CSS101J

PROGRAMMING FOR PROBLEM


SOLVING

Dr. R. Femi
Assistant Professor
Department of Electrical and Electronics Engineering
SRM Institute of Science and Technology,
Kattankulathur
Unit-2
Conditional Control -Statements :Simple if, if...else -
Conditional Statements : else if and nested if -
Conditional Statements : Switch case - Un-conditional
Control Statements : break, continue, goto - Looping
Control Statements: for, while, do.while - Looping
Control Statements: nested for, nested while -
Introduction to Arrays -One Dimensional (1D) Array
Declaration and initialization - Accessing, Indexing and
operations with 1D Arrays - Array Programs – 1D -
Initializing and Accessing 2D Array, Array Programs – 2D
- Pointer and address-of operators -Pointer Declaration
and dereferencing, Void Pointers, Null pointers, Pointer
based Array manipulation
Pointer based Array Manipulation

Pointer and Arrays in C

Replacing the printf(“%d”, *p); statement of the above example, with


below mentioned statements. Lets see what will be the result.
printf(‘%d”, a[i]); prints the array, by incrementing index
printf(‘%d”, i[a]); this will also print elements of array printf(‘%d”,
a+i); this will prints address of all the array elements. printf(‘%d”,
*(a+i)); will print value of array element

printf(‘%d”, *a); will print value of a[0] only


a++ compile time error, we cannot change the base address of the
array.
Pointer based Array Manipulation

Pointer and Arrays in C

Syntax:
*(a+i) //pointer with an array
is same as:
a[i]

Pointer to Multidimensional Array


Let's see how to make a pointer point to a multidimensional
array. In a[i][j], a will give the base address of this array, even a
+ 0 + 0 will also give the base address, that is the address of
a[0][0] element.
Syntax:
*(*(a + i) + j)
Pointer based Array Manipulation

Pointer and Character strings


Pointer is used to create strings. Pointer variables of char type are
treated as string.

char *str = "Hello";

The above code creates a string and stores its address in the
pointer variable str. The pointer str now points to the first
character of the string "Hello".

The string created using char pointer can be assigned a value at


runtime.
Pointer based Array Manipulation

Pointer and Character strings

char *str; str =


"hello";
The content of the string can be printed using printf() and puts()
printf("%s", str);
puts(str);

str is a pointer to the string and also name of the string.


Therefore we do not need to use indirection operator *.
Pointer based Array Manipulation

Array of Pointers

Pointers are very helpful in handling character arrays with rows of


varying lengths.
char *name[3] = {
"Adam",
"chris",
"Deniel“
};
//without pointer
char name[3][20] = {"Adam", "chris", "Deniel"};
Pointer based Array Manipulation

Array of Pointers

In the second approach memory wastage is more, hence it


is preferred to use pointer in such cases.

You might also like