Conditional Statements


We have studied some of the preliminary concepts in c programming, which are essential for writing simple programs involving simple arithmetic expressions in the previous chapter. 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. In this chapter we discuss conditional statements in detail and looping we will discuss in the next chapter. 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


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


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.

       Ex: Consider the program segment for printing the last digit of a number(n) 
           in words.
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


Please enter your comment!
Please enter your name here