MODULE 2: CONTROL
STRUCTURES
CO2: Implement, test
and execute
programs comprising
of control structures.
TOPIC TO BE COVERED
Control Structures
● Introduction to Control Structures
Branching structures
● If statement, If-else statement, Nested if-else, else-if Ladder
● Switch statement
Looping structures
● For loop, While loop, Do while loop
● break and continue
INTRODUCTION TO CONTROL
STRUCTURES
The statement in the program executed in order in which they appear
is called sequential execution. But sometimes we,
1) Need to select set of statement from several alternatives
2) Skip statement depending on condition
3) Repeat statement for known time.
So we need to use the control of the statement to be executed
INTRODUCTION TO CONTROL
STRUCTURES
Control Structures are just a way to specify flow of
control in programs.
Any algorithm or program can be more clear and
understood if they use self-contained modules called
as logic or control structures.
It analyzes and chooses the direction in which a
program flows based on certain parameters or
conditions.
There are three basic types of logic, or flow of control,
known as:
Sequence logic, or sequential flow
Selection logic, or conditional flow
INTRODUCTION TO CONTROL
STRUCTURES
A statement that is used to control the flow of execution in a program
is called control structure. It combines instruction into logical unit.
Logical unit has one entry point and one exit point.
Types of control structures
1. Sequence
2. Selection
3. Repetition
4. Function call (will be discussed in next module)
Sequence: Statements are executed in a specified order. No statement
is skipped and no statement is executed more than once.
INTRODUCTION TO CONTROL
STRUCTURES
For Example
#include<stdio.h>
void main()
{
int a;
int a=5;
printf(“Value of a = %d”,a);
}
INTRODUCTION TO CONTROL
STRUCTURES
Selection:
It selects a statement to execute on the basis of condition. Statement
is executed when the condition is true and ignored when it is false
e.g if, if else, switch structures.
INTRODUCTION TO CONTROL
STRUCTURES
#include<stdio.h>
void main ()
{
int y;
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year",y);
else
printf("%d is not a leap year“,y);
}
INTRODUCTION TO CONTROL
STRUCTURES
Repetition:
In this structure the statements are executed more than one time. It
is also known as iteration or loop e.g while loop, for loop do-while
loops etc.
INTRODUCTION TO CONTROL
STRUCTURES
For Example:
#include<stdio.h>
void main()
{
int a=1; //initialization of looping variable
while(a<=5) // checking condition
{
printf(“I am Indiann”);
a++; //updating the looping variable
} //end of while
} //end of main
SELECTION STRUCTURE
Selection structures allow the computer to make decisions in your
programs. It selects a statement or set of statements to execute on
the basis of a condition.
Types: ·
If-else structure ·
Switch structure
If-else structure:
The if-else statement is an extension of the simple if statement. It
executes one statement if it is true and other one if condition is false.
Switch structure:
It is used when there are many choices and only one statement is to
be executed.
SELECTION STRUCTURE - SIMPLE
IF
If Statement
The simplest if structure involves a single executable statement.
Execution of the statement occurs only if the condition is true.
Syntax:
if (condition)
statement;
SELECTION STRUCTURE - SIMPLE
IF
Example:
#include<stdio.h>
void main ()
{
int marks;
printf("Enter your marks:");
scanf("%d",&marks);
if(marks >=50)
printf("CONGRATULATIONS...!!! you have passed");
}
SELECTION STRUCTURE - SIMPLE
IF
Limitation of If
The statement(s) are executed if the condition is true; if the condition
is false nothing happens in other words we may say it is not the
effective one. Instead of it we use if-else statements etc.
SELECTION STRUCTURE – IF ELSE
If-else statement
In if-else statement if the condition is true, then the true
statement(s), immediately following the if-statement are executed
otherwise the false statement(s) are executed. The use of else
basically allows an alternative set of statements to be executed if the
condition is false.
Syntax:
If (condition)
{
Statement(s);
}
else
{
statement(s);
}
SELECTION STRUCTURE – IF ELSE
SELECTION STRUCTURE – IF ELSE
Example:
#include<stdio.h>
void main ()
{
int y;
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year",y);
else
printf("%d is not a leap year",y);
}
SELECTION STRUCTURE – IF ELSE
IF
IF -else if statement
It can be used to choose one block of statements from many blocks of
statements. The condition which is true only its block of statements is
executed and remaining are skipped.
Syntax:
if (condition)
{
statement(s);
}
else if (condition)
{
statement(s);
}
else
{
(statement);
}
SELECTION STRUCTURE – IF ELSE
IF
SELECTION STRUCTURE – IF ELSE
IF
Example:
#include<stdio.h>
void main ()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
printf("The number is positive");
else if (n<0)
printf("The number is negative");
else
printf("The number is zero");
}
SELECTION STRUCTURE – NESTED
IF
Nested if
In nested-if statement if the first if condition is true the control
will enter inner if. If this is true the statement will execute
otherwise control will come out of the inner if and the else
statement will be executed.
Syntax:
If (condition)
if(condition)
{
statement(s);
}
else
{
statement(s);
}
else
{
statement(s);
}
SELECTION STRUCTURE – NESTED
IF
SELECTION STRUCTURE – NESTED IF
Example:
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
} else {
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
return 0; }
SELECTION STRUCTURE – SWITCH
CASE
Switch Statement
Switch statement is alternative of nested if-else.it is executed when there are
many choices and only one is to be executed.
Syntax:
switch(expression)
{
case 1:
statement;
break;
case 2:
statement;
break;
.
.
.
.
case N:
statement;
break;
default:
statement;
}
SELECTION STRUCTURE – SWITCH
CASE
Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
char c;
clrscr();
printf("Enter an alphabet:");
scanf("%c",&c);
switch(c)
{
case'a':
case'A':
printf("You entered vowel.");
break;
case'e':
case'E':
printf("You entered vowel.");
break;
case'i':
case'I':
printf("You entered vowel.");
break;
case'o':
case'O':
printf("You entered vowel.");
break;
case'u':
case:'U'
printf("You entered vowel.");
break;
default:
printf("You entered a consonant.");
}
getch();
}
REPETITION STRUCTURE - LOOPS
Loops
loop is used to repeatedly execute set of statement. Structure that
repeats a statement is known as iterative, repetitive or looping
construct.
Purpose: Execute statements for a specified number of times.
To use a sequence of values.
Types
 While loop
 Do while loop
 For loop
