This document discusses various control structures in programming like if-else statements, switch statements, and loops. It provides syntax and examples of using if-else statements to execute code conditionally based on simple or nested conditions. Switch statements allow selecting between multiple code blocks based on a variable's value. Loops like for, while, and do-while loops are covered, which allow repetitive execution of code. The break and continue statements are also introduced to control loop flow. Examples are provided to demonstrate different control structures for problems like finding largest of numbers, checking positive/negative/zero, and character input validation.
2. Control Structures
● Introduction to Control Structures
Branching and looping structures
● If statement, If-else statement, Nested if-
else, else-if Ladder
● Switch statement
● For loop, While loop, Do while loop
● break and continue
3. A statement that is used to control the flow
of execution in a program is called control
structure.
The control statements help users specify the
order of execution of the instructions present
in a program.
Instead of executing statements in order,
control structures will allow us to jump to
other places in the program.
6. Used to execute a statement or a set of
statements conditionally.
If the condition, whatever it is, is true, then
the statement is executed.
If the condition is not true then the statement
is not executed and control transfers to the
next executable statements, outside the body
of if.
11. Question 2:
While purchasing certain items, a discount of
10% is offered if quantity purchased is more
than 1000. Accept quantity and price from
user, write a program to calculate the total
expenses.
12. #include<stdio.h>
#include<conio.h>
void main()
{
int q, dis=0;
float r total;
printf(“enter quantity:”);
scanf(“%d”,&q,);
printf(“enter rate:”);
scanf(“%f”,&r,);
if(q>1000)
{
dis=10;
}
total=q*r-q*r*dis/100;
printf(“Expenses=%f”,total);
getch();
}
15. If there are two statements
to be executed
alternatively, then if-else
statement is used.
If the Boolean expression
evaluates to true, then
the if block will be
executed, otherwise,
the else block will be
executed.
20. Question 2:
While purchasing certain items, a discount of
10% is offered if quantity purchased is more
than 1000. Accept quantity and price from
user, write a program to calculate the total
expenses.
37. Question
Accept temperature from user and print
accordingly
Its very cold(if temperature<=0)
Its cold (if temperature is between 1 to 10)
Its cool out (if temperature is between 11 to
20)
Its warm (if temperature is between 21 to 30)
If all above conditions are false then print its
hot
39. Question 1:
Accept time from user and print accordingly
Good morning (1am-12pm)
Good after noon (12pm -16pm)
Good evening (16pm to 19pm)
Good night ( 19pm to 24pm)
41. Question 2:
Write a program to accept number from user,
if number=1 print red
number=2 print green
number=3 print yellow
number=4 print blue
42. #include<stdio.h>
void main()
{
int n;
printf(" Enter 1 to 4 to select random color");
scanf("%d",&n);
if(n==1)
{
printf("You selected Red color");
}
else if(n==2)
{
printf("You selected Green color");
}
else if(n==3)
{
printf("You selected yellow color");
}
else if(n==4)
{
printf("You selected Blue color");
}
else
{
printf("No color selected");
}
}
43. Question 3:
WAP to accept 1-6 numbers from user and
print spelling of respective number.
44. #include <stdio.h>
main ( )
{
int x;
printf("enter an integer between 1 and 6“);
scanf(“%d”,&x);
if(x==1)
printf("the number is one”);
else if(x==2)
printf("the number is two”);
else if(x==3)
printf("the number is three”);
else if(x==4)
printf("the number is four”);
else if(x==5)
printf("the number is five”);
else if(x==6)
printf("the number is six”);
else
printf("you didn't follow the rules“);
}
45. The switch statement
causes a particular group
of statement to be chosen
from several available
groups.
The selection is based
upon the value of an
expression, int variable or
character variable which is
included in the switch
statement.
46. switch(expression)
{
case constant-expression_1 : statement(s);
break;
case constant-expression_2 : statement(s);
break;
case constant-expression_n : statement(s);
break;
default : default_block; //optional
}
49. #include<stdio.h>
#include<conio.h>
void main()
{
char choice;
printf("n Enter your choice");
scanf("%c",&choice);
switch(choice)
{
case ‘R’:
case 'r':printf("n Red");
break;
case ‘W’:
case 'w':printf("n White");
break;
case ‘B’:
case 'b':printf("n Blue");
break;
default:printf("n wrong choice");
}
getch();
}
50. Question 1:
WAP to accept 1-6 numbers from user and
print spelling of respective number using
switch case.
51. #include<stdio.h>
#include<conio.h>
void main()
{
int choice;
printf("n Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("n One");
break;
case 2:printf("n Two");
break;
case 3:printf("n Three");
break;
case 4:printf("n Four");
break;
case 5:printf("n Five");
break;
case 6:printf("n Six");
break;
default:printf("n wrong choice");
}
getch();
}
52. Question 2:
WAP to print number of days in a month using
switch case
53. #include <stdio.h>
int main()
{
int month;
printf("Enter month number(1-12): ");
scanf("%d", &month);
switch(month)
{
case 1: printf("31 days");
break;
case 2: printf("28/29 days");
break;
case 3: printf("31 days");
break;
case 4: printf("30 days");
break;
case 5: printf("31 days");
break; contd…
54. case 6: printf("30 days");
break;
case 7: printf("31 days");
break;
case 8: printf("31 days");
break;
case 9: printf("30 days");
break;
case 10: printf("31 days");
break;
case 11: printf("30 days");
break;
case 12: printf("31 days");
break;
default: printf("Invalid input! Please enter month number between 1-12");
}
}
55. #include <stdio.h>
void main()
{
int month;
printf("Enter month number(1-12): ");
scanf("%d", &month);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: printf("31 days");
break;
case 4:
case 6:
case 9:
case 11: printf("30 days");
break;
case 2: printf("28/29 days");
break;
default: printf("Invalid input! Please enter month number between 1-12");
}
}
56. Write a menu driven program for basic
arithmetic operations( Addition, Subtraction,
Multiplication & Division).
57. #include<stdio.h>
void main()
{
float num1,num2,ans;
int opt;
printf("nEnter the First Number : ");
scanf("%f",&num1);
printf("nEnter the Second Number : ");
scanf("%f",&num2);
printf(“1.Additionn2.Subtractionn3.Multiplyn4.Divide
n5.Exit");
printf("nEnter your choice : ");
scanf("%d",&opt);
switch(opt)
{ contd…
58. case 1:
ans = num1+num2;
printf("nThe addition of 2 numbers is : %f",ans);
break;
case 2:
ans = num1-num2;
printf("nThe differnce of 2 numbers is : %f",ans);
break;
case 3:
ans = num1*num2;
printf("nThe product of 2 numbers is : %f",ans);
break;
case 4:
ans = num1/num2;
printf("nThe division of 2 numbers is : %f",ans);
break;
default:
printf("nYou Entered Wrong Choicen");
}
}
64. Taking control back to previous statement is
known as ‘looping’.
Looping is also called a repetitive or iterative
control mechanism.
Parts of loop: 1. body of the loop
2. Control statement
If the control statement is placed before the
body of the loop, it is called entry-controlled
loop.
If the control statement is placed after the
body of the loop, it is called exit-controlled
loop.
65. Initialization
To set the initial value for the loop counter.
Decision
An appropriate test condition to determine
whether the loop be executed or not.
Updation
incrementing or decrementing the counter
value.
66. The while statement
The do-while statement
The for statement
67. This is used to execute a set of statements
repeatedly as long as the specified condition
is true.
It is entry-controlled loop.
If the loop condition evaluates to be true,
then the statements will be executed.
If the loop condition evaluates to be false,
control will go to the first statement after
curly bracket.
74. Question3
Write a program to print Fibonacci series of n
numbers using while loop.
e.g 0 1 1 2 3 5 8 13...
75. // 0 1 1 2 3 5 8 13...
#include<stdio.h>
void main()
{
int n,i,a,b,c;
printf("Enter a number: ");
scanf("%d",&n);
i=1;
a=0;
b=1;
while(i<=n)
{
printf("%d ",a);
c = a + b;
a = b;
b = c;
i++;
}
}
76. It is exit-controlled loop.
When the loop is constructed using the while
loop, the condition in the loop will be
checked first and then the control will go in
side the loop.
The do-while loop will be executed whether
or not the condition in the loop is true at
least for one time.
81. Question2:
Write a program to find palindrome of
given number using do-while loop.
An integer is a palindrome if the reverse of that
number is equal to the original number.
e.g 1221
82. #include<stdio.h>
void main()
{
int n,a,r,s=0;
printf("n Enter The Number:");
scanf("%d",&n);
a=n;
do
{
r=n%10;
s=s*10+r;
n=n/10;
} while(n>0);
if(a==s)
{ printf("n %d is a Palindrome Number",a); }
else
{ printf("n %d is a not Palindrome Number",a); }
}
83. Question3
Write a program to find whether given
number is Armstrong or not using do-
while loop.(egs. 153,371,1634)
In the case of an Armstrong number of 3 digits,
the sum of cubes of each digit is equal to the
number itself. For example, 153 is an Armstrong
number because
153 = 1*1*1 + 5*5*5 + 3*3*3
86. A for loop is a repetition control structure that allows
you to efficiently write a loop that needs to execute a
specific number of times.
Initialization step allows you to declare and initialize
any loop control variables.
Next, the condition is evaluated. If it is true, the body of
the loop is executed. If it is false, the body of the loop
does not execute and the flow of control jumps to the
next statement just after the 'for' loop.
After the body of the 'for' loop executes, the flow of
control jumps back up to the increment statement.
91. Question 2
Write a program to generate the
multiplication table of a number (entered by
the user) using for loop.
92. #include <stdio.h>
void main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d n", n, i, n*i);
}
}
93. Question 3
Write a program to find factorial of given
number using for loop.
The factorial of a positive number n is given by:
factorial of n (n!) = 1 * 2 * 3 * 4....n
The factorial of a negative number doesn't exist.
And, the factorial of 0 is 1.
96. C programming allows to use one loop inside
another loop.
Syntax :
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
99. When a break statement is encountered
inside a loop, the loop is immediately
terminated and the program control resumes
at the next statement following the loop.
It can be used to terminate a case in
the switch statement.
Syntax: break;
100. #include <stdio.h>
void main ()
{
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %dn", a);
a++;
if( a > 15)
{
/* terminate the loop using break statement */
break;
}
}
getch();
}
101. The continue statement in C programming
works somewhat like the break statement.
Instead of forcing termination, it forces the
next iteration of the loop to take place,
skipping any code in between.
Syntax : continue;
102. #include <stdio.h>
void main ()
{
int a = 10;
/* do while loop execution */
do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
continue;
}
printf("value of a: %dn", a);
a++;
} while( a < 20 );
}
103. A goto statement in C
programming provides an
unconditional jump from
the 'goto' to a labeled
statement in the same
function.
Syntax :
104. #include<stdio.h>
#include<conio.h>
void main()
{
int age;
Vote: //lable
printf("you are eligible for voting");
NoVote: //lable
printf("you are not eligible to vote");
printf("Enter you age:");
scanf("%d", &age);
if(age>=18)
goto Vote;
else
goto NoVote;
getch();
}
105. Use of goto statement is
highly discouraged in any
programming language
because it makes difficult
to trace the control flow of
a program, making the
program hard to
understand and hard to
modify.
Any program that uses a
goto can be rewritten to
avoid them.