2. Control Statements
• The control statements help a user to specify a program control’s
flow.
• They specifies the order of execution of the instructions present in a
program.
• These make it possible for the program to make certain decisions,
perform various tasks repeatedly, or even jump from any one section
of the code to a different section.
3. Types of Control Statements
•There are four types of control statements in C:
Decision making statements (if, if-else, nested-if)
Selection statements (switch-case)
Iteration statements (for, while, do-while)
Jump statements (break, continue, goto)
4. If statement (Decision making)
• If the condition given in If statement is true, then the statement
inside the If block is executed, otherwise the statement outside the If
block is executed.
• Syntax
if(expression){
//code to be executed
}
7. //Finding largest of three numbers
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}
Output
Enter three numbers?
12 23 34
34 is largest
8. If-else Statement (Decision making)
• If the condition given in If statement is true, then the statement
inside the If block is executed, otherwise the statement inside the
Else block is executed.
• Syntax
if(expression){
//code to be executed if condition is true
}
else{
//code to be executed if condition is false
}
10. //Finding odd or even number
#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
11. //Maximum of two numbers
#include<stdio.h>
void main(){
int a,b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
if(a>b)
printf(“%d is big”,a);
else
printf(“%d is big”,b);
}
Output:
Enter two numbers: 20 40
40 is big
12. //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
13. Nested if-else Statements (Decision making)
• The nested if-else statements consist of outer-if and inner-if.
• If the condition of outer-if is true then outer-if block is executed
which contains inner-if and if the condition of inner-if is true,
statements under inner-if block will be executed else the statements
of inner-if else block will be executed.
• If the outer-if condition is not true then the outer-if-else block is
executed.
16. //Maximum of three numbers
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if(num1 > num2)
{
if(num1 > num3)
{
printf(“%d is the maximum“, num1);
}
else
{
printf(“%d is the maximum“, num3);
}
}
else
{
if(num2 > num3)
{
printf(“%d is the maximum“, num2);
}
else
{
printf(“%d is the maximum“, num3);
}
}
return 0;
}
Output:
Enter three numbers: 20 34 56
56 is the maximum
17. The else-if ladder statement (Decision making)
• The if-else-if ladder statement is an extension to the if-else statement
and similar to switch-case statement.
• It is used in the scenario where there are multiple cases to be
performed for different conditions.
• 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.
20. //Students marks grading
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}
Output
Enter your marks?10
Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...
21. Switch Statement (Selection)
• The switch statement is an alternate to if-else-if ladder statement which allows us to
define various statements in the multiple cases for the different values of a single
variable.
• Syntax
switch(expression){
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......
default:
code to be executed if all cases are not matched;
}
22. Rules for switch statement
• The switch expression must be of an integer or character type.
• The case value must be an integer or character constant.
• The case value can be used only inside the switch statement.
• 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.
23. Let's try to understand it by the examples. We are
assuming that there are following variables.
int x,y,z;
char a,b;
float f;
Valid Switch Invalid Switch Valid Case Invalid Case
switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case 'a'; case x;
switch(a+b-2) case 1+2; case x+2;
switch(func(x,y)) case 'x'>'y'; case 1,2,3;
25. Example
#include <stdio.h>
int main()
{
int x = 2;
switch (x) {
case 1:
printf("Choice is 1");
break;
case 2:
printf("Choice is 2");
break;
case 3:
printf("Choice is 3");
break;
default:
printf("Choice other than 1, 2 and 3");
break;
}
}
Output:
Choice is 2
26. //Arithmetic operation using switch case
#include<stdio.h>
int main()
{
int a,b;
int op;
printf(" 1.Additionn 2.Subtractionn
3.Multiplicationn 4.Divisionn");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2:
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3:
printf("Multiplication of %d and %d is: %d”,a,b,a*b);
break;
case 4:
printf("Division of %d and %d is %d : "a,b,a/b);
break;
default:
printf(" Enter Correct Choice.");
break;
}
return 0;
}
Output:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 20 15
Enter your Choice : 1
Sum of 20 and 15 is : 35
27. Loops
• Repeating the same process multiple times until a specific condition is
satisfied is called looping.
• Advantages
It provides code reusability.
Using loops, we do not need to write the same code again and again.
• Types of C Loops
for
while
do-while
28. for loop
• The for loop is used in the case where we need to execute some part
of the code until the given condition is satisfied.
• It is better to use for loop if the number of iteration is known in
advance.
• Syntax
for ( initialization; condition; incr/decr ){
//code to be executed
}
30. Example for for loop
#include<stdio.h>
void main(){
int i=0;
for(i=1;i<=10;i++)
{
printf("%d n",i);
}
}
Output:
1
2
3
4
5
6
7
8
9
10
31. //Fibonacci series using for loop
#include<stdio.h>
void main()
{
int f1=-1,f2=1,f3,i,n;
printf("How many fibonacci
numbers you want: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
f3=f1+f2;
printf("%dn",f3);
f1=f2;
f2=f3;
}
}
Output:
How many fibonacci numbers you want: 7
0
1
1
2
3
5
8
32. //Finding factorial using for loop
#include<stdio.h>
void main()
{
int n,i,fact=1;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial of %d is %d",n,fact);
}
Output:
Enter a number: 5
The factorial of 5 is 120
34. While loop
• The while loop is to be used in the scenario where we don't know the
number of iterations in advance.
• The block of statements is executed in the while loop until the
condition is satisfied.
• Syntax
while(condition){
//code to be executed
}
36. Example for while loop
#include<stdio.h>
void main(){
int i=1;
while(i<=10){
printf("%d n",i);
i++;
}
}
Output:
1
2
3
4
5
6
7
8
9
10
37. //Finding sum of digits using while loop
#include<stdio.h>
void main()
{
int n,sum=0,d;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("The sum of the given digits is %d",sum);
}
Output:
Enter a number: 457
The sum of the given digits is 16
38. //Reversing the digits using while loop
#include<stdio.h>
void main()
{
int n,rev=0,d;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
printf("The reverse of the given digits is %d",rev);
}
Output:
Enter a number: 478
The reverse of the given digits is 874
39. //Finding whether a number is prime or not using while loop
#include <stdio.h>
void main()
{
int n, i, flag = 0;
printf("Enter a number: ");
scanf("%d",&n);
i=2;
while( i<n)
{
if(n%i==0)
{
flag=1;
break;
}
i++;
}
if (flag==0 && n!=1)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
}
Output:
Enter a number: 5
5 is a prime number
Enter a number: 4
4 is not a prime number
40. //Finding first n prime numbers using while loop
#include<stdio.h>
void main()
{
int i,n,p,count,flag;
printf("Enter the number: ") ;
scanf("%d",&n) ;
printf("First %d prime numbers aren”,n);
p=2;
i=1;
while(i<=n)
{
flag=1;
for(count=2;count<=p-1;count++)
{
if(p%count==0)
{
flag=0;
break;
}
}
if(flag==1)
{
printf("%dt",p) ;
i++;
}
p++;
}
}
Output:
Enter the number: 5
First 5 prime numbers are
2 3 5 7 11
42. do-while loop
• The do while loop is a post tested loop.
• Using the do-while loop, we can repeat the execution of several parts
of the statements.
• The do-while loop is mainly used in the case where we need to
execute the loop at least once.
• Syntax:
do{
//code to be executed
}while(condition);
44. Example for do-while loop
#include<stdio.h>
void main(){
int i=1;
do{
printf("%d n",i);
i++;
}while(i<=10);
}
Output:
1
2
3
4
5
6
7
8
9
10
45. Example for do-while loop
#include<stdio.h>
void main()
{
int i=1;
do{
printf(“Hello Dear”);
i++;
}while(i<0);
}
Output:
Hello Dear
In the above program, though the condition is not satisfied, the statement is
executed at least once.
48. Break Statement (Jump Statement)
• The break statement is used to bring the program control out of the
loop for the particular condition.
• break keyword is used
• The break statement is used inside loops or switch statement.
49. Example for break statement
#include<stdio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%dt",i);
if(i == 5)
break;
}
printf(“n came outside of loop");
}
Output
0 1 2 3 4 5
came outside of loop
50. Continue Statement (Jump Statement)
• The continue statement is used to skip the iteration of the loop one
time for the particular condition.
• continue keyword is used
51. Example for continue statement
#include<stdio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%dt",i);
if(i == 5)
continue;
}
printf(“n came outside of loop");
}
Output
0 1 2 3 4 6 7 8 9
came outside of loop
52. Goto Statement (Jump Statement)
• Goto statement is a jump statement that is used to jump from one
part of the code to any other part of the code.
• Syntax
53. Example for goto statement
#include<stdio.h>
void main()
{
printf("Good morningn");
goto point;
printf("How are you all?n");
printf("Had breakfast?n");
printf("Have a good dayn");
point:
printf("Today's Topic");
}
Output:
Good morning
Today’s Topic