REPETITION STRUCTURE - LOOPS
Depending upon the position of a control statement in a program,
looping in C is classified into two types:
1. Entry controlled loop
2. Exit controlled loop
In an entry control loop in C, a condition is checked before executing
the body of a loop. It is also called as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the
body of a loop. It is also called as a post-checking loop.
REPETITION STRUCTURE - LOOPS
The control conditions must be well defined and specified otherwise
the loop will execute an infinite number of times. The loop that does
not stop executing and processes the statements number of times is
called as an infinite loop. An infinite loop is also called as an "Endless
loop." Following are some characteristics of an infinite loop:
1. No termination condition is specified.
2. The specified conditions never meet.
REPETITION STRUCTURE
REPETITION STRUCTURE - WHILE
LOOP
It executes one or more statements while the given condition remains
true. It is useful when number of iterations is unknown.
Syntax
initialization
while (condition)
{
statement;
increment/decrement;
}
REPETITION STRUCTURE - WHILE
LOOP
REPETITION STRUCTURE - WHILE
LOOP
It is an entry-controlled loop. In while loop, a condition is evaluated
before processing a body of the loop. If a condition is true then and
only then the body of a loop is executed. After the body of a loop is
executed then control again goes back at the beginning, and the
condition is checked if it is true, the same process is executed until
the condition becomes false. Once the condition becomes false, the
control goes out of the loop.
After exiting the loop, the control goes to the statements which are
immediately after the loop. The body of a loop can contain more than
one statement. If it contains only one statement, then the curly braces
are not compulsory. It is a good practice though to use the curly
braces even we have a single statement in the body.
In while loop, if the condition is not true, then the body of a loop will
not be executed, not even once.
REPETITION STRUCTURE - WHILE
LOOP
Example
#include<stdio.h>
void main (void)
{
int n; //declaration
n=1; //Initialization
while (n<=5) // Condition
{
printf("n %d",n);
n++; //Increment(Updation)
}
}
REPETITION STRUCTURE - DO
WHILE LOOP
Do while loops are useful where loop is to be executed at least once.
In do while loop condition comes after the body of loop. This loop
executes one or more statements while the given condition is true.
Syntax
initialization
do
{
statement(s);
increment/decrement;
}
while (condition);
REPETITION STRUCTURE - DO
WHILE LOOP
REPETITION STRUCTURE - DO
WHILE LOOP
Example
#include<stdio.h>
void main (void)
{
int a;
a=1;
do
{
printf("n %d",a);
a++;
}
while (a<=5);
}
REPETITION STRUCTURE - FOR
LOOP
For loops are used when the number of iterations is known before
entering the loop. It is also known as counter-controlled loop.
Syntax
for (initialization;condition;increment/decrement)
{
statement()s;
}
REPETITION STRUCTURE - FOR
LOOP
REPETITION STRUCTURE - FOR
LOOP
#include<stdio.h>
#include<conio.h>
void main (void)
{
int n;
for(n=1;n<=5;n++)
{
printf("n %d",n);
}
getch();
}
Output:
1
2
3
4
5
REPETITION STRUCTURE - NESTED
LOOP
Nested loop
A loop within a loop is called nested loop. In this the outer loop is used for
counting rows and the internal loop is used for counting columns.
Any loop can be used as inner loop of another loop.
Syntax
for (initialization;condition;increment/decrement)
{
for(initialization;condition,increment/decrement)
{
statements(s);
}
}
REPETITION STRUCTURE - NESTED
LOOP
REPETITION STRUCTURE - NESTED
LOOP
Example
#include<stdio.h>
#include<conio.h>
void main (void)
{
clrscr();
for(int a=1;a<=3;a++) //count rows
{
for(int s=1;s<=3;s++) // count column
{
printf("*");
} //end of internal loop
printf("n");
} //end of external loop
getch();
} //end of main
REPETITION STRUCTURE-
CONTINUE
Continue statement transfer the control to the start of block.
It is used in the body of loop.
The continue statement skips the current iteration of the loop
and continues with the next iteration.
Its syntax is:
continue;
REPETITION STRUCTURE-
CONTINUE
The continue statement is almost always used with the if...else statement.
REPETITION STRUCTURE-
CONTINUE
// Program to calculate the sum of numbers (10 numbers max)
// If the user enters a negative number, it's not added to the result
#include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;
for (i = 1; i <= 10; ++i)
{
printf("Enter a n%d: ", i);
scanf("%lf", &number);
if (number < 0.0)
{
continue;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
return 0;
}
REPETITION STRUCTURE-
CONTINUE
When the user enters a negative number, the continue
statement is executed and it skips the negative number from
the calculation.
REPETITION STRUCTURE-
CONTINUE
// Program to calculate the sum of marks (Out of 20) in 5 subjects. The user has
to enter marks one by one, if the marks exceed 20 it's not added to the result
#include <stdio.h>
int main()
{
int i;
double marks, sum = 0.0;
for (i = 1; i <=5; ++i)
{
printf("Enter marks %d: ", i);
scanf("%lf", &marks);
if (marks>20||marks<0)
{
continue;
}
sum += marks; // sum = sum + marks;
}
printf("Sum = %.2lf", sum);
return 0;
}
REPETITION STRUCTURE-BREAK
STATEMENT
It is a tool to take the control out of the loop or block.
It transfers control to end of block.
It is used in body of loop and switch statements.
REPETITION STRUCTURE-BREAK
STATEMENT
REPETITION STRUCTURE-GOTO
STATEMENT
It is an unconditional transfer of control.
It transfers the control to the specific point.
The goto statement is marked by label
statement.Label statement can be used anywhere in
the function above or below the goto statement.
It is written as goto label;
where label is an identifeir
label: statement
REPETITION STRUCTURE-GOTO
STATEMENT
PROBLEMS
Example 1: Half Pyramid of *
*
**
***
****
*****
PROBLEMS
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i)
{
for (j = 1; j <= i; ++j)
{
printf("* ");
}
printf("n");
}
return 0;
}
PROBLEMS
Example 2: Half Pyramid of Numbers
1
12
123
1234
12345
PROBLEMS
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("n");
}
return 0;
}
PROBLEMS
Example 3: Half Pyramid of Alphabets
A
BB
CCC
DDDD
EEEEE
PROBLEMS
#include <stdio.h>
int main() {
int i, j;
char input, alphabet = 'A';
printf("Enter an uppercase character you want to print in the last row: ");
scanf("%c", &input);
for (i = 1; i <= (input - 'A' + 1); ++i) {
for (j = 1; j <= i; ++j) {
printf("%c ", alphabet);
}
++alphabet;
printf("n");
}
return 0;
}
PROBLEMS
Example 4: Inverted half pyramid of *
******
*****
***
**
*
PROBLEMS
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("n");
}
return 0;
}
PROBLEMS
Example 5: Inverted half pyramid of numbers
12345
1234
123
12
1
PROBLEMS
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("n");
}
return 0;
}
PROBLEMS
Example 6: Full Pyramid of *
*
***
*****
********
*********
PROBLEMS
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("n");
}
return 0;
}

