C language programs are executed in a sequence, but we can control the execution of C program by using any control mechanism by which we can compare things and come to a decision. This involves using some operations called Relational Operators, conditional statements called if-else and loops. We have fundamental operators to compare two values.

These relational operators compare two values and return true or false after comparison. We can use the relational operators to compare values of any basic data type, so all we need is to modify the behavior of the program.

“The relational operators compare two values of any basic data type and return true or false after comparison.”

So what are the examples of Conditional Operators?

  1. < Less than
  2. > Greater than
  3. == Equal to
  4. <= Less than or equal to
  5. >= Greater than or equal to
  6. != Not equal to

The decision control structure in C can be implemented using

  1. The if statement
  2. The if – else statement
  3. The conditional operators

The if statement by itself will execute a single statement or a group of statements when the condition following if is true

If the condition is false then a group of statements can be executed using else statement

The simple example of an if statement is:

We can also use the code block to specify the statements to be pre-executed if the given condition is true i.e.

These are three methods by way of which we can repeat a part of a program in C programming

  1. Using a for statement
  2. Using a while statement
  3. Using a do-while statement

The for statement is started with the keyword for . There are three expressions which appear with in a for statement.

  1. Initializing Expression
  2. Test Expression
  3. Increment Expression

It has the following standard form.

The while loop continues until the specified condition is true. The general form of while loop is

The do-while loop is similar to the while loop in that the loop continues as long as the specified loop condition remains true. The main difference is that the condition is checked at the end of the do-while statement. So do-while loop is always executed at least once. Its general form is

Loops & Decision Control Structure

The decision control structure in C can be implemented in C using

  1. The if statement
  2. The if – else statement
  3. The conditional operators

The if Statement

The general form of if statement looks like this:

Here 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 parentheses. If the condition, whatever it is true, then the statement is executed. It the condition is not true then the statement is not executed instead the program skips past it. The condition in C is evaluated using C?s relational operators. The relational operators help us to build expression which are either true or false

for e.q

This expression is true if

Demonstration of if statement in C

Multiple statements within if statement

If it is desired that more than one statement is to be executed if the condition following if is satisfied, then such statements must be placed within pair of braces. for e.g.

The following program demonstrate that if year of service greater than 3 then a bonus of Rs. 2500 is given to employee. The program illustrates the multiple statements used within if

If – else statement

The if statement by itself will execute a single statement or a group of statements when the condition following if is true. it does nothing when the condition is false. It the condition is false then a group of statements can be executed using else statement. The following program illustrates this

Nested if – else statement

It we write an entire if – else construct within the body of the if statement or the body of an else statement. This is called ?nesting? of if. For e.g.

The Loop Control Structure in C programming

These are three methods by way of which we can repeat a part of a program. They are:

  1. Using a for statement
  2. Using a while statement
  3. Using a do-while statement

The While Loop

The general from of while is as shown below:

initialize loop counter;

The parentheses after the while contains a condition so long as this condition remains true all statements within the body of the while loop keep getting executed repeatedly for e.g.

The do-while Loop

The do-while loop takes the following form

There is a minor difference between the working of while and do-while loops. The difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this the do-while tests the condition after having executed the statements within the loop. for e.g.

In the above e.q. the printf will not get executed at all since the condition
fails at the first time itself. Now let?s now write the same program using a
do-while loop.

In the above program the printf( ) would be executed once, since first the body of the loop us executed and then the condition is tested.

The for Loop

The general form of for statement is as under:

Now let us write the simple interest problem using the for loop

The Break Statement in C

The keyword break allows us to jump out of a loop instantly without waiting to get back to the conditional test. When the keyword break is encountered inside any C loop, control automati-cally passes to the first statement after the loop. For e.g. the following program is to determine whether a number is prime or not.

Logic: To test a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. It the remainder of any of the divisions is zero, the number is not a prime. Following program implements this logic

The Continue Statement

The keyword continue allows us to take the control to the beginning of the loop bypassing the statements inside the loop which have not yet been executed. When the keyword continue is encountered inside any C loop control automatically passes to the beginning of the loop. for e.g.

The output of the above program would be….

12

21

when the value of i equal to that of j, the continue statement takes the control to the for loop (inner) bypassing rest of the statements pending execution in the for loop (inner).

The Case Control Structure

The Switch Statement in C

The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based upon the current value of an expression that is included within the switch statement.

The form of switch statement is.

Here are some of the rules which apply on switch statement in C language.

  • The expression used in a switch statement should have an integral or enumerated type.
  • There can be multiple case statement with in switch statement where case is followed by the value to be compared to and a colon.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • If there is no break statement appears within a case statement, the flow of the program continues until  break statement is is reached or the switch statement end block reaches.
  • A switch statement can have an optional default that can be used to execute some statements when no case statements are met before. As default is the last statement in switch so no break; statement is required to terminate this.
2 Shares
Share2
+1
Tweet
Share
Stumble