SlideShare a Scribd company logo
Unit 2 : Instructions &
Control Structures
Variable Initialization in C
Variables are initialized (assigned a value) with an equal sign
followed by a constant expression.
The general form of initialization is:
variable name = value;
Example: a=10;
datatype variable_name = value;
Example
• int n = 0;
• float avg=0;
• char c=’a’;
L values and R values in C
int x = 10; // x is an l-value, 10 is an r-value
int arr[5]; arr[2] = 20; // arr[2] is an l-value, 20 is an r-
value
5 = x; //Guess?
l-value (Left Value):An l-value refers to an object that occupies
a specific location in memory (i.e., it has a memory address). It
can appear on the left side of an assignment expression.
Examples: Variables, array elements, dereferenced pointers.
Characteristics: l-values can be modified (if they are not const),
and they refer to a memory location.
r-value (Right Value):
An r-value refers to a value that does not occupy a specific
location in memory. It represents a temporary value that can be
used in expressions but cannot be assigned to directly.
Examples: Constants, literals, temporary results of expressions.
Characteristics: r-values cannot be assigned to because they do
not have a specific memory address.
Type Conversions
• Converting one datatype into another is known as type
casting or, type-conversion. For example, if you want to
store a 'long' value into a simple integer then you can type
cast 'long' to 'int'. You can convert the values from one type
to another explicitly using the cast operator as:
(type_name) expression
C can perform conversions between different data types.
There are two types of type conversions.
i. Implicit type conversion
ii. Explicit type conversion
Data Types In C
In C programming, data types are classifications that specify
which type of data a variable can hold. They determine the
kind of values that can be stored and the operations that can
be performed on those values.
Implicit Casting
• Implicit casting, also known as automatic type conversion,
happens automatically when you mix different data types in an
expression.
• The C compiler automatically converts one type to another
based on certain rules to ensure that the operation can be
performed correctly.
• Rules for Implicit Casting:
• Widening Conversion: When you mix types, C typically converts
the smaller or less precise type to a larger or more precise type.
• For example: int to float float to double
• Integer Promotion: In expressions involving char or short, these
types are promoted to int before performing operations.
#include <stdio.h>
int main()
{
int intValue = 10;
float floatValue = 3.5;
// Implicit casting from int to float
float result = intValue + floatValue;
printf("Result: %fn", result); // Output: Result: 13.500000
return 0;
}
Explicit Casting
Explicit casting, also known as type casting or manual type
conversion, is where you manually specify the type conversion
using a cast operator.
This is done when you need to convert a variable from one
type to another explicitly.
Syntax for Explicit Casting:
(type) expression
type is the data type you want to convert to.
expression is the value or variable you want to cast.
int num = 5;
float result;
result = (float) num / 2; // Casting num to float
printf("Result: %fn", result); // Output: Result: 2.500000
Arithmetic Conversion
Arithmetic conversions in C ensure that operands of different
types are converted to a common type before performing
arithmetic operations. This process is crucial for maintaining
consistency and correctness in arithmetic operations.
char c = 5;
int result = c + 10; // char c is promoted to int before
addition
Type Hierarchy and Conversion:
Operators in C
An operator is a symbol that tells the compiler to perform a certain
mathematical or logical manipulation. Operators are used in programs to
manipulate data and variables.
C operators can be classified into following types based on the operation it
does:
• Arithmetic operators
• Relational operators
• Logical operators
• Bitwise Operators
• Assignment operators
• Conditional operators
• Special operators
• Unary operators : e.g. increment operator( ++) or the decrement
operator( - - ). int a =b++; or something like int C =d- -;
• Binary operators : e.g. ‘+’, ‘ -’, ‘ *’, ‘/’. Syntax can be like int C=a+b;
• Ternary operators :are the one that operates on three operands.
It takes three argumets .
• 1st argument is checked for its validity .
• If it is true 2nd argument is returned else third argument is
returned.
For example int large=
num1>num2 ? num1:num2;
Arithmetic Operators
Relational Operators
Assume variable A holds 10 and variable B holds 20 then:
Assignment Operators
Unit 2- Control Structures in C programming.pptx
Logical Operators
Increment/Decrement Operators
Increment Operator
Increment operator is used to increment the current value of variable by
adding integer 1 and can be applied to only variables denoted by ++.
Pre-Increment Operator : ++a
Example: a=5;
b = ++a;
The value of b will be 6 because 'a' is incremented first and then
assigned to 'b'.value of 'a' is 6
Post-Increment Operator : a++
Example: a=5;
b = a++;
• Decrement Operator
• Decrement operator is used to decrease the current value of
variable by subtracting integer 1 and can be applied to only
variables and is denoted by --.
• Pre-decrement operator : --a
• Example: a=10;
b=--a;
• Post-decrement Operator :a--
• Example: a=10;
b=a--;
Conditional Operator
The “ ? : ” Operator
The conditional operator ? : can be used to
replace if...else statements. It has the following general form:
Exp1 ? Exp2 : Exp3;
where Exp1, Exp2, and Exp3 are expressions.
Bitwise Operators
Decision Making Statements
if statement
if...else statement
nested if statements
switch statement
if Statement
Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the
boolean expression is true */
}
#include <stdio.h>
void main ()
{
int a=11,b=50;
if( a > b )
{
printf("%d is biggern",a);
}
printf(%d is biggern”,b);
}
if-else Statement
if(boolean_expression)
{
//statement(s) will execute if the boolean expression is true
}
else
{
// statement(s) will execute if the boolean expression is false
}
#include <stdio.h>
void main ()
{
int a = 100,b=20;
if( a > b )
{
printf("%d is greater than %dn",a,b );
}
else
{
printf("%d is lesser than %dn",a,b );
}
printf("Outside if-elsen");
}
Nested if statement
if ( test condition 1)
{
//If the test condition 1 is TRUE then these it will check for test
condition 2
if ( test condition 2)
{
/*If the test condition 2 is TRUE then these statements
will be executed*/
}
else
{
/*If the c test condition 2 is FALSE then these
statements will be executed*/
}
}
else
{
//If the test condition 1 is FALSE then these statements will be executed
}
Unit 2- Control Structures in C programming.pptx
Else-if Ladder Statements
if(condition 1)
statement 1;
else if(condition 2)
statement 2;
"
"
"
else if(condition n)
statement n;
else default statement;
Unit 2- Control Structures in C programming.pptx
switch Statement
Unit 2- Control Structures in C programming.pptx
Nested-Switch Statement
Loops
while loop
for loop
do...while loop
while loop
Syntax:
while(condition)
{
statement(s);
}
#include <stdio.h>
void main ()
{
int a = 10;
while( a <= 15 )
{
printf("value of a: %dn", a);
a++;
}
}
infinite loop
#include <stdio.h>
void main()
{
int var =5;
while (var <=10)
{
printf("%d", var);
var--;
}
}
Multiple conditions can be tested using logical operator inside while loop.
#include <stdio.h>
void main()
{
int i=1, j=1;
while (i <= 4 || j <= 3)
{
printf("%d %dn",i, j);
i++;
j++;
}
}
• Nested while loop
• Using While loop within while loops is said to be nested while loop. In nested
while loop one or more statements are included in the body of the loop. In
nested while loop, the number of iterations will be equal to the number of
iterations in the outer loop multiplies by the number of iterations in the inner
loop which is most same as nested for loop.
Syntax:
while (test condition)
{
while (test condition)
{
// inner while loop
}
// outer while loop
}
#include <stdio.h>
void main()
{
int a = 1, b = 1;
while(a <= 3)
{
b = 1;
while(b <= 3)
{
printf("%d ", b);
b++;
}
printf("n");
a++;
}
}
Ad

