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.
- Program to check given number is odd or even
- Program to check a number is positive negative or zero
- Program to finding the roots of quadratic equation
- Program for updating the time by one second
- Program for largest and second largest of three numbers
- Program for doing arithmetic operation on two integers based on an operator
- Program to illustrate goto statement for looping
- Click here for more Programs………..
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");
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.
Syntax: switch(expression) { case value 1: Statements; break; case value 2: Statements; break; ...... ...... case value n: Statements; break; default: Statements; }
Syntax: break;
Sytanx: goto labelname;
....... ....... xyz; ....... ....... goto xyz; .......
....... ....... goto xyz; ....... ....... xyz: ....... In this case all the statements between goto and label xyz will be totally skipped when goto is used unconditionally.