Control Structure - Programming in C
Programming in C – Control Structure
Control Structure = A statement which is used to control the flow of execution in a program.
The control structure in C programming deals with different two statements given below: -
1. If-Statement
2. If-else Statement
The Control Loop structure in C programming deals with different three statements given below: -
1. While Loop
2. Do-While Loop
3. For Loop
The explanation with example and syntax of these statements is given below: -
1. If-Statement: - In If-statement only when the condition is true than the statement is executed.
Syntax for If-Statement: -
If (condition)
Statement;
For example: - if (num > 12)
{
Printf (number is Greater );
}
This is the simple example of If-statement where if number is greater than 12 than the statement will be executes as “ number is greater”. Otherwise, the syntax error will occur.
2. If-else Statement: - In If-else statement if condition is true than statement is executed otherwise, other statement will be executed.
Syntax for If-else Statement: -
If (condition)
Statement 1;
Else {
Statement 2;
Statement 3;
}
For Example: - if ( 20 > 50)
{
Printf(“20 is Smaller than 50“);
}
Else
{
Printf(“20 is Greater than 50”);
}
Output: - “ 20 is smaller than 50 ”.
In the above example, the number 20 is not larger than 50. So, the output would be “20 is smaller than 50”. In case if instead of 20 there another number like “60” than the output would be “60 (number) is Greater than 50”.
Loop Control Structure: - Loop control structure in c programming contain three loop statements. The loop control statements with example and syntax is given below: -
1) While Loop: - In While loop is the simplest and easy to implement in C programming. While loop is similar to the if-statement. When statement is executed in while loop than the condition is rechecked again.
Syntax for while-loop: -
While(condition)
{
Statement;
}
For Example: -
Int i=1;
While (i <10)
{
Printf(“ %d” , i);
i = I + 1;
}
Output = 1, 2, 3, 4, 5, 6, 7, 8, 9.
In the above statement , the values less than 10 are executed. The initial value start with (i = 1).
2) Do-While LOOP: -In do-while loop the conditions are checked at the end of the loop rather than starting of the loop. Do while loop is easy to use and execute.
Syntax for Do-while Loop: -
Do {
Statement;
}
While (expression);
For Example: - int i;
i = 1;
do {
printf(“%d”, i);
i = i + 1;
}
While (i < 10);
}
Output: - 1, 2, 3, 4, 5, 6, 7, 8, 9.
The output of this example is the digits less than 10. The initial value is 1. According to the expression ( i = i + 1), 1 is added in the each new value of i. Like this (0 + 1 = 0), ( 1 + 1 = 2 ), ( 2 + 1 = 3 ) … ( 8 + 1 = 9 ) .
3) For Loop: - For loop is the most popular loop and easy to execute. For loop is frequently used in the various programs and software’s. For loop is similar to while loop.
Syntax: - for ( expression1; expression2; expression3)
{
Statement blocks;
}
For Example: - int i ;
For (i =1;i < 11; i = i++)
{
Printf(“ %d “, i);
}
Output = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
In the above example of for loop, the values less than 11 are printed as output when condition is true for ( i < 11 ). The statement execution stops when the condition being false like when value if i become greater than number 11. That’s why 1 to 10 values are printed as output.
No comments