2. • Control structures form the basic entities of a “structured
programming language“.
• Control structures are used to alter the flow of execution of the
program.
3. There are three types of control structures available in C and C++
• Sequence structure (straight line paths)
• Selection structure (one or many branches)
• Loop structure (repetition of a set of activities)
6. If statement
if (condition)
{
Program statements;
}
If else statement
if (condition)
{
Program statements;
}
Else
{
Program statements;
}
Switch statement
switch (expression)
{
Case value_1;
Statement1;
Break;
Case value_2;
Statement2;
break;
Default:
Statementx;
break
}
7. Problem Statement
If the integer entered by user is 1 – output on screen “UNITED STATES”.
If the integer is 2 – output “SPAIN”, If the integer is 3 output “INDIA”. If
the user enters some other value – output “WRONG ENTRY”.
8. Loop structure (repetition of a set of activities)
• In programming, there exists situations when you need to repeat
single or a group of statements till some condition is met. Such as -
read all files of a directory, send mail to all employees one after
another etc. These task in C programming is handled by looping
statements.
• Looping statement defines a set of repetitive statements. These
statements are repeated with same or different parameters for a
number of times. Looping statement is also known
as iterative or repetitive statement.
9. C supports three looping statements.
• for loop
• while loop
• do...while loop
10. For Loop
• For loop is an entry controlled looping statement. It is used to repeat set of
statements until some condition is met.
• Looping statements whose condition is checked prior to the execution of
its body is called as Entry controlled loop.
Syntax of Loop
for(variable-initialization ; condition ; variable-update)
{
// Body of for loop
}
11.
12. /** * C program to print natural numbers from 1 to
10. */
#include <stdio.h>
int main()
{
/* Declare loop counter variable */
int count; /* Run a loop from 1 to 10 */
for(count=1; count<=10; count++)
{ /* Print current value of count */
printf("%d ", count);
}
return 0;
}