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
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
}
Else-if Ladder Statements
if(condition 1)
statement 1;
else if(condition 2)
statement 2;
"
"
"
else if(condition n)
statement n;
else default statement;
switch Statement
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++;
}
}

Unit 2- Control Structures in C programming.pptx

  • 1.
    Unit 2 :Instructions & Control Structures
  • 2.
    Variable Initialization inC 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 andR 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):Anl-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 • Convertingone 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 InC 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 • Implicitcasting, 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() { intintValue = 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 conversionsin 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 andConversion:
  • 13.
    Operators in C Anoperator 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;
  • 15.
  • 16.
    Relational Operators Assume variableA holds 10 and variable B holds 20 then:
  • 17.
  • 19.
  • 20.
    Increment/Decrement Operators Increment Operator Incrementoperator 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.
  • 23.
  • 24.
    Decision Making Statements ifstatement 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) willexecute 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(condition1) statement 1; else if(condition 2) statement 2; " " " else if(condition n) statement n; else default statement;
  • 33.
  • 35.
  • 36.
  • 37.
  • 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> voidmain() { int var =5; while (var <=10) { printf("%d", var); var--; } }
  • 40.
    Multiple conditions canbe 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 whileloop • 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() { inta = 1, b = 1; while(a <= 3) { b = 1; while(b <= 3) { printf("%d ", b); b++; } printf("n"); a++; } }