Sunday, 21 January 2018

DECISION MAKING AND BRANCHING | Operators in C Program Language | simple c programs with output


DECISION MAKING AND BRANCHING

DECISION MAKING AND BRANCHING
Introduction:
             C Language must be able to perform different sets of actions depending on the circumstances. A C program is a set of statements which are normally executed sequentially n the order in which they appear. This happens when no options or no repetitions of certain calculations are necessary.
However,in practice, we have a number of situations where we may have to change the order of execution of statements based on certain conditions,repeat a group of statements until certain specified conditions are met. This involves a kind of decision making to see whether a particular condition has occurred or not and then direct the computer to execute certain
statements accordingly.



   C has 4 decision making instructions, They are:
  1. If -Else Statement
  2. Switch Statement
  3. Conditional Operator Statement
  4. Goto Statement.



The IF Statement:
                       The IF Statement is a powerful decision making statement and is used to control the flow of execution of statements.

The General form of IF statement is look like this:

             if(This condition is true)

                 execute this statement;

The keyword 'IF' tells the compiler that what follows is a decision control instruction.The condition following the keyword 'If' is always enclosed within a pair of Parenthesis.
     
        It allows the computer to evaluate the expression first and then depending on whether the value of the expression (relation or condition) is 'TRUE' (non-zero) or FALSE (zero),it transfers the control to a particular statement. This point of program has two paths to follow one for the TRUE condition and other is for FALSE condition.

       As a general rule, we express a condition using 'C' relational operators, the following table shows how they are evaluated:

Hierarchy of operators:
           Operator                       Type
 
             !                        Logical NOT     

           *,/,%                  Arithmetic and Modulus

           +, -                       Arithmetic

          <>,<=,>=                    Relational         

           ==,!=                     Relational

            &&                        Logical AND

            ||                        Logical OR

             =                        Assignment


Demonstration of "IF" statement
Example:1

                main()
  {
    int num;
    printf("Enter a number less than 10");
    scanf("%d",&num);
    if(num <= 10)
    printf(" How nice you are");
  }

Description:
     On execution of this program, if you type a number less
than or equal to 10, you get a message on the screen through
printf(). If you type some other number the program doesn't
do anything.

Related Posts:

  • Case Control Structure(SWITCH) Case Control Structure(SWITCH) Introduction:           In real life we are often faced with situations where we are required to make a choice between a number of  alternatives rather than on… Read More
  • Multiple Statements With In If Multiple Statements With In If It may so happen that in program we want more than one statement to be execute if the expression following IF is satisfied.If such multiple statements are to be execute then they must be pl… Read More
  • Use Of Logical Operators Use Of Logical Operators  C allows usage of three logical operators, namely && ,|| and ! These are to be read as 'AND' ,' OR' and 'NOT' respectively.&&, || allows two or more conditions to be combine… Read More
  • Nested - IF Nested - IF    Example:7       /*Quick Demo of NESTED if-else*/  main()  {   int i;   printf("Enter either 1 or 2");   scanf("%d",&I);   if(i==1)   print… Read More
  • The NOT(!) Operators The NOT(!) Operators So far we have used only the logical operators && and ||.Third operator is the Not operator, written as !. This operator reverses the result of the expression it operates on.For example,if th… Read More

0 comments:

Post a Comment