C Control Structures

Control Structures
In simple programs, instructions are executed in the order in which they appear (from top to bottom), each instruction being executed once and only once. However, in most cases, programs require decision making processes which incorporate either looping, selection or both. to achieve this, control statements are needed. There are five main control statements in C: 		 Selection statements if ... statement switch ... statement Looping statements while ... statement do ... while ... statement  for ... statement 

Selection Statements
These are also refferred to as branching statements. The statement to be executed is determined by the result of the condition (boolean expression) within it. They allow selection to be made on two possible outcomes. There are two types of selection statements:
 * 1) if ... statement.
 * 2) switch ... statement.

The if statement
The if ... statement contains several variations, which can be applied depending on how the program is to execute. These are: simple if, if .. else, nested if ...

if statement
It only has one alternative in that the statements within it are executed after the boolean expression is evaluated and it returns true. If the expression evaluated is true, the statements following if ... is executed, otherwise nothing happens. Example: //program to output even numbers #include<stdio.h>  int main {  int x;   printf("Enter a number\n"); scanf("%d",&x); if (x%2==0){ printf("%d ", x); }  }

if ... else ... statement
It is an extension of the if ... statement. The difference is that, this statement has an alternative (else) part. If the test expression (condition) is evaluated to be true, the statements that come immediately after the if... part are executed. Otherwise the statements that fall under else ... are executed. Example: /* program to illustrate the if ... else ... statement */ #include<stdio.h> void main {  int i;   printf("\n Enter a number:"); scanf("%d",&i); if ((i%4==0)&&(i%100==0)) {    printf("%d is divisible by 4 and 100.\n",i); }  else printf("%d is not divisible by 4 and 100.\n",i); }

Switch Statement
It is a branching statement that is used when there are many alternatives to choose from. Its execution is faster than the if ... statement. Syntax: switch (variable) {  case label1: ststement 1; break; case label2: statement 2; break; :                        :   case labeln: statement n;                         break; default: statement; break; } Example: /* Program to demonstrate the use of switch ... statement */ #include<stdio.h>  void main {    int selection; printf("List of destinations: \n"); printf("\t 1:      Mercury"); printf("\t 2:      Venus"); printf("\t 3:      Mars"); printf("Enter your destination from the list:->   "); scanf("%i",&selection); switch (selection) {    case 1: puts("Mercury is the closest to the sun"); puts("Its quite hot compared to the Earth"); puts("The journey will cost $10,000,000"); break; case 2: puts("Venus is the second planet clossest to the sun"); puts("Its quite hot and thw weather might be poisonous"); puts("The journey will cost $8,000,000"); break; case 3: puts("Mars is the closest planet to the Earth"); puts("Its quite good weather but not smooth"); puts("The journey will cost $5,000,000"); break; default: puts("Destination is into unknown world"); break;

Repetitive / Looping (Iterative) ststements
These are control structures whose execution is controlled by a condition (boolean expression).Their statements continuously execute until a specified is fulfilled. There are two types:
 * 1) Pre-condition: condition is tested before statements are executed e.g for .., and while ..
 * 2) Post-condition: condition is tested after the statetment has been executed e.g do ... while

While ... statement
It is applied when the number of times loop is to be executed is not known in advance. The body of the loop is executed repeatedly as long as the test expression is true. The loop terminates when the boolean expression evaluates to false. The body of the loop, is evaluated before executing any of the statements in the body of the loop. As a result, the body of the loop is not executed at all when the test expression is initially false. Example: /*Program to display numbers from 0 .. 9 repeatedly*/ #include<stdio.h> void main {  int digit = 0; while (digit <= 9) {    printf ("%d\n", digit); digit++; } }

Do ... while ... statement
The test expression (Boolean expression) appears at the end of the body of the loop. The minimum number of times that the loop can execute is once since the test for the repetition does not occur until the end of the first pass through the loop. Example: /*Program to display 0 ... 9 continuously */ #include<stdio.h> void main {   int digit = 0; do        printf("%d\n", digit); digit++; while(digit <=9);

for ... statement
This is the most commonly used looping statement in C. The statement header contains three parts; initialization initialization, test expression and update expression.

Initialization expression: Is used to initialize the control variable ( variable that controls the looping action);

Test expression: This is the condition that must be satisfied (return true) for the loop to continue execution. It normally is a Boolean expression.

Update expression: Is used to alter the value of the control variable by either incrementing or decrementing it (depending on how the loop is executing).

The body of the loop is continuously executed as long as the test expression (Boolean expression) is true.

Syntax: for(initialize variable = value; boolean/ test expression; increment variable;) {       statements; } Example: /* Program example for the for... statement */ int main { int i;  int sum = 0; sumofsquares = 0; for(i = 0; i <= 30; i++) {    if(i%2==0) {       sum+=i; sumofsquares += pow(i,2); } printf("Sum of even numbers between 0 and 30 = %d\n",sum); printf("Sum of their squares = %d\n",sumofsquares); return 0; //returns control to the calling function or to the operating system. }
 * 1) include<stdio.h>
 * 2) include<math.h>