SlideShare a Scribd company logo
Unit - 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.
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)
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
}
Flowchart of if statement
Example-1
#include<stdio.h>
void main()
{
int a=5,b=2;
if(a>b)
{
printf(“a is big”);
}
printf(“Outer statement”);
}
Output:
a is big
Outer statement
//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
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
}
Flowchart of if-else statement
//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
//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
//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
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.
Syntax
if ( outer-if condition)
{
if ( inner-if condition)
{
Inner-if statements;
}
else
{
Inner-if-else statements;
}
else
{
outer-if-else statements;
}
Flowchart of else-if ladder statement
//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
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.
Syntax
if(condition1){
//statements;
}
else if(condition2){
//statements;
}
else if(condition3){
//statements;
}
else if(condition4){
//statements;
}
...
else{
//statements;
}
Flowchart of else-if ladder statement
//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 ...
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;
}
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.
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;
Flowchart for switch statement
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
//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
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
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
}
Flowchart of for loop
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
//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
//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
//Infinite for loop
#include<stdio.h>
int main ()
{
for(;;)
{
printf(“Hello Dearn");
}
}
Output:
Hello Dear
Hello Dear
Hello Dear
….
….
….
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
}
Flowchart of while loop
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
//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
//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
//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
//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
Infinite while loop
#include<stdio.h>
void main(){
while(1){
printf(“Hello Dearn");
}
}
Output:
Hello Dear
Hello Dear
……….
……..
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);
Flowchart for do-while loop
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
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.
Infinite do-while loop
#include<stdio.h>
void main(){
do{
printf(“Hello Dearn");
}while(True);
}
Output:
Hello Dear
Hello Dear
……….
……..
C Unit-2.ppt
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.
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
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
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
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
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

More Related Content

PDF
Control statements-Computer programming
PPTX
Branching statements
PPTX
unit 2-Control Structures.pptx
PPTX
Basics of Control Statement in C Languages
PPT
CONTROLSTRUCTURES.ppt
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
C programming Control Structure.pptx
PPTX
Decision Making and Branching
Control statements-Computer programming
Branching statements
unit 2-Control Structures.pptx
Basics of Control Statement in C Languages
CONTROLSTRUCTURES.ppt
Unit ii chapter 2 Decision making and Branching in C
C programming Control Structure.pptx
Decision Making and Branching

Similar to C Unit-2.ppt (20)

PPTX
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
PPTX
control_structures_c_language_regarding how to represent the loop in language...
PPTX
CONTROL FLOW in C.pptx
PDF
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
PPTX
Control Statements -if else in C programming.pptx
PDF
control statement
PPT
Decision making in C(2020-2021) statements
PPTX
Condition Stmt n Looping stmt.pptx
PPTX
computer programming Control Statements.pptx
PPTX
C Programming: Control Structure
PDF
POP Unit 2.pptx.pdf for your time and gauss with example
PPTX
Control structure of c
PPTX
Control Structures in C
PPTX
3. chapter ii
PPTX
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
PDF
exp227-jan-170127160848 (3) (1).pdf
PDF
C in 10 Hours learn programming easily.pdf.pdf
PPSX
Bsit1
PPTX
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
PPTX
Dti2143 chap 4 control structures aka_selection
M2 (1).pptxisedepofengiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
control_structures_c_language_regarding how to represent the loop in language...
CONTROL FLOW in C.pptx
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
Control Statements -if else in C programming.pptx
control statement
Decision making in C(2020-2021) statements
Condition Stmt n Looping stmt.pptx
computer programming Control Statements.pptx
C Programming: Control Structure
POP Unit 2.pptx.pdf for your time and gauss with example
Control structure of c
Control Structures in C
3. chapter ii
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
exp227-jan-170127160848 (3) (1).pdf
C in 10 Hours learn programming easily.pdf.pdf
Bsit1
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
Dti2143 chap 4 control structures aka_selection
Ad

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Computing-Curriculum for Schools in Ghana
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Structure & Organelles in detailed.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
RMMM.pdf make it easy to upload and study
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Institutional Correction lecture only . . .
Renaissance Architecture: A Journey from Faith to Humanism
FourierSeries-QuestionsWithAnswers(Part-A).pdf
GDM (1) (1).pptx small presentation for students
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
Sports Quiz easy sports quiz sports quiz
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
VCE English Exam - Section C Student Revision Booklet
Computing-Curriculum for Schools in Ghana
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
2.FourierTransform-ShortQuestionswithAnswers.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
01-Introduction-to-Information-Management.pdf
Insiders guide to clinical Medicine.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
RMMM.pdf make it easy to upload and study
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Institutional Correction lecture only . . .
Ad

C Unit-2.ppt

  • 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 }
  • 5. Flowchart of if statement
  • 6. Example-1 #include<stdio.h> void main() { int a=5,b=2; if(a>b) { printf(“a is big”); } printf(“Outer statement”); } Output: a is big Outer statement
  • 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.
  • 14. Syntax if ( outer-if condition) { if ( inner-if condition) { Inner-if statements; } else { Inner-if-else statements; } else { outer-if-else statements; }
  • 15. Flowchart of else-if ladder statement
  • 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.
  • 19. Flowchart of else-if ladder statement
  • 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;
  • 24. Flowchart for switch statement
  • 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
  • 33. //Infinite for loop #include<stdio.h> int main () { for(;;) { printf(“Hello Dearn"); } } Output: Hello Dear Hello Dear Hello Dear …. …. ….
  • 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
  • 41. Infinite while loop #include<stdio.h> void main(){ while(1){ printf(“Hello Dearn"); } } Output: Hello Dear Hello Dear ………. ……..
  • 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.
  • 46. Infinite do-while loop #include<stdio.h> void main(){ do{ printf(“Hello Dearn"); }while(True); } Output: Hello Dear Hello Dear ………. ……..
  • 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