2. C Supports Mainly Three Types Of Control Statements.
I. Decision making statements
1) Simple if Statement
2) if – else Statement
3) Nested if-else statement
4) else – if Ladder
5) switch statement
3. II. LOOP CONTROL STATEMENTS
1) for Loop
2) while Loop
3) do-while Loop
5. IF - Statement
It is one of the powerful conditional statement. If statement is responsible for modifying the
flow of execution of a program.
If statement is always used with a condition.
The condition is evaluated first before executing any statement inside the body of If. The
syntax for if statement is as follows:
8. How If Statement Works?
1. The if statement evaluates the test expression inside the parenthesis ().
2. If the test expression is evaluated to true, statements inside the body of if are executed.
3. If the test expression is evaluated to false, statements inside the body of if are not
executed.
10. IF- Else Statement
•It is observed that the if statement executes only when the condition following if is true.
•It does nothing when the condition is false.
•In if-else either True-Block or False – Block will be executed and not both.
•The “else” Statement cannot be used without “if”.
14. ELSE IF
The else..if statement is useful when you need to check multiple conditions within the program,
nesting of if-else blocks can be avoided using else..if statement.
The if-else-if ladder statement is an extension to the if-else statement.
It is used in the scenario where there are multiple cases to be performed for different conditions.
In if-else-if ladder statement, if a condition is true then the statements defined in the if block will
be executed, otherwise if some other condition is true then the statements defined in the else-if
block will be executed, at the last if none of the condition is true then the statements defined in
the else block will be executed.
15. SYNTAX
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
18. OUT PUT
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
19. NESTED ELSE IF
When an if else statement is present inside the body of another “if” or “else” then this
is called nested if else.
(or)
When a series of decision is required, nested if-else is used. Nesting means using one
if-else construct within another one
20. Syntax Of Nested If Else Statement:
if(condition)
{
//Nested if else inside the body of "if"
if(condition2)
{
//Statements inside the body of nested "if"
}
else
{
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
23. SWITCH
The switch statement in C is an alternate to if-else-if ladder statement which allows us
to execute multiple operations for the different possible values of a single variable
called switch variable.
Here, We can define various statements in the multiple cases for the different values
of a single variable.
25. RULES FOR SWITCH STATEMENT IN C LANGUAGE
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break
statement found in the case, all the cases will be executed present after the matched
case. It is known as fall through the state of C switch statement.
28. case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
29. OUT PUT
enter a number:4
number is not equal to 10, 50 or 100
30. EXAMPLE 2
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x>y && x+y>0)
{
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
}
}