Conditional statement in c
The primary types of control statements in C are:
·
Decision-making control statements
1. Simple if statement
2. If-else statements
3. Nested if-else statements
4. else-if ladder
·
Conditional statements
·
Goto statements in C
·
Loop control statements in C
1. While Loop
2. Do-while Loop
3. For Loop
1)
Simple-if statement
Here is the syntax for the if statement:
If (condition)
{
Statement;
}
2) If-else statement
If a condition is
satisfied, the code can be executed using the if-else expression. Based on the
situation, the code will execute either the if block or the else block.
Statement 1;
Else
Statement 2;
Inside the nested if-else lines, you'll find another if or else. Therefore, the condition of the "if" block (an outer if) must be true for the if block that includes an inner if to be performed. If the condition of the if block is true, then the statements within the if block are run; if not, the statements within the inner if's "else" block are executed.
Whenever the outer
"if" condition is not true, the "else" block of the outer
if, which is also an if, is performed. Depending on the condition, the
statement within the outer else's inner if is performed if true; otherwise, the
else block is executed.
{
// if a block of outer if’s
If (condition2)
{
statement 1;
}
Else
{
Statement 2;
}
}
// else block of outer if’s
Else
{
If(condition3)
{
Statement 3; //inner if
}
Else
{
Statement 4; //else
block of inner if
}
}
Flowchart
4)
Else if the ladder statement
The statements under each "if" in the else-if
ladder are executed if one of the two conditions is true; if not, the
statements under the else block are executed. The else-if ladder contains a
large number of else-if statements.
All statements are
executed based on the "if" condition. If one of the "if"
conditions is false, then the other "if" condition is checked. If
that one is true as well, then the statements based on that particular "if"
condition are executed. This process will run indefinitely provided the else-if
lines are there in the code.
The else-if ladder statement has the following syntax:
if (condition 1)
{
Statement 1;
}
else if (condition2)
{
Statement 2;
}
else lf (condition3)
{
Statement 3;
}
……………….
else if (condition n-l)
{
statement n-l;
}
Else
{
statement n;
}
5) Conditional control statements
Multi-way branching will be permitted by the switch statement depending on the expression's value for the switch.
Control is sent to that specific case label and the
statements under it are executed based on the expression. If the switch expression doesn't fit any of the
conditions, the default statement is performed.
Switch (expression)
{
Case label 1: statement 1;
break;
Case label 2: statement 2;
break;
Case label 3: statement 3;
break;
Case label n: statement;
break;
default: default statement;
}
Other statements;
Flowchart
Let us understand this by a program in C: -
Go to statement syntax is as follows: -
Syntax: got to label name;
Example: goto odd;
6.1) while loop
A while loop is sometimes called an entrance loop because the lines that make up its body are executed once the condition is fulfilled.
For the first time, if the while loop condition is false, the statements within will not be executed.
After this, you will find the while loop syntax: -
While(condition)
{
Statement 1 ;
Statement 2;
Statement n;
}
6.2) do-while loop
Another name for a do-while loop is an exit loop since it checks the condition second after executing the statements.
For every true condition in the while loop, the body will continue to execute
until it becomes false. If the condition is not true, the control will exit the
while loop and run the subsequent statements.
The syntax of do-while is as follows: -
Do
{
Statement 1;
Statement 2;
…………………
…………………
}
6.3) for loop
Pre-test loop is another
name for the for loop. Expression 1 is an initialization, Expression 2 is a
conditional expression, and Expression 3 is an update, according to the
grammar. Initializing the variables for the statement can be done.
The syntax is given below: -
For(expression 1;expression 2;expression 3)
{
Statement 1;
Statement 2;
……………
……………
}
Statement n;
Within the for loop, Expression1 is used to set the value of the variable. Expression 2 is then looked at to see whether the condition is true. The lines under Expression 3 are performed after the loop's body if the for-loop is executed. If the for loop's condition is true, the process will continue; if not, it will carry out the statements that follow the for loop.
Flowchart
No comments:
Post a Comment