2. Control structure:
In a program, a control structure determines the order in which
statements are executed. Control structures refer to the structure
of program that may be sequential or conditional or iterative. C
programs are basically of three structures:
โข In sequential execution, each statement in the source code will be
executed one by one in a sequential order.
โข The selection or conditional statement is used for making decisions
and branching statements.
โข The iterative control structures are used for repetitively executing a
block of code multiple times.
Control statement:
The statement that alters the flow of execution of the program is
known as control statements.
1. Decision Making (conditional) statements
2. Loop statements
3. Jumping Statements
8/12/2021 2
3. 1. Decision Making (conditional)
statements
Conditional statement:
A statement that is executed only when certain condition
within a program has been met is called conditional
expression. These expressions are used for decision
making.
Following statements are used to perform the task of
decision making:
I. IF statement
II. SWITCH statement
III. Conditional statement
8/12/2021 3
4. I. IF statement:
If the statement is used to control the flow of
execution of statements. It is implemented in
different forms. Depending on the complexity
of conditions to be tested, the different forms
are:
A. Simple if statement
B. Ifโฆ.. else
C. Nested if else
D. Else if ladder
8/12/2021 4
5. A. Simple if statement
Syntax of simple if statement:
if (condition)
{
//These statements will only execute if the condition is true
}
Flowchart for simple if statement:
8/12/2021 5
6. Example for simple if statement:
#include <stdio.h>
#include <conio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf(" %d is less than %d",x,y);
}
return 0;
getch();
}
8/12/2021 6
7. B. If โฆ. else Statement
Syntax of if...else statement:
if (testexpression)
{
// statementsto be executed if the test expression is true
}
else {
// statementsto be executed if the test expression is false
}
8/12/2021 7
8. Example for if...else statement:
// Check whether an integer is odd or even using ifโฆ. Else statement
#include <stdio.h>
int main()
{
int num;
printf("Enteran integer: ");
scanf("%d", &num);
if (num%2 == 0)
{
printf("%dis an even integer.",num);
}
else
{
printf("%dis an odd integer.",num);
}
return 0;
}
8/12/2021 8
11. Example of Nested if...else statement:
/* C Program to find given number is positive or negative using nested if ...else */
#include <stdio.h>
#include <conio.h>
int main()
{
float num;
printf("Enter a number: ");
scanf("%f",&num);
if (num <= 0)
{
if (num == 0)
printf("Youentered 0.");
else
printf("%fis a negative number.",num);
}
else
printf("%fis a positive number.",num);
return 0;
getch();
}
8/12/2021 11
12. // Check Largest number among two given numbers
#include <stdio.h>
main()
{
int a, b;
printf("Enter two numbers: n");
scanf("%d %d", &a, &b);
if(a >= b)
{
if (a==b)
{
printf("Both number are equaln");
}
else {
printf("%d is Largestn", a);
}
}
else {
printf("%d is Largestn", b);
}
}
8/12/2021 12
13. D. Else if ladder
Syntax of else if ladder statement:
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}
8/12/2021 13
15. Example of else if ladder statement:
// Check Largest number among two given numbers
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: n");
scanf("%d %d", &a, &b);
if(a == b)
{
printf("Both numbers are equalโ,a,b);
}
else if (a > b)
{
printf(" %d is largest than %d", a, b);
}
else
{
printf(" %d is largest than %d", b, a);
}
return 0;
}
8/12/2021 15
16. II. Switch Statement
The switch statement allows us to execute one code block among many alternatives.
A switch statement tests the value of a variable and compares it with multiple cases.
Once the case match is found, a block of statements associated with that particular case is executed.
Each case in a block of a switch has a different name/number which is referred to as an identifier.
The value provided by the user is compared with all the cases inside the switch block until the match is
found.
If a case match is not found, then the default statement is executed, and the control goes out of the
switch block.
Syntax of switch statement:
switch (expression)
โ{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
8/12/2021 16
18. #include <stdio.h>
int main()
{
int num=2;
switch(num)
{
case 1:
printf("Case1:Value is: %d", num);
break;
case 2:
printf("Case2:Value is: %d", num);
break;
case 3:
printf("Case3:Value is: %d", num);
break;
default:
printf("Default:Value is: %d", num);
}
return 0;
}
8/12/2021 18
19. #include <stdio.h>
main()
{
int a, b;
char ch;
printf("What do you want to perform?? n ");
printf(" Enter first letter: A for add, S for subtract, M for multiply, or D for divide?n");
printf("Your choice: ");
ch = getchar();
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
switch(ch)
{
case 'A': printf("%d", a + b);
break;
case 'S': printf("%d", a - b);
break;
case 'M': printf("%d", a * b);
break;
case 'D': if(b!=0) printf("%d", a / b);
break;
default:
printf("choose properly") ;
}
}
8/12/2021 19
20. III. Conditional(ternary ) statement
If any operator is used on three operands or variable is known as Ternary
Operator.
The first argument is a comparison argument, the second is the result
upon a true comparison, and the third is the result upon a false
comparison.
Ternary operator is shortened way of writing an if-else statement.
syntax:
(condition) ? expression-2 : expression-3
8/12/2021 20
21. //example program to illustrate the use of ternary statements
#include<stdio.h>
main()
{
int a, b, c, large;
printf("Enter any three number: ");
scanf("%d%d%d",&a,&b,&c);
large=a>b ? (a>c?a:c) : (b>c?b:c);
printf("Largest Number is: %d", large);
}
8/12/2021 21
22. 2. Loop statements
Loop statement:
loops are used to repeat a block of code until a
specified condition is met.
Following statements are used to perform
repetitive task:
I. for loop
II. while loop
III. do while loop
8/12/2021 22
36. 3 . Jumping statement
There are three different controls used to jump
from one C program statement to another and
make the execution of the programming
procedure fast. These three Jumping controls are:
A. go to statement
B. break statement
C. continue statement
8/12/2021 36
37. A. go to statement
The go to moves the control on a specified address called label or label name.
Syntax:
goto label_name;
.. ..
label_name: C-statements
Example program of go to statement:
#include <stdio.h>
main()
{
int sum=0;
for(int i = 0; i<=10; i++)
{
sum = sum+i;
if(i==5)
{
goto addition;
}
}
addition:
printf("%d", sum);
}
8/12/2021 37
38. B. break statement
Break is always used with the decision-makingstatementlike if and switch
statements.
The statementwill quit from the loop when the conditionis true.
Example program using break statement
#include< stdio.h>
main()
{
int i=1;
while(i<=10)
{
if(i==6)
{
break;
}
printf("n I=%d",i);
i++;
}
}
8/12/2021 38
39. C. continue statement
This statementis skip some part of iteration (loop) and comes to the next looping
step i.e. it will increment / decrement the loop value, when continue occurs.
Example program using continue statement
#include< stdio.h>
main()
{
int i=1;
while(i<=10)
{
if(i==6)
{
i++;
continue;
}
printf("n I=%d",i);
i++;
}
}
8/12/2021 39
40. Assignment 4(submit up to 22 Jan)
1. WAP to input an integer and check if it is palindrome or not.
2. Differentiate between
โข pre-test and post-test loop.
โข Break and continue statement
โข Switch and nested if else
3. WAP to generate all prime numbers from 1 to 100.
4. WAP to evaluate the following power series:
ex =1+x/1!+x2/2!+x3/3!+โฆโฆ.xn/n!, 0<x<1
3. WAP to display the following output :
5
5 4
5 4 3
8/12/2021 40