c Control Structures in ‘C’ Language


Control Structures in ‘C’ Language
Realtime Programming Requirements:
Any Real Time Programming Consists of VALIDATION CONTROL and business LOGIC  Implementation. This actually makes the programming Logic to be identified as Procedural in Nature. This kind of programming makes the programmer to add intelligence to programs, and make the programs to be under the control of the programmers ideologies. The different types of constructs that come into the picture are…
Branching.
Selection .
Looping.
Branching Principle:
In this principle we shift the control of the program associated to the following
steps
1. Carryout a LOGICAL TEST at a Point inside the Program.
2. Carryout the POSSIBLE ACTIONS, depending on the OUTCOME of
LOGICAL TEST.
The number of Logical Tests has no Limitation and depending on the situation
can be Nested or can be Connected.
The Conditions can be compulsive in nature or they can be alternatives as per
the application necessity.
Selection Principle:
It is a special way of branching, in which one GROUP of STATEMENTS
are SELECTED from SEVERAL AVAILABLE GROUPS.
The GROUP SELECTION depends on the CONSTANT that is Applied
through the Process.
The CONSTANT that Applies can be EXPLICITLY supplied or IMPLICITLY
Supplied.
Looping Principle:
In this a GROUP of INSTRUCTIONS get EXECUTED REPEATEDLY until
some LOGICAL CONDITION has been satisfied.
This Principle is the BASIC POINT of Reusability in Programming
Standards. Much of the Coming topics in our Course depend on the
Experience what you gain from this Topic.
Control Statement Requirements:
Any Control Statement basically requires the Preparation of logical expressions. All the Logical Expressions in ‘C’ Language are applied using different operators. The Operators that are applied can be either BINARY or UNARY in Nature. The Different types of operators are
Relational or Comparision Operators :
Greater Than →>
Lesser Than →<
Greater Than or Equal To →>=
Lesser Than or Equal To →<=
Equality or Not Equality Operators :
Equality Operator →==
Not Equality Operator →!= or <>
Logical Connectivities:
Logical AND Operator →&& Ex: (x > 100) && (y == ‘c’)
Logical OR Operator →|| Ex: (x < 100) || (y == ‘c’)
Negation:
Unary Negation Operator →! Ex: !(x > 9 && y != 20)
Branching Principle in Detail
The Principle as per the real time requirements comes in different standards,
hence let us understand all the different approaches that come into affect and
understand the situational Logic.
Form I Logic
if(condition or conditions)
Statement 1;
Statement2;
Statement3;
In this case when the condition is TRUE, all statements execute
sequentially from Statement 1 to Statement 3.
But when the condition is FALSE, the Statement 2 and Statement 3 are
Executed.
In this concept actually Statement 2 and Statement 3 are under the
control of the Block or Module under which the ‘if’ Statement is
implemented. But because we are not controlling the hierarchy of the
conditional state using the compound statement Logic it can confuse us
if not tracked properly.
The Statement 1 is the only statement that is under the control of the
‘if’ Condition.
False State
True State


Illustrative Program
Program
# include < stdio.h>
void main ()
{
int x;
printf ( “\n Enter value for x: ”);
scanf (“%d”, &x);
if ( x >= 10)
printf (“\n x = %d is greater than 10” , x);
printf (“\n We are in True Block”);
}
Form II Logic
if(condition or conditions)
{
Statement 1;
Statement2;
Statement3;
}
Illustrative Program
Program
#include <stdio.h>
void main()
{
int x = 0;
printf(“\n Enter value for x : ” );
scanf( “%d”, &x);
if (x >= 0)
{
printf (“\n x = %d is greater than 10.” , x);
printf (“\n We are in True Block”);
}
printf (“We are in the main Block Control”);
}
Statement Under the
Control of the main()
module.
Statement Under
the Control of the
‘if’ Condition.
True State
False State
Area Under the Control of the main()
Module or any other Block.


