Conditional Statements

2341
conditional statements

We have studied some of the preliminary concepts in c programming, which are essential for writing simple programs involving simple arithmetic expressions. All the statements in a program are executed sequentially from statement 1 to statement n. Some times we need to change the control flow in a program in order to make certain decisions. Any structured languages like C provides statements that can alter the sequence in which the instructions are executed.

We have two types of control structures to change the flow of control namely Conditional Branching and Looping. The relational and logical operators are used to make conditional expressions. The result of conditional expressions is either true or false.

Relational Operators

,<,>=,<=,==and != are the six relational operators used in C language. These all are binary operators which take two operands and give the result as either true or false. Using these operators we can make relational conditions to check the operands as greater than, lesser than or equal to etc.

                Ex: a>b   x<y  a==b etc.

Logical Operators:

Now let us try to solve the more complex the conditional statements using logical operators. C provides three logical operators viz. &&(AND), | | (OR) and ! (NOT) operators, which take relational expressions or other logical expressions as arguments and give the result as either True or false. By the use of logical operators we can improve the program logic and which may reduce the number of program statements or number of operations.

The if statement:

if is a conditional statement, used to make decisions. It has number of formats. We will discuss the different formats of if statement one by one.

Simple if

         Syntax:
                if(conditional-expression)
                   statement(s);

The statements will be executed only when the conditional expression gives true, otherwise those statements under if () condition will not be executed.

In case of more than one statement if we want to execute depending on condition. then we need to make it compound statement by enclosing the statements within a pair of braces{}.

if……else statement

        Syntax:
               if(condition-expression)
                  statement1;
               else
                  statement2;

Here, when conditional expression evaluates to true, staments1 gets executed, otherwise statement2 gets executed. We can have more than one statement under if & else parts. In such case we need to make it a compound statement. The following program explains the advantage of using if…..else over if statement. It reduces one condition by introducing else. For example, We know when an integer is divided by 2 he remainder must be either 1 or 0. When it is not zero, no need to check for 1 because there is no alternative, it must be 1.

else if-ladder

When the number of alternatives are more than two, one can select one option from number of alternatives by using number of else of constructs.

        Syntax:
               if(cond-1)
               statements(s);
               else
               if(cond-2)
               statements(s);
               else
               if(cond-3)
               statements(s);
               .......
               .......
       Ex: Consider the program segment for printing the last digit of a number(n) 
           in words.
               if(n%10==0)
               print("zero")
               else
               if(n%10==1)
               printf("one")
               ..........
               ..........
               if(n%10==8)
               printf("eight")
               else
               printf("nine");
Nested if statement
While solving complicated programs we need to check number of conditions, one within another. IN such cases it is essential to use nested if conditions.
        Syntax:
               if(conditional-expression)
               {
               Statements;
               if(conditional-expression)
               {
               Statements(s);
                     if(conditional-expression)
                     Statements(s);
                     else
                     Statements;
               }
               else
               Statements(s);
               }
               else
               Statements(s);
In case of nested conditions the inner condition only if outer condition is true.
There may be corresponding else statement otherwise statements. We can nest the 
conditions to any depth.
switch()…..case statement
It is an alternative statement for else…….if constructs. This statement makes program easily understandable and more readable. It is used to select one option from number of alternatives.
               Syntax:
                      switch(expression)
                      {
                       case value 1: Statements;
                                     break;
                       case value 2: Statements;
                                     break;
                       ......
                       ......
                       case value n: Statements;
                                     break;
                       default: Statements;
                       }
Expression can be an arithmetic expression or simply a constant. Depending on the value of expression the control goes through switch construct for searching that particular case value and executes statement block of that case, and the ‘break’, statement at the end of the statement block in that case transfers the control to the end of the switch statement, if there is no match then the default case is executed and the switch statement terminates. However ‘default’ is optional and need not necessarily be included in the switch construct.
The ‘break;’ statement
This statement is used to break the compound or looping statement. It is mainly used in loops and switch()……case statement.
                   Syntax: 
                          break;
goto statement
goto is the unconditional looping statement. But it is used with conditional statements to do repeated operations. On executing the goto statement takes the program control to the specified label mentioned.
                  Sytanx:
                         goto labelname;
where labelname is any user defied word and must be mentioned in the program line ended with colon (:). Using goto statement without conditions leads to infinite loop or may skips certain statements. Consider the following program segment.
                        .......
                        .......
                        xyz;
                        .......
                        .......
                        goto xyz;
                        .......
Here xyz is a label. On execution the goto statement transfers the control of the program execution to that label. When the execution of goto statement in the above program segment without condition definitely leads to infinite loop. Now consider the program segment given below.
                        .......
                        .......
                        goto xyz;
                        .......
                        .......
                        xyz:
                        .......
In this case all the statements between goto and label xyz will be totally skipped 
when goto is used unconditionally.
conditional statements in c language Conditional Statements conditional statements in c program conditional statements in c examples all conditional statements in c control and conditional statements in c conditional and branching statements in c types of conditional statements in c example of conditional statement in c conditional statements concepts in c conditional statement control in c types of conditional control statements in c list of conditional control statement in c different conditional statements in c define conditional statements in c conditional decision making statements in c conditional statements in c programming example if statement conditional formatting chow conditional statements work in c conditional if statement in c conditional statements in c with examples conditional statements meaning in c conditional statements in c operators conditional statements structure in c conditional statements used in c various conditional statements in c
switch case in c switch case break statement in c break statement goto statement goto statement in c Conditional Statements estuduhelper Conditional Statements coding.booksinhindi.com