More Related Content

Similar to Unit 2- Control Structures in C programming.pptx (20)

Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
Kathirvel Ayyaswamy
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
Army Public School and College -Faisal
 
C Building Blocks
C Building Blocks C Building Blocks
C Building Blocks
imtiazalijoono
 
C++ chapter 2
C++ chapter 2C++ chapter 2
C++ chapter 2
SHRIRANG PINJARKAR
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdfModule 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
Fundamentals of computers - C Programming
Fundamentals of computers - C ProgrammingFundamentals of computers - C Programming
Fundamentals of computers - C Programming
MSridhar18
 
C Language Part 1
C Language Part 1C Language Part 1
C Language Part 1
Thapar Institute
 
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdfintroduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
rajd20284
 
What is c
What is cWhat is c
What is c
pacatarpit
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C operators
C operatorsC operators
C operators
GPERI
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
yasir_cesc
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Mohammed Saleh
 
C PRESENTATION.pptx
C PRESENTATION.pptxC PRESENTATION.pptx
C PRESENTATION.pptx
VAIBHAV175947
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
Hemantha Kulathilake
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
REHAN IJAZ
 
C basics
C basicsC basics
C basics
sridevi5983
 
C basics
C basicsC basics
C basics
sridevi5983
 
operatorsincprogramming-190221094522.pptx
operatorsincprogramming-190221094522.pptxoperatorsincprogramming-190221094522.pptx
operatorsincprogramming-190221094522.pptx
ShirishaBuduputi
 
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdfModule 2_PPT_P1 POP Notes module 2 fdfd.pdf
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
anilcsbs
 
