0% found this document useful (0 votes)
19 views

C Programming

This document discusses control statements, loops, arrays and strings in C programming. It covers various control statements like if-else, switch case and ternary operator. It also discusses different types of loops like for, while, do-while and use of break, continue and goto statements. Examples are provided for control statements and loops.

Uploaded by

Kanishka Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C Programming

This document discusses control statements, loops, arrays and strings in C programming. It covers various control statements like if-else, switch case and ternary operator. It also discusses different types of loops like for, while, do-while and use of break, continue and goto statements. Examples are provided for control statements and loops.

Uploaded by

Kanishka Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

MGM University

Jawaharlal Nehru Engineering College, Aurangabad.


Department of Computer science & Engineering

Course Name:
Building Programming Logic in C (APS21ESL104)

By
Sukhwant Kour Siledar
Department of Computer Science & Engineering
JNEC, MGM University.

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
MGM University
Jawaharlal Nehru Engineering College, Aurangabad.
Department of Computer science & Engineering

Chapter 2:
Control Statements, Loops, Arrays
and Strings

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Control Statements
C Control Statements

Conditional statements Loops

•The conditional statements (also known as decision control structures) such


as if, if else, switch, etc. are used for decision-making purposes in C programs.

•Loops in programming are used to repeat a block of code until the specified
condition is met. A loop statement allows programmers to execute a statement or
group of statements multiple times without repetition of code.

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
If-Else
• If Statement: It is used to decide whether a certain statement or block of statements will be
executed or not i.e if a certain condition is true then a block of statements is executed otherwise not.
Syntax of if Statement
if(condition)
{
// Statements to execute if condition is true
}
• If-else Statement: We can use the else statement with the if statement to execute a block of code when
the condition is false. The if-else statement consists of two blocks, one for false expression and one for
true expression.
Syntax of if else in C
if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if condition is false
}
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
• A nested if in C is an if statement that is the target of another if statement.
Nested if statements mean an if statement inside another if statement.
Syntax of Nested if-else
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
If statement If-Else statement Nested If-Else statement

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
If-else-if ladder

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Conditional or Ternary Operator (?:)
• The conditional operator in C is kind of similar to the if-else statement as it follows
the same algorithm as of if-else statement but the conditional operator takes less
space and helps to write the if-else statements in the shortest way possible. It is
also known as the ternary operator in C as it operates on three operands.
Syntax of Conditional/Ternary Operator in C
variable = (condition) ? Expression2 : Expression3;

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
#include<stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is %d > %d", m, n) :
printf("n is greater than m that is %d > %d", n, m);
return 0;
}

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Switch Statement
• The switch case statement is used for executing one condition from
multiple conditions.
• The switch statement consists of conditional-based cases and a default
case.
• Rules:
1. In a switch statement, the “case value” must be of “char” and “int” type.
2. There can be one or N number of cases.
3. The values in the case must be unique.
4. Each statement of the case can have a break statement. It is optional.
5. The default Statement is also optional.

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Syntax of switch Statement
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
.
.
.
case value_n: statement_n;
break;

default: default_statement;
}

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Examples:
• Day of week
• Calculator
• Menu Driven Program

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
#include <stdio.h>
Example (Calculator)
#include <stdlib.h>
int main()
{
// switch variable
char choice;
// operands
int x, y;
while (1) {
printf("Enter the Operator (+,-,*,/)\nEnter x to "
"exit\n");
scanf(" %c", &choice);
if (choice == 'x') {
exit(0);
}
printf("Enter the two numbers: ");
scanf("%d %d", &x, &y);

// switch case with operation for each operator


switch (choice) {
case '+':
printf("%d + %d = %d\n", x, y, x + y);
break;
case '-':
printf("%d - %d = %d\n", x, y, x - y);
break;
case '*':
printf("%d * %d = %d\n", x, y, x * y);
break;
case '/': Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
How switch Statement Work?
Step 1: The switch variable is evaluated.
Step 2: The evaluated value is matched against all the present cases.
Step 3A: If the matching case value is found, the associated code is executed.
Step 3B: If the matching code is not found, then the default case is executed
if present.
Step 4A: If the break keyword is present in the case, then program control
breaks out of the switch statement.
Step 4B: If the break keyword is not present, then all the cases after the
matching case are executed.
Step 5: Statements after the switch statement are executed.

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Loops

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
For Loop

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
For Loop

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
While Loop

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
do-while

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
break Statement

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Example of break

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Continue Statement

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Example of Continue

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
goto statement

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Example of
goto
statement

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Exit Function
• In C, exit() terminates the calling process without executing the rest code
which is after the exit() function call. It is defined in the <stdlib.h> header file.

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
What is an Array?

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Example of an Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Declaration of one dimensional
Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Example of One-dimensional Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Initialization of one dimensional Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Compile time Initialization

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Compile time Initialization

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Run time initialization

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Two dimensional Arrays

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Declaration of Two dimensional
Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Initialization of Two dimensional
Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Initialization of Two dimensional
Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Multi-Dimensional Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Example Multi-dimensional Arrays

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Operations on Array

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Advantages of Array
Below are some advantages of the array:

● In an array, accessing an element is very easy by using the index number.


● The search process can be applied to an array easily.
● 2D Array is used to represent matrices.
● For any reason a user wishes to store multiple values of similar type then the Array can be used and
utilized efficiently.
● Arrays have low overhead.
● C provides a set of built-in functions for manipulating arrays, such as sorting and searching.
● C supports arrays of multiple dimensions, which can be useful for representing complex data structures
like matrices.
● Arrays can be easily converted to pointers, which allows for passing arrays to functions as arguments or
returning arrays from functions.

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Disadvantages of Array

Array size is fixed: The array is static, which means its size is always fixed. The memory which is allocated to

it cannot be increased or decreased.

Array is homogeneous:The array is homogeneous, i.e., only one type of value can be store in the array.

Array is Contiguous blocks of memory: The array stores data in contiguous(one by one) memory location.

Insertion and deletion are not easy in Array: The operation insertion and deletion over an array are

problematic as to insert or delete anywhere in the array, it is necessary to traverse the array and then shift

the remaining elements as per the operation.

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
String
A String in C programming is a sequence of characters terminated with a null character ‘\0’.
The C String is stored as an array of characters.
The difference between a character array and a C string is that the string in C is terminated with a unique
character ‘\0’.

If we want to store a string of size n then we should always declare a string with a size equal to or greater
than n+1.

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
String

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
String Initialization

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
// C program to read string from user
#include<stdio.h>

int main()
{
// declaring string
char str[50];

// reading string
scanf("%s",str);
// gets(str);

// print string
printf("%s",str);

return 0;
}

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Read and Print String
#include<stdio.h>
void main()
{
char Name[10];
int i;
printf("Enter Your Name:");
for (i = 0; i < 10; ++i) {
scanf("%c", &Name[i]);
}
printf("\nYour name is:");
for (i = 0; i < 10; ++i) {
printf("%c", Name[i]);
}
}

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
// C program to print string using Pointers
#include <stdio.h>

int main()
{

char str[20] = "MGMJNEC";


char* ptr = str;

while (*ptr != '\0'){


printf("%c", *ptr);

ptr++;
}
return 0;
}

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
String Functions

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Strlen()

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Strcat()

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Strcmp()

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Strcpy()

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Strlen()

Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad
THANK
YOU
Department of Computer Science and Engineering, Jawaharlal Nehru Engineering College, Aurangabad

You might also like