Unit 5 Control Statement
Unit 5 Control Statement
Unit 5 Control Statement
Control Statement
Theory:
5.1Selective Structures
5.2If, if else, if else ladder
5.3Switch and goto statement
5.4Repetitive Structure
5.5While loop
5.6 Do while loop
5.7For loop
5.8Nested loop
5.9Break and Continue statements
Practical:
1. Program about operator
2. Write program to use break and continue statement in For Loop, while Loop, Do-While
Loop
3. Write a program using switch statement
4. Program to display string message using For Loop
5. Program to display a pattern using nested For loop
6. Develop program to display 1 to 20 elements using For loop, While Loop, Do-While
7. Develop program to test even or odd number of an integer variable using if-else
statements
8. Program to display the largest number between any two numbers.
9. Program to display the smallest number between any two numbers.
10.Program to calculate the simple interest
11.Program to display the Fibonacci series
12.Program to display the series 1, 5,9,13 up to 13 terms.
===============================================================
C - Decision Making
Decision making structures require that the programmer specifies one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to be false.
Show below is the general form of a typical decision making structure found in most of
the programming languages −
C programming language assumes any non-zero and non-null values as true, and if it is
either zero or null, then it is assumed as false value.
Selective structures
Selective structures is used in C program to select appropriate output or input from a
group of input/output. The various selective structures are
If
If else
If else ladder
Switch
Goto
Label
C if else Statement
The if-else statement in C is used to perform the operations based on some specific
condition. The operations specified in if block are executed if and only if the given
condition is true.
There are the following variants of if statement in C language.
If statement
If-else statement
If else-if ladder
Nested if
If Statement
The if statement is used to check some given condition and perform some operations
depending upon the correctness of that condition. It is mostly used in the scenario
where we need to perform the different operations for the different conditions.
Example:
#include<stdio.h>
void main()
{
int number;
printf("Type a number:");
scanf("%d", &number);
if (number < 0) { // check whether the number is negative number.
number = -number; // If it is a negative then convert it into positive.
printf("The absolute value is %d\n", number);
}
}
Example of if statement
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
Output:
Variable x is less than y
Explanation: The condition (x<y) specified in the “if” returns true for the value of x and y, so
the statement inside the body of if is executed.
Example of multiple if statements
We can use multiple if statements to check more than one conditions.
#include <stdio.h>
int main()
{
int x, y;
printf("enter the value of x:");
scanf("%d", &x);
printf("enter the value of y:");
scanf("%d", &y);
if (x>y)
{
printf("x is greater than y\n");
}
if (x<y)
{
printf("x is less than y\n");
}
if (x==y)
{
printf("x is equal to y\n");
}
printf("End of Program");
return 0;
}
In the above example the output depends on the user input.
Output:
enter the value of x:20
enter the value of y:20
x is equal to y
End of Program
If-else Statement
The if-else statement is used to perform two operations for a single condition. The if-else
statement is an extension to the if statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and the other is for the
incorrectness of the condition. Here, we must notice that if and else block cannot be
executed simiulteneously. Using if-else statement is always preferable since it always
invokes an otherwise case with every if condition.
The syntax of the if-else statement is given below.
if(expression)
{
//code to be executed if condition is true
}
Else
{
//code to be executed if condition is false
}
Flowchart of the if-else statement in C
Let's see the simple example to check whether a number is even or odd using if-else
statement in C language.
#include<stdio.h>
int main()
{
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
Else
{
printf("%d is odd number",number);
}
return 0;
}
Output
enter a number:4
4 is even number
enter a number:5
5 is odd number
Program to check whether a person is eligible to vote or not.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
Output
Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote
If else-if ladder 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. There are multiple else-if blocks possible. It is
similar to the switch case statement where the default is executed instead of else block if
none of the cases is matched.
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
}
Flowchart:
C Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows us to
execute multiple operations for the different possibles 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.
The syntax of switch statement in c language is given below:
switch(expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
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.
Flowchart of switch statement in C
Functioning of switch case statement
First, the integer expression specified in the switch statement is evaluated. This value is
then matched one by one with the constant values given in the different cases. If a match is
found, then all the statements specified in that case are executed along with the all the
cases present after that case including the default statement. No two cases can have similar
values. If the matched case contains a break statement, then all the cases present after that
will be skipped, and the control comes out of the switch. Otherwise, all the cases following
the matched case will be executed.
Let's see a simple example of c language switch statement.
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
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; }
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
Switch case 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 ");
} }
Output
hi
C Switch statement is fall-through
In C language, the switch statement is fall through; it means if you don't use a break
statement in the switch case, all the cases after the matching case will be executed.
Let's try to understand the fall through state of switch statement by the example given
below.
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equal to 10\n");
case 50:
printf("number is equal to 50\n");
case 100:
printf("number is equal to 100\n");
default:
printf("number is not equal to 10, 50 or 100");
} return 0; }
Output
enter a number:10
number is equal to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
Output
enter a number:50
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
Exercises
Very short questions.
a. What is decision control structure?
b. When you use if statement in C?
c. Write the correct syntax of if statement.
d. What are the different selective statements in C?
e. When you have multiple conditions to check then which selective statements you
choose?
Short questions:
1. Write difference between if and if else statement in C.
2. Draw a flowchart to represent if else statement.
3. What is if else if statement? Demonstrate with its example.
4. Write a program to display your name when you are student of class 9.
5. Define if else ladder statement. Write the correct syntax of it.
Long questions:
6. What is switch statement in C? How switch case statement works in program?
Explain with an example.
7. Write a program to input any number and print month name using both if else ladder
statement and switch case statement.
8. Program to calculate the grade of the student according to the specified marks.