Fundamentals of computers - C Programming
Fundamentals of computers - C ProgrammingFundamentals of computers - C Programming
Fundamentals of computers - C Programming
MSridhar18
 
introduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdfintroduction to c programming - Topic 3.pdf
introduction to c programming - Topic 3.pdf
rajd20284
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C operators
C operatorsC operators
C operators
GPERI
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
yasir_cesc
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
Hemantha Kulathilake
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
REHAN IJAZ
 
operatorsincprogramming-190221094522.pptx
operatorsincprogramming-190221094522.pptxoperatorsincprogramming-190221094522.pptx
operatorsincprogramming-190221094522.pptx
ShirishaBuduputi
 

More from shilpar780389 (6)

BCA - Chapter Groups- presentation(ppt).pptx
BCA - Chapter Groups- presentation(ppt).pptxBCA - Chapter Groups- presentation(ppt).pptx
BCA - Chapter Groups- presentation(ppt).pptx
shilpar780389
 
Internet_Technology_UNIT V- Introduction to XML.pptx
Internet_Technology_UNIT V- Introduction to XML.pptxInternet_Technology_UNIT V- Introduction to XML.pptx
Internet_Technology_UNIT V- Introduction to XML.pptx
shilpar780389
 
Data Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptxData Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptx
shilpar780389
 
UNIT 3- Java- Inheritance, Multithreading.pptx
UNIT 3- Java- Inheritance, Multithreading.pptxUNIT 3- Java- Inheritance, Multithreading.pptx
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
UNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptxUNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
Unit 1 – Introduction to Java- (Shilpa R).pptx
Unit 1 – Introduction to Java- (Shilpa R).pptxUnit 1 – Introduction to Java- (Shilpa R).pptx
Unit 1 – Introduction to Java- (Shilpa R).pptx
shilpar780389
 
BCA - Chapter Groups- presentation(ppt).pptx
BCA - Chapter Groups- presentation(ppt).pptxBCA - Chapter Groups- presentation(ppt).pptx
BCA - Chapter Groups- presentation(ppt).pptx
shilpar780389
 
Internet_Technology_UNIT V- Introduction to XML.pptx
Internet_Technology_UNIT V- Introduction to XML.pptxInternet_Technology_UNIT V- Introduction to XML.pptx
Internet_Technology_UNIT V- Introduction to XML.pptx
shilpar780389
 