Form III Logic
if (condition or conditions)
{
Statement 1; /*True Block*/
}
else
{
Statement 1; /*False Block*/
}
In this case, the TRUE BLOCK gets executed when the condition is
TRUE.
The FALSE BLOCK gets executed when the condition is FALSE.
Illustrative Program
Program
# include <stdio.h>
void main()
{
int x = 0;
printf (“\nEnter value for x : ” );
scanf ( “%d”, &x);
if (x >= 0)
{
printf (“\n x = %d is greater than 10” , x);
printf (“\nWe are in True Block”);
}
else
{
printf (“\n x = %d is not greater than 10” , x);
printf (“\nWe are in False Block”);
}
}
True State
False State


Sample Programs
Calculating Conditional Taxes
Program
# include <stdio.h>
void main()
{
float taxable, taxes;
printf ("\nEnter the income: ");
scanf ("%f", &taxable);
if (taxable <= 20000.00)
taxes = 0.02 * taxable;
else
taxes = 0.025 * (taxable - 20000.00) + 400.00;
printf ("\n\nThe Taxes are %0.2f ", taxes);
}



Finding the EVEN Number or ODD Number
Program
#include<stdio.h>
void main()
{
int num = 0;
printf("\nEnter a number: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("\nThe number %d is Even ", num);
else
printf("\nThe number %d is Odd ", num);
}

Temperature Conversion
Program
#include <stdio.h>
void main ()
{
char temp_type;
float temp, Fahren, Celsius;
printf("\nEnter temperature : ");
scanf ("%f", &temp);
printf("\nEnter Conversion type f or c : ");
fflush(stdin);
scanf ("%c", &temp_type);
if (temp_type == 'c')
{
Fahren = (5.0/9.0) * (temp - 32.00);
printf ("\nThe Equivalent Celsius = %f.", Fahren);
}
else
{
Celsius = ( 9.0 * temp) / 5.0 + 32;
printf ("\nThe Equivalent Fahrenheit = %f", Celsius);
}
}


Program to find the Greatest of the given two numbers
Program
#include <stdio.h>
void main()
{
int Num1, Num2;
printf("\nEnter the Numbers : ");
scanf("%d%d", &Num1, &Num2);
if(Num1 > Num2)
{
printf("\n%d is Greater than %d", Num1, Num2);
}
else
{
printf("\n%d is greater than %d", Num2, Num1);
}
printf("\nDid you watch any Bugs.");
printf("\n Concentrate");
}
Program to find the Greater or Equal of the given two numbers
Program
#include <stdio.h>
void main()
{
int Num1, Num2;
printf("\nEnter the numbers : ");
scanf("%d%d", &Num1, &Num2);
if(Num1 > Num2)
{
printf("\n%d is Greater than %d", Num1, Num2);
}
else
{
if(Num2 > Num1)
{
printf("\n%d is Greater than %d", Num2, Num1);
}
else
{
printf("\n%d is equal to %d", Num1, Num2);
}
}
}
Working with Characters in Conditionals
Characters are very Special Cases to handle in Programming Languages, as any
thing or every thing that is entered through the standard Input Console i.e. Key
Board is treated as a Character Data Type.
Whenever Character data has to be stored, ‘C’ uses ASCII(American
Standard Code for Information Interchange) equivalent of the character set.
When storing the character data, the character value is converted into
ASCII equivalent Number.
While retrieving the data, to print the character, the character equivalent of
the ASCII Number that is stored is printed.
STORING : Characters →ASCII(NUMBER)
RETRIEVING : Number(ASCII) →Character
Program
#include <stdio.h>
void main()
{
char ch = 'a';
printf("\nThe ASCII Value of Character %c is %d", ch, ch);
printf("\nEnter any character: ");
scanf("%c", &ch);
printf("\nThe ASCII value of Character %c is %d", ch, ch);
}

No comments: