Programming with
Branching and
Looping
Chapter -1 Operators in C Programming
 Operators in C are special symbols that perform specific operations on one or
more operands.
 They are the building blocks of expressions and are essential for manipulating
data in C programs.
Classification of Operators
Based on Operations Based on Operands
Arithmetic Operators
Relational Operators
Increment / Decrement
Operators
Conditional Operator
Unary Operators
Binary Operators
Ternary Operator
Logical Operators
Bitwise Operators
Special Operators
Arithmetic Operators
Operation Operator Syntax Result Example (a=10,b=5)
Multiply * a*b Result=a*b 50
Divide / a/b Result=a/b 2
Addition + a+b Result=a+b 15
Subtraction - a-b Result=a-b 5
Modulus % a%b Result=a%b 0
 Arithmetic operators can be applied to any integer/ floating-point number.
 The operator % gives reminder of as integer division.
Note- Modulus operator can be applied only to integer operands.
Relational Operators
Also called as comparison operator, evaluates the relationship between two values.
It returns true or false based on the condition between the operands.
Operation Operator Syntax Example (a=10,b=5)
Greater Than > a>b True
Less Than < a<b False
Greater than or equal to >= a>=b True
Less than or equal to <= a<=b False
equal to == a==b False
Not equal to != a!=b True
Logical Operators
• C language supports three logical operators:
1.Logical AND (&&)
2.Logical OR (||)
3.Logical NOT (!)
• Logical NOT (!) has the highest priority.
• && and || has same priority (whichever comes first , will be evaluated first).
1. Logical AND (&&) :
• Logical AND operator is a binary operator, which simultaneously evaluates
two values or relational expression.
• If both or one of the operands is false, then whole expression evaluates to false.
NOTE – Non- Zero number includes both +ve and –ve numbers.
Example-
OUTPUT-
2. Logical OR (||) :
• Logical OR operator is a binary operator, which simultaneously evaluates
two values or relational expression.
• Returns a false value if both the operands are false.
Example -
OUTPUT-
3. Logical NOT (!) :
• Logical NOT operator takes a single expression and negates the value
of the expression.
Example -
OUTPUT-
Pre- Increment/ Pre- Decrement Operators
• Pre-Increment – First increment, then use it in expression.
• Pre- Decrement – First decrement, then use it in expression.
Post- Increment Operator Example -
Int a=5, b=0;
b= ++a;
a= a+1;
a = 5+1 = 6;
++a;
a=6+1 =7;
Dependent expression
(Look at Pre and Post)
Stand Alone Expression
(Priorities)
a
b
6
5 6 7
Post- Increment/ Post- Decrement Operators
• Post- Increment – First use it in expression, then increment it.
• Post- Decrement – First use it in expression, then decrement it.
Post- Increment Operator Example -
Int a=5, b=0;
b= a++;
a= a+1;
a = 5+1 = 6;
a++;
a=6+1 =7;
Dependent expression
(Look at Pre and Post)
Stand Alone Expression
(Priorities) 5
5 6 7 a
b
Bitwise Operators
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Shifts bits to the left side
>> Shifts bits to the right side
Performs operations at bit level
Bitwise AND
Example -
A=5 => 0101
&
B=1 => 0001
---------------------------
C=1 => 0001
Output -
Bitwise OR
Example-
A=5 => 0101
|
B=1 => 0001
---------------------------
C=5 => 0101
Ouput-
Bitwise NOT
Example-
A=5 => 0000 0000 0000 0101
~
------------------------------------------
C= - 6 =>1111 1111 1111 1010
Output-
Bitwise XOR Example-
A=5 => 0101
^
B=1 => 0001
---------------------------
C=4 => 0100
Output-
Left shift
• Left shift operator shifts all bits towards left by certain number of specified
bits.
• Syntax: number << b ( b is number of bits to be shifted).
• Left shift operator is used to multiply the number by 2n
.
Let’s test your knowledge :
2<<1 = ?
4<<1 = ?
2<<3 = ?
-4<<1 = ?
You know what ?
2’s compliment is used to change the sign of a number
Left Shift
Right Shift
• Right shift operator shifts all bits towards right by certain number of specified
bits.
• Syntax: number >> b ( b is number of bits to be shifted).
• Left shift operator is used to divide the number by 2n
.
Let’s test your knowledge :
2>>1 = ?
4>>1 = ?
2>>3 = ?
Right Shift
Conditional Operator
• If condition is true, exp1 is evaluated. Otherwise, exp2 is evaluated.
• Syntax : condition ? exp1 : exp2
• Shorthand for simple if-else statements
• Example: Find greatest of three numbers,
Output:
Assignment Operator
• The most common assignment operator is =.
• Assigns the value in right side to the left side.
Operator Example
Same as
= a=b
a=b
+= a+=b
a=a+b
-= a-=b
a=a-b
*= a*=b
a=a*b
/= a/=b
a=a/b
%= a%=b
Special Operator
• Special Operator includes two operators such as following
1. Comma operator : used to link related expressions together.
2. sizeof operator : Unary operator , used for finding the size of data type,
Constant, array, structure, etc. Output:
Order of evaluation of operators
• An expression can contain several operators with equal precedence.
• When several such operators appear at the same level in an expression
evaluation proceeds according to the associativity of the operator, either
from right to left or from left to right.
Precedence &
associativity table
They allow programs to make decisions, repeat actions, and handle data powerfully.
Chapter 2 -Branching and Looping Statements
Branching Statements
Conditional
Statement
Unconditional Statement
if
if else
nested if else
else if ladder
switch break
continue goto
return
Conditional Statement: The execution flow is decided by the condition
Unconditional Statement: Executes without any condition
if Statement
 If the condition is true the statements inside block will be executed.
 If condition is false it skips the statements and continues with next