Data Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptxData Structures-UNIT Four_Linked_List.pptx
Data Structures-UNIT Four_Linked_List.pptx
shilpar780389
 
UNIT 3- Java- Inheritance, Multithreading.pptx
UNIT 3- Java- Inheritance, Multithreading.pptxUNIT 3- Java- Inheritance, Multithreading.pptx
UNIT 3- Java- Inheritance, Multithreading.pptx
shilpar780389
 
UNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptxUNIT – 2 Features of java- (Shilpa R).pptx
UNIT – 2 Features of java- (Shilpa R).pptx
shilpar780389
 
Unit 1 – Introduction to Java- (Shilpa R).pptx
Unit 1 – Introduction to Java- (Shilpa R).pptxUnit 1 – Introduction to Java- (Shilpa R).pptx
Unit 1 – Introduction to Java- (Shilpa R).pptx
shilpar780389
 
Ad

Recently uploaded (20)

2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Ad

Unit 2- Control Structures in C programming.pptx

  • 1. Unit 2 : Instructions & Control Structures
  • 2. Variable Initialization in C Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is: variable name = value; Example: a=10; datatype variable_name = value; Example • int n = 0; • float avg=0; • char c=’a’;
  • 3. L values and R values in C int x = 10; // x is an l-value, 10 is an r-value int arr[5]; arr[2] = 20; // arr[2] is an l-value, 20 is an r- value 5 = x; //Guess?
  • 4. l-value (Left Value):An l-value refers to an object that occupies a specific location in memory (i.e., it has a memory address). It can appear on the left side of an assignment expression. Examples: Variables, array elements, dereferenced pointers. Characteristics: l-values can be modified (if they are not const), and they refer to a memory location. r-value (Right Value): An r-value refers to a value that does not occupy a specific location in memory. It represents a temporary value that can be used in expressions but cannot be assigned to directly. Examples: Constants, literals, temporary results of expressions. Characteristics: r-values cannot be assigned to because they do not have a specific memory address.
  • 5. Type Conversions • Converting one datatype into another is known as type casting or, type-conversion. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as: (type_name) expression C can perform conversions between different data types. There are two types of type conversions. i. Implicit type conversion ii. Explicit type conversion
  • 6. Data Types In C In C programming, data types are classifications that specify which type of data a variable can hold. They determine the kind of values that can be stored and the operations that can be performed on those values.
  • 7. Implicit Casting • Implicit casting, also known as automatic type conversion, happens automatically when you mix different data types in an expression. • The C compiler automatically converts one type to another based on certain rules to ensure that the operation can be performed correctly. • Rules for Implicit Casting: • Widening Conversion: When you mix types, C typically converts the smaller or less precise type to a larger or more precise type. • For example: int to float float to double • Integer Promotion: In expressions involving char or short, these types are promoted to int before performing operations.
  • 8. #include <stdio.h> int main() { int intValue = 10; float floatValue = 3.5; // Implicit casting from int to float float result = intValue + floatValue; printf("Result: %fn", result); // Output: Result: 13.500000 return 0; }
  • 9. Explicit Casting Explicit casting, also known as type casting or manual type conversion, is where you manually specify the type conversion using a cast operator. This is done when you need to convert a variable from one type to another explicitly. Syntax for Explicit Casting: (type) expression type is the data type you want to convert to. expression is the value or variable you want to cast.
  • 10. int num = 5; float result; result = (float) num / 2; // Casting num to float printf("Result: %fn", result); // Output: Result: 2.500000
  • 11. Arithmetic Conversion Arithmetic conversions in C ensure that operands of different types are converted to a common type before performing arithmetic operations. This process is crucial for maintaining consistency and correctness in arithmetic operations. char c = 5; int result = c + 10; // char c is promoted to int before addition
  • 12. Type Hierarchy and Conversion:
  • 13. Operators in C An operator is a symbol that tells the compiler to perform a certain mathematical or logical manipulation. Operators are used in programs to manipulate data and variables. C operators can be classified into following types based on the operation it does: • Arithmetic operators • Relational operators • Logical operators • Bitwise Operators • Assignment operators • Conditional operators • Special operators
  • 14. • Unary operators : e.g. increment operator( ++) or the decrement operator( - - ). int a =b++; or something like int C =d- -; • Binary operators : e.g. ‘+’, ‘ -’, ‘ *’, ‘/’. Syntax can be like int C=a+b; • Ternary operators :are the one that operates on three operands. It takes three argumets . • 1st argument is checked for its validity . • If it is true 2nd argument is returned else third argument is returned. For example int large= num1>num2 ? num1:num2;
  • 16. Relational Operators Assume variable A holds 10 and variable B holds 20 then:
  • 20. Increment/Decrement Operators Increment Operator Increment operator is used to increment the current value of variable by adding integer 1 and can be applied to only variables denoted by ++. Pre-Increment Operator : ++a Example: a=5; b = ++a; The value of b will be 6 because 'a' is incremented first and then assigned to 'b'.value of 'a' is 6 Post-Increment Operator : a++ Example: a=5; b = a++;
  • 21. • Decrement Operator • Decrement operator is used to decrease the current value of variable by subtracting integer 1 and can be applied to only variables and is denoted by --. • Pre-decrement operator : --a • Example: a=10; b=--a; • Post-decrement Operator :a-- • Example: a=10; b=a--;
  • 22. Conditional Operator The “ ? : ” Operator The conditional operator ? : can be used to replace if...else statements. It has the following general form: Exp1 ? Exp2 : Exp3; where Exp1, Exp2, and Exp3 are expressions.
  • 24. Decision Making Statements if statement if...else statement nested if statements switch statement
  • 25. if Statement Syntax: if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ }
  • 26. #include <stdio.h> void main () { int a=11,b=50; if( a > b ) { printf("%d is biggern",a); } printf(%d is biggern”,b); }
  • 27. if-else Statement if(boolean_expression) { //statement(s) will execute if the boolean expression is true } else { // statement(s) will execute if the boolean expression is false }
  • 28. #include <stdio.h> void main () { int a = 100,b=20; if( a > b ) { printf("%d is greater than %dn",a,b ); } else { printf("%d is lesser than %dn",a,b ); } printf("Outside if-elsen"); }
  • 29. Nested if statement if ( test condition 1) { //If the test condition 1 is TRUE then these it will check for test condition 2 if ( test condition 2) { /*If the test condition 2 is TRUE then these statements will be executed*/ } else { /*If the c test condition 2 is FALSE then these statements will be executed*/ } } else { //If the test condition 1 is FALSE then these statements will be executed }
  • 31. Else-if Ladder Statements if(condition 1) statement 1; else if(condition 2) statement 2; " " " else if(condition n) statement n; else default statement;
  • 38. #include <stdio.h> void main () { int a = 10; while( a <= 15 ) { printf("value of a: %dn", a); a++; } }
  • 39. infinite loop #include <stdio.h> void main() { int var =5; while (var <=10) { printf("%d", var); var--; } }
  • 40. Multiple conditions can be tested using logical operator inside while loop. #include <stdio.h> void main() { int i=1, j=1; while (i <= 4 || j <= 3) { printf("%d %dn",i, j); i++; j++; } }
  • 41. • Nested while loop • Using While loop within while loops is said to be nested while loop. In nested while loop one or more statements are included in the body of the loop. In nested while loop, the number of iterations will be equal to the number of iterations in the outer loop multiplies by the number of iterations in the inner loop which is most same as nested for loop. Syntax: while (test condition) { while (test condition) { // inner while loop } // outer while loop }
  • 42. #include <stdio.h> void main() { int a = 1, b = 1; while(a <= 3) { b = 1; while(b <= 3) { printf("%d ", b); b++; } printf("n"); a++; } }