0% found this document useful (0 votes)
16 views54 pages

C Programming - Module III

The document outlines the course ES 202, which introduces computers and programming in C, taught by Dr. Kanta Prasad Sharma. It covers various programming concepts including conditional statements, loops, storage types, and jump statements, along with examples and syntax. Additionally, it provides references for further reading and assessment materials for students.

Uploaded by

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

C Programming - Module III

The document outlines the course ES 202, which introduces computers and programming in C, taught by Dr. Kanta Prasad Sharma. It covers various programming concepts including conditional statements, loops, storage types, and jump statements, along with examples and syntax. Additionally, it provides references for further reading and assessment materials for students.

Uploaded by

jerry482891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

ES 202 - Introduction to Computers and Programming in C

Dr. Kanta Prasad Sharma ( K P Sharma)


Associate Professor –CSE
Amity School of Engineering & Technology
Greater Noida Campus
+91 9760 207629
Faculty Profile
Greater Noida Campus Academic Highlights
 Ph.D. – Information Technology
 Industrial Training- UI Path Automation
 Industrial Training – Global Logic – Full Stack
 Books Published : 5
Experience
14 Years as academics &Innovative Research
Research Highlights

 SCI :18 [ WOS -H Index :08] & WCIF – 01.02

 Scopus : 42 [Scopus- H Index : 12]

 Citations : 454 [ H- 11 & I- 13 ]


Dr. Kanta Prasad Sharma
Associate Professor (CSE)  Research Gate : 9.9 [ H- 8]
Amity School of Engineering & Technology
Official : Faculty Cabin - 413 ( IV Floor,)
Amity University – G –Noida Campus  Patents :15 [ Published :18 ,Grant:10]
+91 9760 20 7629 Members
MODULE - III
Greater Noida Campus

Outlines

• Introduction To C Standards - Conditional Statements


( IF , ELSE IF , SWITCH )
• Break & Continue Statements
• Loops – For , While , Do while, Nested
• Storage Types ( Automatic , Registers etc )
• Pre define Processor
• Memory Storage Unit
Conditional Statements
Greater Noida Campus

The conditional statements (also known as decision control structures).

These used for decision-making purposes in C programs.

Decision-Making Statements and are used to evaluate one or more conditions and make the decision

These , in programming languages [ decide ] the direction of the flow of program execution.

Nested f-else-if
If if-else if Ladder switch Jump
Conditional Statements
Greater Noida Campus
If Statements
Greater Noida Campus

• Simple decision-making statement.


• It is used to decide
• whether a certain statement or block of
statements will be executed or not.
• If a certain condition is true then a block of

statements is executed otherwise not.


Syntax
If (condition )
{
// statement for execute if true
}
Example – if statement
Greater Noida Campus

// C program to illustrate If statement


#include <stdio.h>
#include<onio.h> OUTPUT
int main()
??
{
int i = 10;

if (i > 15)
{
printf("10 is greater than 15");
}

printf("I am Not in if");


}
If – Else Statements
Greater Noida Campus

• whether a certain statement or block of statements will be executed.


• If a certain condition is true then a block of statements is
executed.
• what if we want to do something else when the condition is false ?

Syntax
If (condition )
{
// statement for execute if true
}
Else
{
// statement for execute if false
}
Example – if else statement
Greater Noida Campus

// C program to illustrate If statement


#include <stdio.h>
Void main() OUTPUT
{ ??
int i = 20;
if (i < 15)
{
printf("i is smaller than 15");
}
else
{
printf("i is greater than 15");
}
}
Nested if-else Statements
Greater Noida Campus

• if statement that is the target of another if statement.


