Chapter 4
Control Structures
8/12/2021 1
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
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
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
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
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
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
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
C. Nested if...else statement
Syntax of Nested if...else statement:
if(condition) {
if(condition2) {
//Statement1
}
else {
//Statement2
}
}
else {
//Statement3
}
8/12/2021 9
Flowchart of Nested if...else statement:
8/12/2021 10
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
// 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
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
Flowchart of else if ladder statement:
8/12/2021 14
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
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
flowchart of switch statement
8/12/2021 17
#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
#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
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
//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
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
I. for loop
8/12/2021 23
8/12/2021 24
8/12/2021 25
8/12/2021 26
8/12/2021 27
8/12/2021 28
8/12/2021 29
II. while loop
8/12/2021 30
8/12/2021 31
8/12/2021 32
III. do while loop
8/12/2021 33
8/12/2021 34
8/12/2021 35
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
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
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
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
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
Thank you 
8/12/2021 41

chapter-4 slide.pdf

  • 1.
  • 2.
    Control structure: In aprogram, 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: Ifthe 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 ifstatement 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 simpleif 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...elsestatement: // 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
  • 9.
    C. Nested if...elsestatement Syntax of Nested if...else statement: if(condition) { if(condition2) { //Statement1 } else { //Statement2 } } else { //Statement3 } 8/12/2021 9
  • 10.
    Flowchart of Nestedif...else statement: 8/12/2021 10
  • 11.
    Example of Nestedif...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 Largestnumber 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 ifladder 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
  • 14.
    Flowchart of elseif ladder statement: 8/12/2021 14
  • 15.
    Example of elseif 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 Theswitch 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
  • 17.
    flowchart of switchstatement 8/12/2021 17
  • 18.
    #include <stdio.h> int main() { intnum=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 toillustrate 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 Loopstatement: 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
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
    III. do whileloop 8/12/2021 33
  • 34.
  • 35.
  • 36.
    3 . Jumpingstatement 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 tostatement 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 Breakis 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 Thisstatementis 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 upto 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
  • 41.