statements.
 If there is only one statement in the if block then braces are not needed, if
no statements in if block then braces are compulsory.
 Syntax: if(condition)
{
statements;
}
Example:
if Statement
Condition
Statements
End if
True
False
NextStatements
If-else Statement
 If the condition is true the statement1 is executed and if its false then
statement2 will be executed.
 At least one statement will be executed.
Example:
if(condition)
{
statement1;
}
else {
statement2;
}
Next statement;
Syntax:
If-else Statement
Condition
Statements
End if
True
False
Statements
NextStatements
Nested if else Statement
 if else statement within one or more if else statement
 Used when multiple conditions are to be used.
Continued
if(condition)
{
if(conditon1)
statement-1;
else
statement-2;
}
Syntax:
else
{
if(condition2)
statement-3;
else
statement-4 ;
}
Continuation
Continued
Nested if else Statement
Condition
Condition Condition
Statement1
Statement2
Statement3
Statement4
NextStatement
False
False
False
True
True
True
Continuation
Else if ladder
 This is used when multiple conditions are to be
checked on the same data, at least one statement will
be executed.
 The else block at end is compulsory.
if(condition1)
statement-1;
else if(condition2)
statement-2;
Syntax:
Continued
else if(condition)
statement-3 ;
.
.
else
statement-N;
Continuation
Continued
Continuation
Else if ladder
switch statement
 Simplified multiway decision statement evaluating a single variable.
 Compares the variable in the switch statement with each
