With : Rumman Ansari
Operators Precedence in C Category Operator Associativity Postfix () [] -> . ++ - - Left to right Unary + - ! ~ ++ - - (type)* & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left Comma , Left to right
#include<stdio.h> void main() { int x; x=-3+4*5-6; printf("%d n",x); x=3+(4%5)-6; printf("%d n",x); x=-3*4%-6/5; printf("%d n",x); x=(7+6)%5/2; printf("%d n",x); } 11 1 0 1 Program Output
#include<stdio.h> void main() { int x=2,y,z; x*=3+2; printf("%d n",x); x*=y=z=4; printf("%d n",x); x=y==z; printf("%d n",x); x==(y=z); printf("%d n",x); } 10 40 1 1 Program Output
#include<stdio.h> void main() { int x,y,z; x=2; y=1; z=9; x= x&&y||z; printf("%d n",x); printf("%d n",x||!y&&z); x=y=1; z=x++ -1; printf("%d n",x); printf("%d n",z); z+=-x ++ + ++y ; printf("%d n",x); printf("%d n",z); } Program Output 1 1 2 0 3 0
Decision Making Statement Description if statement An if statement consists of a boolean expression followed by one or more statements. if...else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. nested if statements You can use one if or else if statement inside another if orelse if statement(s). switch statement A switch statement allows a variable to be tested for equality against a list of values. nested switch statements You can use one switch statement inside another switchstatement(s).
if #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* check the boolean condition using if statement */ if( a < 20 ) { /* if condition is true then print the following */ printf("a is less than 20n" ); } printf("value of a is : %dn", a); return 0; } Flow Diagram: 1
if...else statement Syntax: if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ } Flow Diagram: 2
#include <stdio.h> int main () { /* local variable definition */ int a = 100; /* check the boolean condition */ if( a < 20 ) { /* if condition is true then print the following */ printf("a is less than 20n" ); } else { /* if condition is false then print the following */ printf("a is not less than 20n" ); } printf("value of a is : %dn", a); return 0; }
if...else if...else Statement Syntax: if(boolean_expression 1) { /* Executes when the boolean expression 1 is true */ } else if( boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } else if( boolean_expression 3) { /* Executes when the boolean expression 3 is true */ } else { /* executes when the none of the above condition is true */ } 3
Example #include <stdio.h> int main () { /* local variable definition */ int a = 100; /* check the boolean condition */ if( a == 10 ) { /* if condition is true then print the following */ printf("Value of a is 10n" ); } else if( a == 20 ) { /* if else if condition is true */ printf("Value of a is 20n" ); } else if( a == 30 ) { /* if else if condition is true */ printf("Value of a is 30n" ); } else { /* if none of the conditions is true */ printf("None of the values is matchingn" ); } printf("Exact value of a is: %dn", a ); return 0; }
nested if statements4 Syntax: if( boolean_expression 1) { /* Executes when the boolean expression 1 is true */ if(boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } }
Example #include <stdio.h> int main () { /* local variable definition */ int a = 100; int b = 200; /* check the boolean condition */ if( a == 100 ) { /* if condition is true then check the following */ if( b == 200 ) { /* if condition is true then print the following */ printf("Value of a is 100 and b is 200n" ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); return 0; }
switch statement Syntax: switch(expression){ case constant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); } switch ( integer expression ) { case constant 1 : do this ; case constant 2 : do this ; case constant 3 : do this ; default : do this ; } 5
Flow diagram
Example #include <stdio.h> int main () { /* local variable definition */ char grade = 'B'; switch(grade) { case 'A' : printf("Excellent!n" ); break; case 'B' : case 'C' : printf("Well donen" ); break; case 'D' : printf("You passedn" ); break; case 'F' : printf("Better try againn" ); break; default : printf("Invalid graden" ); } printf("Your grade is %cn", grade ); return 0; }
nested switch statements Syntax: switch(ch1) { case 'A': printf("This A is part of outer switch" ); switch(ch2) { case 'A': printf("This A is part of inner switch" ); break; case 'B': /* case code */ } break; case 'B': /* case code */ } 6
Example #include <stdio.h> int main () { /* local variable definition */ int a = 100; int b = 200; switch(a) { case 100: printf("This is part of outer switchn", a ); switch(b) { case 200: printf("This is part of inner switchn", a ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); return 0; }