Module 2- Control Structures

  • 1.
    MODULE 2: CONTROL STRUCTURES CO2:Implement, test and execute programs comprising of control structures.
  • 2.
    TOPIC TO BECOVERED Control Structures ● Introduction to Control Structures Branching structures ● If statement, If-else statement, Nested if-else, else-if Ladder ● Switch statement Looping structures ● For loop, While loop, Do while loop ● break and continue
  • 3.
    INTRODUCTION TO CONTROL STRUCTURES Thestatement in the program executed in order in which they appear is called sequential execution. But sometimes we, 1) Need to select set of statement from several alternatives 2) Skip statement depending on condition 3) Repeat statement for known time. So we need to use the control of the statement to be executed
  • 4.
    INTRODUCTION TO CONTROL STRUCTURES ControlStructures are just a way to specify flow of control in programs. Any algorithm or program can be more clear and understood if they use self-contained modules called as logic or control structures. It analyzes and chooses the direction in which a program flows based on certain parameters or conditions. There are three basic types of logic, or flow of control, known as: Sequence logic, or sequential flow Selection logic, or conditional flow
  • 5.
    INTRODUCTION TO CONTROL STRUCTURES Astatement that is used to control the flow of execution in a program is called control structure. It combines instruction into logical unit. Logical unit has one entry point and one exit point. Types of control structures 1. Sequence 2. Selection 3. Repetition 4. Function call (will be discussed in next module) Sequence: Statements are executed in a specified order. No statement is skipped and no statement is executed more than once.
  • 6.
    INTRODUCTION TO CONTROL STRUCTURES ForExample #include<stdio.h> void main() { int a; int a=5; printf(“Value of a = %d”,a); }
  • 7.
    INTRODUCTION TO CONTROL STRUCTURES Selection: Itselects a statement to execute on the basis of condition. Statement is executed when the condition is true and ignored when it is false e.g if, if else, switch structures.
  • 8.
    INTRODUCTION TO CONTROL STRUCTURES #include<stdio.h> voidmain () { int y; printf("Enter a year:"); scanf("%d",&y); if (y % 4==0) printf("%d is a leap year",y); else printf("%d is not a leap year“,y); }
  • 9.
    INTRODUCTION TO CONTROL STRUCTURES Repetition: Inthis structure the statements are executed more than one time. It is also known as iteration or loop e.g while loop, for loop do-while loops etc.
  • 10.
    INTRODUCTION TO CONTROL STRUCTURES ForExample: #include<stdio.h> void main() { int a=1; //initialization of looping variable while(a<=5) // checking condition { printf(“I am Indiann”); a++; //updating the looping variable } //end of while } //end of main
  • 11.
    SELECTION STRUCTURE Selection structuresallow the computer to make decisions in your programs. It selects a statement or set of statements to execute on the basis of a condition. Types: · If-else structure · Switch structure If-else structure: The if-else statement is an extension of the simple if statement. It executes one statement if it is true and other one if condition is false. Switch structure: It is used when there are many choices and only one statement is to be executed.
  • 12.
    SELECTION STRUCTURE -SIMPLE IF If Statement The simplest if structure involves a single executable statement. Execution of the statement occurs only if the condition is true. Syntax: if (condition) statement;
  • 13.
    SELECTION STRUCTURE -SIMPLE IF Example: #include<stdio.h> void main () { int marks; printf("Enter your marks:"); scanf("%d",&marks); if(marks >=50) printf("CONGRATULATIONS...!!! you have passed"); }
  • 14.
    SELECTION STRUCTURE -SIMPLE IF Limitation of If The statement(s) are executed if the condition is true; if the condition is false nothing happens in other words we may say it is not the effective one. Instead of it we use if-else statements etc.
  • 15.
    SELECTION STRUCTURE –IF ELSE If-else statement In if-else statement if the condition is true, then the true statement(s), immediately following the if-statement are executed otherwise the false statement(s) are executed. The use of else basically allows an alternative set of statements to be executed if the condition is false. Syntax: If (condition) { Statement(s); } else { statement(s); }
  • 16.
  • 17.
    SELECTION STRUCTURE –IF ELSE Example: #include<stdio.h> void main () { int y; printf("Enter a year:"); scanf("%d",&y); if (y % 4==0) printf("%d is a leap year",y); else printf("%d is not a leap year",y); }
  • 18.
    SELECTION STRUCTURE –IF ELSE IF IF -else if statement It can be used to choose one block of statements from many blocks of statements. The condition which is true only its block of statements is executed and remaining are skipped. Syntax: if (condition) { statement(s); } else if (condition) { statement(s); } else { (statement); }
  • 19.
  • 20.
    SELECTION STRUCTURE –IF ELSE IF Example: #include<stdio.h> void main () { int n; printf("Enter a number:"); scanf("%d",&n); if(n>0) printf("The number is positive"); else if (n<0) printf("The number is negative"); else printf("The number is zero"); }
  • 21.
    SELECTION STRUCTURE –NESTED IF Nested if In nested-if statement if the first if condition is true the control will enter inner if. If this is true the statement will execute otherwise control will come out of the inner if and the else statement will be executed. Syntax: If (condition) if(condition) { statement(s); } else { statement(s); } else { statement(s); }
  • 22.
  • 23.
    SELECTION STRUCTURE –NESTED IF Example: #include <stdio.h> int main() { double n1, n2, n3; printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1 >= n2) { if (n1 >= n3) printf("%.2lf is the largest number.", n1); else printf("%.2lf is the largest number.", n3); } else { if (n2 >= n3) printf("%.2lf is the largest number.", n2); else printf("%.2lf is the largest number.", n3); } return 0; }
  • 24.
    SELECTION STRUCTURE –SWITCH CASE Switch Statement Switch statement is alternative of nested if-else.it is executed when there are many choices and only one is to be executed. Syntax: switch(expression) { case 1: statement; break; case 2: statement; break; . . . . case N: statement; break; default: statement; }
  • 25.
  • 26.
    Example: #include<stdio.h> #include<conio.h> void main () { charc; clrscr(); printf("Enter an alphabet:"); scanf("%c",&c); switch(c) { case'a': case'A': printf("You entered vowel."); break; case'e': case'E': printf("You entered vowel."); break; case'i': case'I': printf("You entered vowel."); break; case'o': case'O': printf("You entered vowel."); break; case'u': case:'U' printf("You entered vowel."); break; default: printf("You entered a consonant."); } getch(); }
  • 27.
    REPETITION STRUCTURE -LOOPS Loops loop is used to repeatedly execute set of statement. Structure that repeats a statement is known as iterative, repetitive or looping construct. Purpose: Execute statements for a specified number of times. To use a sequence of values. Types  While loop  Do while loop  For loop
  • 28.
    REPETITION STRUCTURE -LOOPS Depending upon the position of a control statement in a program, looping in C is classified into two types: 1. Entry controlled loop 2. Exit controlled loop In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop. In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.
  • 29.
    REPETITION STRUCTURE -LOOPS The control conditions must be well defined and specified otherwise the loop will execute an infinite number of times. The loop that does not stop executing and processes the statements number of times is called as an infinite loop. An infinite loop is also called as an "Endless loop." Following are some characteristics of an infinite loop: 1. No termination condition is specified. 2. The specified conditions never meet.
  • 30.
  • 31.
    REPETITION STRUCTURE -WHILE LOOP It executes one or more statements while the given condition remains true. It is useful when number of iterations is unknown. Syntax initialization while (condition) { statement; increment/decrement; }
  • 32.
  • 33.
    REPETITION STRUCTURE -WHILE LOOP It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop. After exiting the loop, the control goes to the statements which are immediately after the loop. The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body. In while loop, if the condition is not true, then the body of a loop will not be executed, not even once.
  • 34.
    REPETITION STRUCTURE -WHILE LOOP Example #include<stdio.h> void main (void) { int n; //declaration n=1; //Initialization while (n<=5) // Condition { printf("n %d",n); n++; //Increment(Updation) } }
  • 35.
    REPETITION STRUCTURE -DO WHILE LOOP Do while loops are useful where loop is to be executed at least once. In do while loop condition comes after the body of loop. This loop executes one or more statements while the given condition is true. Syntax initialization do { statement(s); increment/decrement; } while (condition);
  • 36.
  • 37.
    REPETITION STRUCTURE -DO WHILE LOOP Example #include<stdio.h> void main (void) { int a; a=1; do { printf("n %d",a); a++; } while (a<=5); }
  • 38.
    REPETITION STRUCTURE -FOR LOOP For loops are used when the number of iterations is known before entering the loop. It is also known as counter-controlled loop. Syntax for (initialization;condition;increment/decrement) { statement()s; }
  • 39.
  • 40.
    REPETITION STRUCTURE -FOR LOOP #include<stdio.h> #include<conio.h> void main (void) { int n; for(n=1;n<=5;n++) { printf("n %d",n); } getch(); } Output: 1 2 3 4 5
  • 41.
    REPETITION STRUCTURE -NESTED LOOP Nested loop A loop within a loop is called nested loop. In this the outer loop is used for counting rows and the internal loop is used for counting columns. Any loop can be used as inner loop of another loop. Syntax for (initialization;condition;increment/decrement) { for(initialization;condition,increment/decrement) { statements(s); } }
  • 42.
  • 43.
    REPETITION STRUCTURE -NESTED LOOP Example #include<stdio.h> #include<conio.h> void main (void) { clrscr(); for(int a=1;a<=3;a++) //count rows { for(int s=1;s<=3;s++) // count column { printf("*"); } //end of internal loop printf("n"); } //end of external loop getch(); } //end of main
  • 44.
    REPETITION STRUCTURE- CONTINUE Continue statementtransfer the control to the start of block. It is used in the body of loop. The continue statement skips the current iteration of the loop and continues with the next iteration. Its syntax is: continue;
  • 45.
    REPETITION STRUCTURE- CONTINUE The continuestatement is almost always used with the if...else statement.
  • 46.
    REPETITION STRUCTURE- CONTINUE // Programto calculate the sum of numbers (10 numbers max) // If the user enters a negative number, it's not added to the result #include <stdio.h> int main() { int i; double number, sum = 0.0; for (i = 1; i <= 10; ++i) { printf("Enter a n%d: ", i); scanf("%lf", &number); if (number < 0.0) { continue; } sum += number; // sum = sum + number; } printf("Sum = %.2lf", sum); return 0; }
  • 47.
    REPETITION STRUCTURE- CONTINUE When theuser enters a negative number, the continue statement is executed and it skips the negative number from the calculation.
  • 48.
    REPETITION STRUCTURE- CONTINUE // Programto calculate the sum of marks (Out of 20) in 5 subjects. The user has to enter marks one by one, if the marks exceed 20 it's not added to the result #include <stdio.h> int main() { int i; double marks, sum = 0.0; for (i = 1; i <=5; ++i) { printf("Enter marks %d: ", i); scanf("%lf", &marks); if (marks>20||marks<0) { continue; } sum += marks; // sum = sum + marks; } printf("Sum = %.2lf", sum); return 0; }
  • 49.
    REPETITION STRUCTURE-BREAK STATEMENT It isa tool to take the control out of the loop or block. It transfers control to end of block. It is used in body of loop and switch statements.
  • 50.
  • 51.
    REPETITION STRUCTURE-GOTO STATEMENT It isan unconditional transfer of control. It transfers the control to the specific point. The goto statement is marked by label statement.Label statement can be used anywhere in the function above or below the goto statement. It is written as goto label; where label is an identifeir label: statement
  • 52.
  • 53.
    PROBLEMS Example 1: HalfPyramid of * * ** *** **** *****
  • 54.
    PROBLEMS #include <stdio.h> int main() { inti, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (j = 1; j <= i; ++j) { printf("* "); } printf("n"); } return 0; }
  • 55.
    PROBLEMS Example 2: HalfPyramid of Numbers 1 12 123 1234 12345
  • 56.
    PROBLEMS #include <stdio.h> int main(){ int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (j = 1; j <= i; ++j) { printf("%d ", j); } printf("n"); } return 0; }
  • 57.
    PROBLEMS Example 3: HalfPyramid of Alphabets A BB CCC DDDD EEEEE
  • 58.
    PROBLEMS #include <stdio.h> int main(){ int i, j; char input, alphabet = 'A'; printf("Enter an uppercase character you want to print in the last row: "); scanf("%c", &input); for (i = 1; i <= (input - 'A' + 1); ++i) { for (j = 1; j <= i; ++j) { printf("%c ", alphabet); } ++alphabet; printf("n"); } return 0; }
  • 59.
    PROBLEMS Example 4: Invertedhalf pyramid of * ****** ***** *** ** *
  • 60.
    PROBLEMS #include <stdio.h> int main(){ int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (j = 1; j <= i; ++j) { printf("* "); } printf("n"); } return 0; }
  • 61.
    PROBLEMS Example 5: Invertedhalf pyramid of numbers 12345 1234 123 12 1
  • 62.
    PROBLEMS #include <stdio.h> int main(){ int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (j = 1; j <= i; ++j) { printf("%d ", j); } printf("n"); } return 0; }
  • 63.
    PROBLEMS Example 6: FullPyramid of * * *** ***** ******** *********
  • 64.
    PROBLEMS #include <stdio.h> int main(){ int i, space, rows, k = 0; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i, k = 0) { for (space = 1; space <= rows - i; ++space) { printf(" "); } while (k != 2 * i - 1) { printf("* "); ++k; } printf("n"); } return 0; }