You are on page 1of 25

DEVA Sir (NOTES)

SELECTIONS
Mallesham Devasane (Code: DEVA10)
www.Unacademy.com/@devagatecse
DEVA Sir (NOTES)

Selections

If statement:
Syntax:
if (condition)
{
//Statements if condition is true
}

The statements inside the body of “if” only execute if the given condition returns true.
DEVA Sir (NOTES)

Selections
Flow Diagram of if statement
DEVA Sir (NOTES)

Selections

Example of if statement:
#include <stdio.h>
int main()
{
int x = 20, y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
printf(“\nEnd of the program”);
return 0;
}

Output:
Variable x is less than y
End of the program
DEVA Sir (NOTES)

Selections

if else… statement:
Syntax:
if(condition) {
// Statements inside body of if
}
else {
//Statements inside body of else
}
• If condition returns true then the statements inside the body of “if” are executed
• If condition returns false then the statements inside the body of “else” are executed.
DEVA Sir (NOTES)

Selections

Flow diagram
DEVA Sir (NOTES)

Selections
Example:
#include <stdio.h>
int main()
{
int x=10, y=20;
if(x>y)
{
printf(“x is greater");
}
else
{
printf(“y is greater”);
}
printf(“\nEnd of the program”);
return 0;
}
Output:
y is greater
End of the program
DEVA Sir (NOTES)

Selections

nested if:
• It is used when multiple conditions need to be tested.

Syntax:
if(condition1)
{
// statements of outer if
if(condition2)
{
//statements of inner if;
}
}

The inner statement will execute only when outer if statement is true otherwise control won't even
reach inner if statement.
DEVA Sir (NOTES)

Selections

Flowchart:
DEVA Sir (NOTES)

Selections
Example:
#include<stdio.h>
int main(){
int num;
printf("Enter a number\n");
scanf("%d", &num);
/* Outer if statement */
if(num < 500){
printf("First Condition is true\n");
if(num > 100){
printf("First and Second conditions are true\n");
}
}
printf(“End of the nested if\n");

getch();
return(0);
}
DEVA Sir (NOTES)

Selections

Output:
Enter a numbers
80
First Condition is true
End of the nested if
DEVA Sir (NOTES)

Selections

If else Ladder:
• It is used to test set of conditions in sequence.

Syntax:
if(condition1) {
statement1;
} else if (condition2) {
statement2;
} else if (condition3) {
statement3;
} else {
statement4;
}

• Particular if condition is tested only when all previous if conditions in if-else ladder is false.
• If any of the conditional expression evaluates to true, then it will execute the corresponding
code block and exits whole if-else ladder.
DEVA Sir (NOTES)

Selections
Flow Chart:
DEVA Sir (NOTES)

Selections
Example:
#include<stdio.h>
#include<conio.h>

int main(){
int marks;
printf("Enter your marks between 0-100\n");
scanf("%d", &marks);
/* if else ladder statement to print Student Grade */
if(marks >= 90){
/* Marks between 90-100 */
printf("YOUR GRADE : A\n");
}
else if (marks >= 70 && marks < 90)
{
DEVA Sir (NOTES)

Selections
/* Marks between 70-89 */
printf("YOUR GRADE : B\n");
}
else if (marks >= 50 && marks < 70){
/* Marks between 50-69 */
printf("YOUR GRADE : C\n");
}
else {
/* Marks less than 50 */
printf("YOUR GRADE : Failed\n");
}
getch();
return(0);
}
Output:
Enter your marks
96
YOUR GRADE : A
DEVA Sir (NOTES)

Selections

Go-To Statement:
• It is unconditional jump statement
• The control jumps directly to the label mentioned in the goto statement.
• The goto statement must be used within the same function where label is present.

Syntax:
goto label_name;
..
..
label_name: C-statements
DEVA Sir (NOTES)

Selections
Flow Diagram:
DEVA Sir (NOTES)

Selections

Example:
#include <stdio.h>
int main()
{
int sum=0;
for(int i = 0; i<=10; i++){
sum = sum+i;
if(i==5)
{
goto addition;
}
}

addition:
printf("%d", sum);

return 0;
}
DEVA Sir (NOTES)

Selections

switch...case Statement:
• If you are checking on the value of a single variable in if...else...if, it is better to use switch
statement.
• The switch statement is often faster than nested if...else (not always). Also, the syntax of
switch statement is cleaner and easy to understand.
DEVA Sir (NOTES)

Selections

Syntax:
switch (n)
​{
case constant1:
// code to be executed if n is equal to constant1;
break;

case constant2:
// code to be executed if n is equal to constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any constant
}
DEVA Sir (NOTES)

Selections

Flowchart:
DEVA Sir (NOTES)

Selections

Example1:
# include <stdio.h>
int main()
{
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
break;
DEVA Sir (NOTES)

Selections
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
break;
// operator is doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
}
return 0;
}

Output: Enter an operator (+, -, *,): -


Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1
DEVA Sir (NOTES)

Selections
Example2:
int main()
{
int num=2;
switch(n+2)
{
case 1:
printf("Case1: Value is: %d", num);
case 2:
printf("Case2: Value is: %d", num);
case 3:
printf("Case3: Value is: %d", num);
default:
printf("Default Value is: %d", num);
}
return 0;
}
Output: Default: value is: 2
DEVA Sir (NOTES)

Selections

Limitations of switch:
• Logical operators cannot be used with switch statement.
• The expression used in switch must be integral type ( int, char and enum)
• All the statements following a matching case execute until a break statement is reached.
• The default block can be placed anywhere.
• Two case labels cannot have same value

You might also like