subsequent case value.
Continued
switch statement
Syntax:
switch(var)
{
value1: //statement1
break;
value2:
//statement2
break;
.
.
valueN:
Continuation
break statement
 These break statements are used when the control
has to be brought out of block
 It can be used in the switch and the loop statements.
for(…….)
{
…..
if(condition)
{
break;
……..
}
}
………..
Example:
continue statement
 Continue statement is used when the current iteration has to
be skipped in loops.
 Used only inside the loops.
for(… : …. : ….)
{
…..
if(condition)
{
continue ;
……..
}
………..
}
Example:
goto statement
 goto statements can be used for unconditional jump.
 There are two types of goto statements,
1.Forward jump
2.Backward jump
Forward jump: Backward jump:
goto Label :
………..
…………..
……….
………
Label:
Label :
………..
…………..
……….
………
goto Label:
Continued
goto statement
Continuation
return statement
 It is used to return from the function.
 When this statement is used it restores the address stored on stack.
Example :
int add(int x, int y)
{
int sum=x + y;
return sum;
}
int main()
{
int a=5,b=10
printf(“%d”, add(a , b));
return 0;
}
Iterative statement
while loop do while loop for loop
while loop
while(condition)
{
Statements;
Aux_var_update;
}
Syntax
Statement X
Update the
condition
expression
Statement Block
Next
Statement
Condition
False
True
 The loop continues executing
as long as the condition is
true; once the condition fails,
it exits the loop.
You know ? It is also called as pretest loop or entry controlled loop.
Next_Statements
True
False
 The loop keeps on executing till the condition is true; once
condition fails it comes out of loop.
 The loop will be executed at least once even if the condition is
false as you are checking the condition at the end.
 Semi colon is used to indicate the end of do while loop.
do while loop
Continued
do while loop
do
{
Statements;
Aux_var_update;
} while(condition);
Syntax
Statement X
Statement Block
Update the
Condition
expression
Condition
Next Statement
true
false
Continuation
You know ?
It is also called exit control loop or post test loop.
For Loop
 This loop is most used loop, it has three parts one is initialization
where we can initialize
 variables, Condition and update part.
Syntax
for( initialization : condition : Update)
{
Statements;
}
Statements;
1
2 3
When condition fails
Continued
4
Initialization of
loop variable
Condition
Statement block
Updating loop
variable
NextStatement
False
True
For Loop
Continuation
Any Doubts ????

Module2.2_Operators-in-C-Programming.pptx

  • 1.
  • 2.
    Chapter -1 Operatorsin C Programming  Operators in C are special symbols that perform specific operations on one or more operands.  They are the building blocks of expressions and are essential for manipulating data in C programs.
  • 3.
    Classification of Operators Basedon Operations Based on Operands Arithmetic Operators Relational Operators Increment / Decrement Operators Conditional Operator Unary Operators Binary Operators Ternary Operator Logical Operators Bitwise Operators Special Operators
  • 4.
    Arithmetic Operators Operation OperatorSyntax Result Example (a=10,b=5) Multiply * a*b Result=a*b 50 Divide / a/b Result=a/b 2 Addition + a+b Result=a+b 15 Subtraction - a-b Result=a-b 5 Modulus % a%b Result=a%b 0  Arithmetic operators can be applied to any integer/ floating-point number.  The operator % gives reminder of as integer division. Note- Modulus operator can be applied only to integer operands.
  • 5.
    Relational Operators Also calledas comparison operator, evaluates the relationship between two values. It returns true or false based on the condition between the operands. Operation Operator Syntax Example (a=10,b=5) Greater Than > a>b True Less Than < a<b False Greater than or equal to >= a>=b True Less than or equal to <= a<=b False equal to == a==b False Not equal to != a!=b True
  • 6.
    Logical Operators • Clanguage supports three logical operators: 1.Logical AND (&&) 2.Logical OR (||) 3.Logical NOT (!) • Logical NOT (!) has the highest priority. • && and || has same priority (whichever comes first , will be evaluated first).
  • 7.
    1. Logical AND(&&) : • Logical AND operator is a binary operator, which simultaneously evaluates two values or relational expression. • If both or one of the operands is false, then whole expression evaluates to false. NOTE – Non- Zero number includes both +ve and –ve numbers. Example- OUTPUT-
  • 8.
    2. Logical OR(||) : • Logical OR operator is a binary operator, which simultaneously evaluates two values or relational expression. • Returns a false value if both the operands are false. Example - OUTPUT-
  • 9.
    3. Logical NOT(!) : • Logical NOT operator takes a single expression and negates the value of the expression. Example - OUTPUT-
  • 10.
    Pre- Increment/ Pre-Decrement Operators • Pre-Increment – First increment, then use it in expression. • Pre- Decrement – First decrement, then use it in expression. Post- Increment Operator Example - Int a=5, b=0; b= ++a; a= a+1; a = 5+1 = 6; ++a; a=6+1 =7; Dependent expression (Look at Pre and Post) Stand Alone Expression (Priorities) a b 6 5 6 7
  • 11.
    Post- Increment/ Post-Decrement Operators • Post- Increment – First use it in expression, then increment it. • Post- Decrement – First use it in expression, then decrement it. Post- Increment Operator Example - Int a=5, b=0; b= a++; a= a+1; a = 5+1 = 6; a++; a=6+1 =7; Dependent expression (Look at Pre and Post) Stand Alone Expression (Priorities) 5 5 6 7 a b
  • 12.
    Bitwise Operators & BitwiseAND | Bitwise OR ^ Bitwise XOR ~ Bitwise NOT << Shifts bits to the left side >> Shifts bits to the right side Performs operations at bit level
  • 13.
    Bitwise AND Example - A=5=> 0101 & B=1 => 0001 --------------------------- C=1 => 0001 Output -
  • 14.
    Bitwise OR Example- A=5 =>0101 | B=1 => 0001 --------------------------- C=5 => 0101 Ouput-
  • 15.
    Bitwise NOT Example- A=5 =>0000 0000 0000 0101 ~ ------------------------------------------ C= - 6 =>1111 1111 1111 1010 Output-
  • 16.
    Bitwise XOR Example- A=5=> 0101 ^ B=1 => 0001 --------------------------- C=4 => 0100 Output-
  • 17.
    Left shift • Leftshift operator shifts all bits towards left by certain number of specified bits. • Syntax: number << b ( b is number of bits to be shifted). • Left shift operator is used to multiply the number by 2n . Let’s test your knowledge : 2<<1 = ? 4<<1 = ? 2<<3 = ? -4<<1 = ? You know what ? 2’s compliment is used to change the sign of a number
  • 18.
  • 19.
    Right Shift • Rightshift operator shifts all bits towards right by certain number of specified bits. • Syntax: number >> b ( b is number of bits to be shifted). • Left shift operator is used to divide the number by 2n . Let’s test your knowledge : 2>>1 = ? 4>>1 = ? 2>>3 = ?
  • 20.
  • 21.
    Conditional Operator • Ifcondition is true, exp1 is evaluated. Otherwise, exp2 is evaluated. • Syntax : condition ? exp1 : exp2 • Shorthand for simple if-else statements • Example: Find greatest of three numbers, Output:
  • 22.
    Assignment Operator • Themost common assignment operator is =. • Assigns the value in right side to the left side. Operator Example Same as = a=b a=b += a+=b a=a+b -= a-=b a=a-b *= a*=b a=a*b /= a/=b a=a/b %= a%=b
  • 23.
    Special Operator • SpecialOperator includes two operators such as following 1. Comma operator : used to link related expressions together. 2. sizeof operator : Unary operator , used for finding the size of data type, Constant, array, structure, etc. Output:
  • 24.
    Order of evaluationof operators • An expression can contain several operators with equal precedence. • When several such operators appear at the same level in an expression evaluation proceeds according to the associativity of the operator, either from right to left or from left to right.
  • 25.
  • 26.
    They allow programsto make decisions, repeat actions, and handle data powerfully. Chapter 2 -Branching and Looping Statements
  • 27.
    Branching Statements Conditional Statement Unconditional Statement if ifelse nested if else else if ladder switch break continue goto return Conditional Statement: The execution flow is decided by the condition Unconditional Statement: Executes without any condition
  • 28.
    if Statement  Ifthe condition is true the statements inside block will be executed.  If condition is false it skips the statements and continues with next statements.  If there is only one statement in the if block then braces are not needed, if no statements in if block then braces are compulsory.  Syntax: if(condition) { statements; } Example:
  • 29.
  • 30.
    If-else Statement  Ifthe condition is true the statement1 is executed and if its false then statement2 will be executed.  At least one statement will be executed. Example: if(condition) { statement1; } else { statement2; } Next statement; Syntax:
  • 31.
  • 32.
    Nested if elseStatement  if else statement within one or more if else statement  Used when multiple conditions are to be used. Continued if(condition) { if(conditon1) statement-1; else statement-2; } Syntax: else { if(condition2) statement-3; else statement-4 ; } Continuation Continued
  • 33.
    Nested if elseStatement Condition Condition Condition Statement1 Statement2 Statement3 Statement4 NextStatement False False False True True True Continuation
  • 34.
    Else if ladder This is used when multiple conditions are to be checked on the same data, at least one statement will be executed.  The else block at end is compulsory. if(condition1) statement-1; else if(condition2) statement-2; Syntax: Continued else if(condition) statement-3 ; . . else statement-N; Continuation Continued
  • 35.
  • 36.
    switch statement  Simplifiedmultiway decision statement evaluating a single variable.  Compares the variable in the switch statement with each subsequent case value. Continued
  • 37.
  • 38.
    break statement  Thesebreak statements are used when the control has to be brought out of block  It can be used in the switch and the loop statements. for(…….) { ….. if(condition) { break; …….. } } ……….. Example:
  • 39.
    continue statement  Continuestatement is used when the current iteration has to be skipped in loops.  Used only inside the loops. for(… : …. : ….) { ….. if(condition) { continue ; …….. } ……….. } Example:
  • 40.
    goto statement  gotostatements can be used for unconditional jump.  There are two types of goto statements, 1.Forward jump 2.Backward jump Forward jump: Backward jump: goto Label : ……….. ………….. ………. ……… Label: Label : ……….. ………….. ………. ……… goto Label: Continued
  • 41.
  • 42.
    return statement  Itis used to return from the function.  When this statement is used it restores the address stored on stack. Example : int add(int x, int y) { int sum=x + y; return sum; } int main() { int a=5,b=10 printf(“%d”, add(a , b)); return 0; }
  • 43.
    Iterative statement while loopdo while loop for loop
  • 44.
    while loop while(condition) { Statements; Aux_var_update; } Syntax Statement X Updatethe condition expression Statement Block Next Statement Condition False True  The loop continues executing as long as the condition is true; once the condition fails, it exits the loop. You know ? It is also called as pretest loop or entry controlled loop. Next_Statements True False
  • 45.
     The loopkeeps on executing till the condition is true; once condition fails it comes out of loop.  The loop will be executed at least once even if the condition is false as you are checking the condition at the end.  Semi colon is used to indicate the end of do while loop. do while loop Continued
  • 46.
    do while loop do { Statements; Aux_var_update; }while(condition); Syntax Statement X Statement Block Update the Condition expression Condition Next Statement true false Continuation You know ? It is also called exit control loop or post test loop.
  • 47.
    For Loop  Thisloop is most used loop, it has three parts one is initialization where we can initialize  variables, Condition and update part. Syntax for( initialization : condition : Update) { Statements; } Statements; 1 2 3 When condition fails Continued 4
  • 48.
    Initialization of loop variable Condition Statementblock Updating loop variable NextStatement False True For Loop Continuation
  • 49.