Deicision Making and Branching

We have seen that programs are some set of statements written in a specific order. Processor will executed the programs in the order in which it was written. We may come to a situation where we need to alter the sequential execution of the statements to achieve some results. We may need to take some decisions based on the needs of the context for which the program was written and will branch or alter the execution of statements accordingly.  For example, if we are preparing a students results publishing program, then we may need to take decision on whether a student is passed or not.  If a student gets a specified minimum mark or more he is passed otherwise he is failed, this is a simple kind of decision making, we will implement this in C programs using the some decision making/branching control structures and conditions.

Conditions are tested for its truth values and if the condition is true then it will execute some statements, if the condition is false it will execute some other statements or will check another condition for its truth values.  C supports 4 different decision making statements and are commonly used in all other programming languages.

Decision making statements in C

  1. If statement
  2. Switch statement
  3. Conditional Operators
  4. Goto statement

Among the 4 decision making statements, If, Switch and Conditional operators are supported by most of the programming languages.  Goto statement is a branching statement and is not commonly used in many programming languages and in C itself usage of goto statement is not recommended.  Goto statements where the constructs used in the unstructured programming languages for branching and present languages like C, C++, Java etc. possesses structured feature and it follows Sequential, Conditional and Iteration statements in its programs.

Previous
Next

Special Operators and Operator Precedence in C

In the last posts we've discussed about C's all 7 type of operators, now we'll see which all are the Special operators in C.

Special Operators
Operator
Description
Example
,
Used to separate a pair of expressions. 
In this example two initialization expressions
are separated using a comma operator.
for(i=1,j=n;i++,j--)

.
Dot (.) operator is used in C to refer structure
members.
struct student s;
s.id=1021;
s.mark=86;
sizeof( )
Size of operator is used to compute the size of
any variable or any type. It will return the size in bytes.
sizeof(int);
char grade;
sizeof(grade);
->
Arrow operator is used to access structure
members using a structure pointer variable.
struct student p*;
p=&s;
p->id;

Notes
  • Comma operator is used to separate a pair of instructions, its commonly used in for loop to initialize or update more than one variable.  For example : for(i=0,j=n;i
  • If we defined a user defined structure data type, then its members are accessed its variable by a dot (.) operator.
  • sizeof( ) operator is a special operator, which will take a variable or a type as argument and will return the size of its argument in bytes.  For example, sizeof(int) will return 2. char ch; sizeof(ch) will return 1.
  • -> operator is used with structure pointers to access the structure members.

Operator Precedence and Associativity


When a combination of operators are used in an expression, then that mixed expression will be evaluated in the order of precedence of the operator used in that operation.  BODMAS theoram is applicable to the precedence  for arithmetic operators.  The following is the precedence of the C operators :

1.  ( )
2.  ++, --, !, sizeof( )
3.  /, %, *
4.  +, -
5.  <, <=, >, >=
6.  !=, ==
7.  &&
8.  ||
9.  ? :
10. All assignment and shorthand operators.
11. ,

If more than one same operator used in an expression then it will be evaluated in the order of its associativity rule.  !, ++, --, ? :, =,  +=, -=, *=, /= are evaluated from Right to left and rest of all operators discussed above are evaluated from Left to right.

Previous
Next

Design by The Blogger Templates The Blog Full of Games

© CourseZone
2008 - 2010 All Rights Reserved.