Categories
How To Guides

Loops in C Language

A computer is a versatile device because it has got the ability to perform a set of the instructions repeatedly. This will involve some recurring portions of a program either a defined number of times or until a particular condition is satisfied. This recurring operation is completed through a loop control instruction.

There are three different methods which allows you to repeat a part of the program.

  • Using for statement
  • Using a while statement
  • Using a do-while statement

The while Loop

The while loop is suited for the cases where repetitive tasks are required. It is a control flow statemnt that lets the codes to be executed on recurring basis on the given Boolean condition. This loop consists of block of the code and a constion or expression. The condition is then evaluated and if the condition is true the code will execute till the condition becomes false.

The for Loop

The for Loop is the most popular instruction. This loop lets you to specify three things about a loop in a single line.

  • Setting a loop counter to an initial value.
  • Testing the counter of loop in order to determine whether the value has reached number of repitions desired.
  • Incresing the loop counter value every time the program segment within the loop is executed.

For statement’s genral form is as under:

{

do thus;

and this;

and this;

}

Categories
How To Guides

Control Instructions in C Language

Control instructions in C language enable the user to specify the order in which different instructions in a program has to be executed by the computer. We can say that control instructions determine the ‘flow of control’ in a program. Control Instructions in C language are of four types.

  • Sequence Control Instructions
  • Selection or Decision Control Instruction
  • Repetition or Loop Control Instructions
  • Case Control Instruction

Sequence Control Instructions

This type of instruction that ensures that the instructions are executed in the same order in which they appear in the program.

Selection or Decision Control Instruction

This type of instructions lets the computer to take a decision to which the instruction is to be executed next.

Repetition or Loop Control Instruction

Loop Control Instruction helps computer to execute group of statements repeatedly (on loop).

Case Control Instruction

Like Decision Control instruction, Case Control instruction allows the PC to take a decision to which the excution needs to be executed next.

In C language a decision control instruction can be implemented

  • the if statement
  • The if-else statement
  • The conditional operators

The if Statement

Like most of the languages, C also uses the keyword if to implement decision control instruction. The general look of this statement is as under.

if ( this condition is true )

execute this statement ;

This statement tells the compiler that what follows is a decision control instruction. The condition which follows the if statement is always enclosed in parentheses. If the statement is true then it is executed and if the statement is not true then it will be executed.

The if-else Statement

This type of statement will execute if a specified condition is true. But if the condition is false another block of code will be executed. For that else statement is used.

Categories
How To Guides

Types of Instructions on C language

In C language there are basically three types of instructions which are as follows

  • Type Declaration Instruction
  • Arthimetic Instruction
  • Control Instruction

Type Declaration Instruction:

This instruction is used to declare the type of variables used in a C program. Any of the variable is used in the program needs to be declared before using it in any statement. The type declaration statement is started at the beginning of main() function.

There are some fine variations of the type declaration instruction which are as follows

While declaring the type of variable we can initialize it as shown below.

int i = 10, j = 25 ;

float a = 1.5, b = 1.99 + 2.4 * 1.44 ;

The order if variable definition is sometimes very important and sometimes not. e.g.

int i = 10, j = 25 ;

is similar to

int j = 25, j = 10 ;

However,

float a = 1.5, b = a + 3.1 ;

is alright, but

float b = a + 3.1, a = 1.5 ;

is not alright. The reasin behind this is that we are trying to use a even before defining it.

Arithmetic Instruction:

This type of instruction is used to perform arithmetic operations between constants and variables. An arithmetic instruction consists of variable name of the left side of = and variable names and constants on the right side of =. The variables as well as constants which are appearing on the right side of = are connected by arithmetic operators like +,-,* and /.

For example

int ad ;

float kot, deta, alpha, beta, gamma ;

ad = 3600 ;

kot = 0.0076 ;

deta = alpha * beta / gamma + 4.2 * 3 / 7 ;

Here

  • *, /, -, + are the arithmetic operators.
  • = is the assignment operator.
  • 3, 7 and 3600 are integer constants.
  • 4.2 and 0.0076 are real constants.
  • ad is an integer variable.
  • kot, deta, alpha, beta, gamma are real variables.

Control Instruction:

Control instruction is used to control the sequence of execution of various statements in the C program.