C Programming Language Step by Step Part 5

  • 1.
  • 2.
    Operators Precedence inC Category Operator Associativity Postfix () [] -> . ++ - - Left to right Unary + - ! ~ ++ - - (type)* & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left Comma , Left to right
  • 3.
    #include<stdio.h> void main() { int x; x=-3+4*5-6;printf("%d n",x); x=3+(4%5)-6; printf("%d n",x); x=-3*4%-6/5; printf("%d n",x); x=(7+6)%5/2; printf("%d n",x); } 11 1 0 1 Program Output
  • 4.
    #include<stdio.h> void main() { int x=2,y,z; x*=3+2;printf("%d n",x); x*=y=z=4; printf("%d n",x); x=y==z; printf("%d n",x); x==(y=z); printf("%d n",x); } 10 40 1 1 Program Output
  • 5.
    #include<stdio.h> void main() { int x,y,z; x=2;y=1; z=9; x= x&&y||z; printf("%d n",x); printf("%d n",x||!y&&z); x=y=1; z=x++ -1; printf("%d n",x); printf("%d n",z); z+=-x ++ + ++y ; printf("%d n",x); printf("%d n",z); } Program Output 1 1 2 0 3 0
  • 6.
    Decision Making Statement Description ifstatement An if statement consists of a boolean expression followed by one or more statements. if...else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. nested if statements You can use one if or else if statement inside another if orelse if statement(s). switch statement A switch statement allows a variable to be tested for equality against a list of values. nested switch statements You can use one switch statement inside another switchstatement(s).
  • 7.
    if #include <stdio.h> int main() { /* local variable definition */ int a = 10; /* check the boolean condition using if statement */ if( a < 20 ) { /* if condition is true then print the following */ printf("a is less than 20n" ); } printf("value of a is : %dn", a); return 0; } Flow Diagram: 1
  • 8.
    if...else statement Syntax: if(boolean_expression) { /* statement(s)will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ } Flow Diagram: 2
  • 9.
    #include <stdio.h> int main() { /* local variable definition */ int a = 100; /* check the boolean condition */ if( a < 20 ) { /* if condition is true then print the following */ printf("a is less than 20n" ); } else { /* if condition is false then print the following */ printf("a is not less than 20n" ); } printf("value of a is : %dn", a); return 0; }
  • 10.
    if...else if...else Statement Syntax: if(boolean_expression1) { /* Executes when the boolean expression 1 is true */ } else if( boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } else if( boolean_expression 3) { /* Executes when the boolean expression 3 is true */ } else { /* executes when the none of the above condition is true */ } 3
  • 11.
    Example #include <stdio.h> intmain () { /* local variable definition */ int a = 100; /* check the boolean condition */ if( a == 10 ) { /* if condition is true then print the following */ printf("Value of a is 10n" ); } else if( a == 20 ) { /* if else if condition is true */ printf("Value of a is 20n" ); } else if( a == 30 ) { /* if else if condition is true */ printf("Value of a is 30n" ); } else { /* if none of the conditions is true */ printf("None of the values is matchingn" ); } printf("Exact value of a is: %dn", a ); return 0; }
  • 12.
    nested if statements4 Syntax: if(boolean_expression 1) { /* Executes when the boolean expression 1 is true */ if(boolean_expression 2) { /* Executes when the boolean expression 2 is true */ } }
  • 13.
    Example #include <stdio.h> int main() { /* local variable definition */ int a = 100; int b = 200; /* check the boolean condition */ if( a == 100 ) { /* if condition is true then check the following */ if( b == 200 ) { /* if condition is true then print the following */ printf("Value of a is 100 and b is 200n" ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); return 0; }
  • 14.
    switch statement Syntax: switch(expression){ caseconstant-expression : statement(s); break; /* optional */ case constant-expression : statement(s); break; /* optional */ /* you can have any number of case statements */ default : /* Optional */ statement(s); } switch ( integer expression ) { case constant 1 : do this ; case constant 2 : do this ; case constant 3 : do this ; default : do this ; } 5
  • 15.
  • 16.
    Example #include <stdio.h> int main() { /* local variable definition */ char grade = 'B'; switch(grade) { case 'A' : printf("Excellent!n" ); break; case 'B' : case 'C' : printf("Well donen" ); break; case 'D' : printf("You passedn" ); break; case 'F' : printf("Better try againn" ); break; default : printf("Invalid graden" ); } printf("Your grade is %cn", grade ); return 0; }
  • 17.
    nested switch statements Syntax: switch(ch1){ case 'A': printf("This A is part of outer switch" ); switch(ch2) { case 'A': printf("This A is part of inner switch" ); break; case 'B': /* case code */ } break; case 'B': /* case code */ } 6
  • 18.
    Example #include <stdio.h> int main() { /* local variable definition */ int a = 100; int b = 200; switch(a) { case 100: printf("This is part of outer switchn", a ); switch(b) { case 200: printf("This is part of inner switchn", a ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); return 0; }