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++;
}
}

More Related Content

PDF
CP Handout#3
PDF
C Operators and Control Structures.pdf
PPT
Basic concept of c++
PPTX
Operators in C Programming
PPTX
C sharp part 001
PDF
Types of Operators in C
PPTX
C Operators and Control Structures.pptx
PPT
Csharp4 operators and_casts
CP Handout#3
C Operators and Control Structures.pdf
Basic concept of c++
Operators in C Programming
C sharp part 001
Types of Operators in C
C Operators and Control Structures.pptx
Csharp4 operators and_casts

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

PDF
Programming for Problem Solving
PDF
C Building Blocks
PPT
C++ chapter 2
PDF
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
PPTX
Fundamentals of computers - C Programming
PPTX
C Language Part 1
PDF
introduction to c programming - Topic 3.pdf
PPTX
What is c
PPTX
Control statements in c
PPTX
Control Structures in C
PPT
C operators
PDF
[C++][a] tutorial 2
PPT
Lecture 3
PPTX
C PRESENTATION.pptx
PPTX
COM1407: Type Casting, Command Line Arguments and Defining Constants
DOCX
Programming Fundamentals lecture 7
PPTX
C basics
PPTX
C basics
PPTX
operatorsincprogramming-190221094522.pptx
Programming for Problem Solving
C Building Blocks
C++ chapter 2
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Fundamentals of computers - C Programming
C Language Part 1
introduction to c programming - Topic 3.pdf
What is c
Control statements in c
Control Structures in C
C operators
[C++][a] tutorial 2
Lecture 3
C PRESENTATION.pptx
COM1407: Type Casting, Command Line Arguments and Defining Constants
Programming Fundamentals lecture 7
C basics
C basics
operatorsincprogramming-190221094522.pptx
Ad

More from shilpar780389 (6)

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

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Machine learning based COVID-19 study performance prediction
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectroscopy.pptx food analysis technology
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
20250228 LYD VKU AI Blended-Learning.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine learning based COVID-19 study performance prediction
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
sap open course for s4hana steps from ECC to s4
KodekX | Application Modernization Development

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++; } }