• Means , if statement inside another if statement .
• what if we want to do something else when the condition is false ?
Syntax
If (condition 1)
{
if (condition 2)
{ // statement 1 to be execute }
else
{ // statement 2 to be execute }
}
Else
{ if (condition 3)
{ // statement 3 to be execute }
else
{ // statement 4 to be execute }
}
Nested if-else Statements
Greater Noida Campus
Example – Nested if statement
Greater Noida Campus

#include <stdio.h>
void main() else
{ {
int i = 10; if (i == 20)
if (i == 10) {
{ printf("i is smaller than 22 too\n");
if (i < 15)
else
printf("i is smaller than 15\n");
if (i < 12) printf("i is greater than 25");
printf("i is smaller than 12 too\n"); }
else
printf("i is greater than 15"); }
}
}
if-else if Ladder Statements
Greater Noida Campus

• if else if statements  are used [when the user has to decide among multiple
options] .
• if statements are executed from the top down
• one of the conditions controlling the if is true,
• the statement associated with  if is executed, and
• the rest of the C else-if ladder is bypassed.
• If none of the conditions is true, then the final else statement will be executed.
if-else if Ladder Statements
Greater Noida Campus

If (condition)

statement;

else if (condition)

statement; . .

else

statement
if-else if Ladder Example
Greater Noida Campus
#include <stdio.h>
int main()
{
int i = 20;
OUTPUT
if (i == 10)
???
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Switch Statement
Greater Noida Campus

• Basically, it is used to perform different actions based on different conditions(cases).

• Switch case statements follow a selection-control mechanism and allow a value to change control of

execution.

• They are a substitute for long if statements that compare a variable to several integral values.

• The switch statement is a multiway branch statement.

• It provides an easy way to dispatch execution to different parts of code based on the value of the

expression.
Switch Statement
Greater Noida Campus

Rules of the switch case statement

• In a switch statement, the “case value” must be of “char” and “int” type.
• There can be one or N number of cases.

• The values in the case must be unique.

• Each statement of the case can have a break statement.


• The default Statement is also optional.
Switch Statement
Greater Noida Campus
Switch Statement
Greater Noida Campus
#include <stdio.h>
#include<stdio.h> int main()
void main() {
{ int num = 2;
int movie = 1; switch (num + 2)
switch (movie << (2 + movie)) {
{ case 1:
default: printf("Case 1: ");
printf(" Traffic"); case 2:
case 4: printf("Case 2: ");
printf(" Sultan"); case 3:
case 5: printf("Case 3: ");
printf(" Dangal"); default:
case 8: printf("Default: ");
printf(" Bahubali"); }
} return 0;
} }
Switch Statement
Greater Noida Campus

#include<stdio.h>
#define L 10 case L*3:
void main() printf("PQR");
{ break;
auto a = 10;
switch (a, a*2)
{ default:
case L: printf("MNO");
printf("ABC"); case L*4:
break; printf("www");
case L*2: break;
printf("XYZ"); }
break; }
Jump Statement
Greater Noida Campus

• jump statement is used to alter the


normal flow of execution of a program.
• It allows the program to transfer control
to a different part of the code,
• like a different function or a different
goto Break Continue
block of code within the same function.

• There are three types of jump statements


in goto, break, and continue.
goto Statement
Greater Noida Campus
#include <stdio.h>
• The goto statement is utilized to transfer control to a void main()
labeled statement in the same function. {
• goto statement to jump back to the label until the int i = 1;
condition is met loop:
printf("%d ", i);
Syntax
goto label;
i++;
.
.
if (i<= 10)
. {
label: statement;
goto loop;
}
}
Break Statement
Greater Noida Campus
#include <stdio.h>
• The break statement is utilized to exit a loop or switch void main()
statement before its normal termination. {
int i;
• It is commonly utilized in loops to exit [early] if any
for (i = 1; i<= 10; i++)
specific condition is met.
{
Syntax
loop (condition) if (i == 5)
{ {
if (condition)
{ break;
break; }
}
} printf("%d ", i);
}
}
Continue Statement
Greater Noida Campus
#include <stdio.h>
• The continue statement is utilized to skip the remaining void main()
code in a loop for the current iteration and move on to {
the next iteration. int i;
• It is commonly used in loops to skip over certain for (i = 1; i<= 10; i++)
elements. {
• Syntax if (i % 2 == 0)
for (int i = 0; i< n; i++) {
{
if (condition) continue;
{ }
continue;
} printf("%d ", i);
// Code to execute if condition is false }
}
}
Return Statement
Greater Noida Campus
#include <stdio.h>
int square(int n)
• C also provides the return statement, which is used to {
if (n < 0)
exit a function and return a value to the calling
{
function. printf("Error: Negative number\n");
return -1;
• The return statement can also be used to terminate a
}
program in the main() function. return n * n;
}
int main()
• Syntax {
return value; int result = square(-5);
if (result == -1) {
return 1;
}
printf("%d", result);
return 0;
}
Loops
Greater Noida Campus

for
Loops are a block of code
that executes [itself ] until while
the specified [ condition] becomes false.

Do while
Loops
Greater Noida Campus

• Loops allow [the user ] to execute the same set of


statements  repeatedly without writing the same code
multiple times.
• It saves time and effort and increases the efficiency.
for
• It reduces the [ chance ] of getting errors during
compilation. while
• Loop makes the code readable and easier to understand,
especially when dealing with complex logic or large data sets. Do while
• It promotes code reusability.
For Loops
Greater Noida Campus
For Loops
Greater Noida Campus
For Loops
Greater Noida Campus

#include <stdio.h>
void main() Output
{
?
int i;
for (i = 0; i < 10; i++)
{
printf("%d\n", i+1);
}
}
For Loops
Greater Noida Campus

#include <stdio.h>
void main() Print
{ Table of
int i; 2
for (i = ??; i < ??; i++)
{
printf("%d\n", ??);
}
}
For Loops
Greater Noida Campus

Output
?
For Loops
Greater Noida Campus
For Loops
Greater Noida Campus

?
While Loop
Greater Noida Campus

#include <stdio.h>
while (condition)
{ void main()
// code block to be executed {
} int i = 0;
while (i < 5)
{
printf("%d\n", i);
i++;
}
}
Do While Loop
Greater Noida Campus
#include <stdio.h>
int main()
do {
{ int i = 0;
// code block to be executed do
} {
while (condition); printf("%d\n", i);
i++;
}
while (i < 5);
}
Exercise
Greater Noida Campus

#include <stdio.h>
void main()
{
int countdown = 3;
while (countdown > 0)
Output ??
{
printf("%d\n", countdown);
countdown--;
}
printf("Happy New Year!!\n");
}
Exercise
Greater Noida Campus

#include <stdio.h>
void main() Output ??
{
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d\n", a, b);
}
Exercise
Greater Noida Campus

#include <stdio.h>
void main() Output ??
{
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("a = %d, b = %d\n", a, b);
}
Storage Classes
Greater Noida Campus

 Storage Classes define the scope (visibility) & lifetime.


 Scope / lifetime  [variables and/or functions] within a C Program.
 They precede the type that they modify/ access modifiers

• Auto
• Register
• Static
• Extern
Auto Storage Classes
Greater Noida Campus

• Default storage class  for all variables.


• Variables are declared  [ inside a function or a block] .
• The keyword "auto",  [optional] , can be used to define local variables.

• The scope and lifetime of auto  variables are within the same block in
which they are declared.
Register Storage Classes
Greater Noida Campus

• The register storage class used  to define local variables.

• Variables should be  [stored][ in a register] instead of RAM.

• This means variable has a maximum size equal to the register size

(usually one word) and can't have the unary '&' operator applied to it (as

it does not have a memory location).


Static Storage Classes
Greater Noida Campus

• Instructs  compiler to keep a local variable  existence  during the life-time


[Program].
• Instead of creating and destroying[ Local Variables].
• Local Variables each time it comes into and goes out of scope.
• Therefore, making  [local variables]static  allows them to maintain their values
between function calls.
• The static modifier also  applied to global variables.
• When this is done, it causes that variable's scope to be restricted to the file in which it is
declared.
Static Storage Classes
Greater Noida Campus

#include <stdio.h>
void func(void);
void func(void)
static int count = 5;
{
void main()
static int i = 5;
{
i++;
while(count--)
printf("i is %d and count is %d\n", i,
{
count);
func();
}
} }
Static Storage Classes
Greater Noida Campus
E Book & Refrences
Greater Noida Campus

Web Links
[Link]

[Link]

[Link]

[Link]

Book Reference

Let US C  Yashwant Kanitkar


Programming in C  E Balagurusamy
Programmin in ANSI C  Tata McGraw-Hill
Assessment Test
Greater Noida Campus

Quiz

You might also like