Académique Documents
Professionnel Documents
Culture Documents
Control structures are the statements that control the flow of the source code.
1. Conditional statement
2. Looping
if Statement
The syntax of the if statement in C programming is:
1. if (condition)
2. {
3. // statements to be executed if the condition is true
4. }
if...else Statement
The if statement may have an optional else block. The syntax of
the if..else statement is:
1. if (condition) {
2. // statements to be executed if the test expression is true
3. }
4. else {
5. // statements to be executed if the test expression is false
6. }
The if...else if…else allows you to check between multiple condition and execute different
statements.
Example:
1. #include <stdio.h>
2. int main()
3. {
4. int a,b,c;
5. printf("Enter first integer: ");
6. scanf("%d, &a);
7. printf("Enter second integer: ");
8. scanf("%d, &b);
9. printf("Enter third integer: ");
10. scanf("%d, &c);
11. if (a>b && a>c)
12. {
13. printf("greatest is %d”, a);
14. }
15. else if(b>a && b>c)
16. {
17. printf("greatest is %d”, b);
18. }
19. else
20. {
printf("greatest is %d”, c);
21. }
22. }
C switch Statement
The switch statement allows us to execute one code block among many
alternatives.
You can do the same thing with the if...else..if ladder. However, the syntax of
the switch statement is much easier to read and write.
Syntax of switch...case
1. switch (expression)
2. {
3. case constant1:
4. // statements
5. break;
6.
7. case constant2:
8. // statements
9. break;
10. .
11. .
12. .
13. default:
14. // default statements
15. }
are executed. For example, if the value of the expression is equal to constant2,
If we do not use break, all statements after the matching label are executed.
Example:
#include<stdio.h>
void main ()
int n;
printf("enter a number");
scanf("%d",&n);
switch(n)
case 1: printf("sunday");
break;
case 2: printf("monday");
break;
case 3: printf("tuesday");
break;
case 4: printf("wednesday");
break;
case 5: printf("thursday");
break;
case 6: printf("friday");
break;
Important questions:
while loop
The syntax of the while loop is:
1. while (condition)
2. {
3. // statements inside the body of the loop
4. }
do...while loop
The do..while loop is similar to the while loop with one important difference. The
body of do...while loop is executed at least once. Only then, the test expression is
evaluated.
The syntax of the do...while loop is:
1. do
2. {
3. // statements inside the body of the loop
4. }
5. while (condition);
Example:
#include<stdio.h>
void main()
{
int a=1;
do
{
printf("\n %d", a);
a=a+1;
}while(a<=5);
}
for Loop
The syntax of the for loop is:
1. for (initializationStatement; condition; increment/decrement)
2. {
3. // statements inside the body of loop
4. }
This process goes on until the condition is false. When the condition is false, the
loop terminates.
#include<stdio.h>
void main ()
int i;
}
While do-while
1. Condition is at top. Condition is at bottom.
2. There is no semicolon at the end of The semicolon is compulsory at
while. the end of do-while.
Computer executes the body if and Computer executes the body at
3. only if the condition is true. least once even if the condition is
false.
4. This should be used when the This should be used when the
condition is more important. process is more important.
5. This loop is also called as entry This loop is also called as entry
controlled loop. controlled loop.
6. It contains only one keyword while. It contains two keyword do and
while.
Syntax: Syntax:
while(condition) do{
7. { Statements;
Statements; }while(condition);
}
Flowchart: Flowchart:
8.
Example: Example:
while(n<=100) do{
{ printf(“%d”,n);
9. printf(“%d”,n); n=n+1;
n=n+1; }while(n<=100);
}
Important questions:
14.Explain the different types of loops with examples.
15.Write the difference between while and do…while loop.
16.Write a program to print factorial of a number using while, do…while,
for loop.
17.Write a program to print first 10 even numbers.
18.Write a program to find sum up to n numbers.
19.Write a program to print the Fibonacci series up to nth term.
1 1 2 3 5 8…….upto nth term
20.Write a program to print the multiplication table of a number.