Vous êtes sur la page 1sur 14

Control Structure:

Control structures are the statements that control the flow of the source code.

There are two types of control structure:

1. Conditional statement
2. Looping

Conditional statement: These are the decision making statements. 


Following are the various condtional statements:

 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. }

How if statement works?


The if statement evaluates the condition inside the parenthesis ().
 If the condition is evaluated to true, statements inside the body of if are
executed.
 If the condition is evaluated to false, statements inside the body of if are
nconot executed.
Example
1. #include <stdio.h>
2. void main()
3. {
4. int n;
5.
6. printf("Enter an integer: ");
7. scanf("%d", &n);
8. if (n==5)
9. {
10. printf("the entered number is equal to 5”);
11. }
12.
13. }

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. }

How if...else statement works?

If the condition is evaluated to true,

statements inside the body of if are executed.


 statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,

 statements inside the body of else are executed


 statements inside the body of if are skipped from execution.
Example:
1. #include <stdio.h>
2. void main()
3. {
4. int n;
5. printf("Enter an integer: ");
6. scanf("%d", &n);
7. if (n%2 == 0)
8. {
9. printf("even number");
10. }
11. else
12. {
13. printf("odd number”);
14. }
15. }

C if...else if…else Statement

The if...else statement executes two different codes depending upon whether the


condition is true or false. Sometimes, a choice has to be made from more than 2
possibilities.

The if...else if…else allows you to check between multiple condition and execute different
statements.

Syntax of if...else if…else statement.


1. if (condition 1)
2. {
3. // statement(s)
4. }
5. else if(condition 2)
6. {
7. // statement(s)
8. }
9. else if (condition 3)
10. {
11. // statement(s)
12. }
13. .
14. .
15. else
16. {
17. // statement(s)
18. }

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. }

How does the switch statement work?

The expression is evaluated once and compared with the values of each case label.


 If there is a match, the corresponding statements after the matching label

are executed. For example, if the value of the expression is equal to constant2,

statements after case constant2: are executed until break is encountered.

 If there is no match, the default statements are executed.

If we do not use break, all statements after the matching label are executed.

The default clause inside the switch statement is optional.

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;

default: printf("wrong input");

Important questions:

1. Explain if...else with an example.


2. Explain switch with an example.
3. Write a program to check whether a number is even or odd.
4. Write a program to check whether a number is divisible by 5.
5. Write a program to check whether a number is divisible by 5 and 7.
6. Write a program to print the percentage of 5 subjects if the student passed in all the
subject otherwise print fail.
7. Write a program to check whether a number is positive or negative.
8. Write a program to find greatest of two numbers.
9. Write a program to find the smallest of two numbers.
10. Write a program to find the greatest of three numbers.
11. Write a program to find the smallest of three numbers.
12. Write a menu driven program to add, subtract, divide and multiply two numbers using
if…else if… else.
13. Write a menu driven program to add, subtract, divide ,multiply and modular division of
two numbers using switch.

LOOP: A loop is a block of statement that keeps on executing until a


condition is satisfied.
There are three types of loops:
 while loop
 do … while loop
 for loop

while loop
The syntax of the while loop is:
1. while (condition)
2. {
3. // statements inside the body of the loop
4. }

How while loop works?


 The while loop evaluates the condition inside the parenthesis ().
 If the condition is true, statements inside the body of while loop are
executed. Then, the condition is evaluated again.
 The process goes on until the condition is evaluated to false.
 If the test expression is false, the loop terminates (ends).
Example:
#include<stdio.h>
void main()
{
int a=1;
while(a<=5)
{
printf("\n %d", a);
a=a+1;
}
}

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);

How do...while loop works?


 The body of do...while loop is executed once. Only then, the condition is
evaluated.
 If the test expression is true, the body of the loop is executed again and the
condition is evaluated.
 This process goes on until the condition becomes false.
 If the test expression is false, the loop ends.

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. }

How for loop works?


 The initialization statement is executed only once.
 Then, the test expression is evaluated. If the test expression is evaluated to
false, the for loop is terminated.
 However, if the condition is evaluated to true, statements inside the body
of for loop are executed, and increment/decrement expression is done.
 Again the condition is evaluated.

This process goes on until the condition is false. When the condition is false, the
loop terminates.

for loop Flowchart


Example:

#include<stdio.h>

void main ()

int i;

for(i=1; i <=5; i=i+1)

printf("\n %d", 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.

Vous aimerez peut-être aussi