SlideShare a Scribd company logo
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;
}
Ad

More Related Content

What's hot (20)

Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Uma mohan
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
Anil Kumar
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
Function
FunctionFunction
Function
jayesh30sikchi
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
Sudip Singh
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
ErumShammim
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
 
Minimization of DFA.pptx
Minimization of DFA.pptxMinimization of DFA.pptx
Minimization of DFA.pptx
SadagopanS
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
Thesis Scientist Private Limited
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
Kamal Acharya
 
Nested loops
Nested loopsNested loops
Nested loops
Adnan Ferdous Ahmed
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
baabtra.com - No. 1 supplier of quality freshers
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
Infoviaan Technologies
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
Online
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
Uma mohan
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
Suneel Dogra
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
Anil Kumar
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
Sudip Singh
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
ErumShammim
 
Minimization of DFA.pptx
Minimization of DFA.pptxMinimization of DFA.pptx
Minimization of DFA.pptx
SadagopanS
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
Kamal Acharya
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
Online
 

Similar to Module 2- Control Structures (20)

C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
MURALIDHAR R
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
JavvajiVenkat
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdfModule 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and loopingManaging input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
Rai University
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
Rai University
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
Rai University
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Bsit1
Bsit1Bsit1
Bsit1
jigeno
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Session 3
Session 3Session 3
Session 3
Shailendra Mathur
 
control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
Likhil181
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
MURALIDHAR R
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdfModule 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and loopingManaging input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
Rai University
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
Rai University
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
Rai University
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
Likhil181
 
Ad

More from nikshaikh786 (20)

Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
Module 1_ Introduction.pptx
Module 1_ Introduction.pptxModule 1_ Introduction.pptx
Module 1_ Introduction.pptx
nikshaikh786
 
Module 1_ Introduction to Mobile Computing.pptx
Module 1_  Introduction to Mobile Computing.pptxModule 1_  Introduction to Mobile Computing.pptx
Module 1_ Introduction to Mobile Computing.pptx
nikshaikh786
 
Module 2_ GSM Mobile services.pptx
Module 2_  GSM Mobile services.pptxModule 2_  GSM Mobile services.pptx
Module 2_ GSM Mobile services.pptx
nikshaikh786
 
MODULE 4_ CLUSTERING.pptx
MODULE 4_ CLUSTERING.pptxMODULE 4_ CLUSTERING.pptx
MODULE 4_ CLUSTERING.pptx
nikshaikh786
 
MODULE 5 _ Mining frequent patterns and associations.pptx
MODULE 5 _ Mining frequent patterns and associations.pptxMODULE 5 _ Mining frequent patterns and associations.pptx
MODULE 5 _ Mining frequent patterns and associations.pptx
nikshaikh786
 
DWM-MODULE 6.pdf
DWM-MODULE 6.pdfDWM-MODULE 6.pdf
DWM-MODULE 6.pdf
nikshaikh786
 
TCS MODULE 6.pdf
TCS MODULE 6.pdfTCS MODULE 6.pdf
TCS MODULE 6.pdf
nikshaikh786
 
Module 3_ Classification.pptx
Module 3_ Classification.pptxModule 3_ Classification.pptx
Module 3_ Classification.pptx
nikshaikh786
 
Module 2_ Introduction to Data Mining, Data Exploration and Data Pre-processi...
Module 2_ Introduction to Data Mining, Data Exploration and Data Pre-processi...Module 2_ Introduction to Data Mining, Data Exploration and Data Pre-processi...
Module 2_ Introduction to Data Mining, Data Exploration and Data Pre-processi...
nikshaikh786
 
Module 1_Data Warehousing Fundamentals.pptx
Module 1_Data Warehousing Fundamentals.pptxModule 1_Data Warehousing Fundamentals.pptx
Module 1_Data Warehousing Fundamentals.pptx
nikshaikh786
 
Module 2_ Cyber offenses & Cybercrime.pptx
Module 2_ Cyber offenses & Cybercrime.pptxModule 2_ Cyber offenses & Cybercrime.pptx
Module 2_ Cyber offenses & Cybercrime.pptx
nikshaikh786
 
Module 1- Introduction to Cybercrime.pptx
Module 1- Introduction to Cybercrime.pptxModule 1- Introduction to Cybercrime.pptx
Module 1- Introduction to Cybercrime.pptx
nikshaikh786
 
MODULE 5- EDA.pptx
MODULE 5- EDA.pptxMODULE 5- EDA.pptx
MODULE 5- EDA.pptx
nikshaikh786
 
MODULE 4-Text Analytics.pptx
MODULE 4-Text Analytics.pptxMODULE 4-Text Analytics.pptx
MODULE 4-Text Analytics.pptx
nikshaikh786
 
Module 3 - Time Series.pptx
Module 3 - Time Series.pptxModule 3 - Time Series.pptx
Module 3 - Time Series.pptx
nikshaikh786
 
Module 2_ Regression Models..pptx
Module 2_ Regression Models..pptxModule 2_ Regression Models..pptx
Module 2_ Regression Models..pptx
nikshaikh786
 
MODULE 1_Introduction to Data analytics and life cycle..pptx
MODULE 1_Introduction to Data analytics and life cycle..pptxMODULE 1_Introduction to Data analytics and life cycle..pptx
MODULE 1_Introduction to Data analytics and life cycle..pptx
nikshaikh786
 
IOE MODULE 6.pptx
IOE MODULE 6.pptxIOE MODULE 6.pptx
IOE MODULE 6.pptx
nikshaikh786
 
MAD&PWA VIVA QUESTIONS.pdf
MAD&PWA VIVA QUESTIONS.pdfMAD&PWA VIVA QUESTIONS.pdf
MAD&PWA VIVA QUESTIONS.pdf
nikshaikh786
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
nikshaikh786
 
Module 1_ Introduction.pptx
Module 1_ Introduction.pptxModule 1_ Introduction.pptx
Module 1_ Introduction.pptx
nikshaikh786
 
Module 1_ Introduction to Mobile Computing.pptx
Module 1_  Introduction to Mobile Computing.pptxModule 1_  Introduction to Mobile Computing.pptx
Module 1_ Introduction to Mobile Computing.pptx
nikshaikh786
 
Module 2_ GSM Mobile services.pptx
Module 2_  GSM Mobile services.pptxModule 2_  GSM Mobile services.pptx
Module 2_ GSM Mobile services.pptx
nikshaikh786
 
MODULE 4_ CLUSTERING.pptx
MODULE 4_ CLUSTERING.pptxMODULE 4_ CLUSTERING.pptx
MODULE 4_ CLUSTERING.pptx
nikshaikh786
 
MODULE 5 _ Mining frequent patterns and associations.pptx
MODULE 5 _ Mining frequent patterns and associations.pptxMODULE 5 _ Mining frequent patterns and associations.pptx
MODULE 5 _ Mining frequent patterns and associations.pptx
nikshaikh786
 
Module 3_ Classification.pptx
Module 3_ Classification.pptxModule 3_ Classification.pptx
Module 3_ Classification.pptx
nikshaikh786
 
Module 2_ Introduction to Data Mining, Data Exploration and Data Pre-processi...
Module 2_ Introduction to Data Mining, Data Exploration and Data Pre-processi...Module 2_ Introduction to Data Mining, Data Exploration and Data Pre-processi...
Module 2_ Introduction to Data Mining, Data Exploration and Data Pre-processi...
nikshaikh786
 
Module 1_Data Warehousing Fundamentals.pptx
Module 1_Data Warehousing Fundamentals.pptxModule 1_Data Warehousing Fundamentals.pptx
Module 1_Data Warehousing Fundamentals.pptx
nikshaikh786
 
Module 2_ Cyber offenses & Cybercrime.pptx
Module 2_ Cyber offenses & Cybercrime.pptxModule 2_ Cyber offenses & Cybercrime.pptx
Module 2_ Cyber offenses & Cybercrime.pptx
nikshaikh786
 
Module 1- Introduction to Cybercrime.pptx
Module 1- Introduction to Cybercrime.pptxModule 1- Introduction to Cybercrime.pptx
Module 1- Introduction to Cybercrime.pptx
nikshaikh786
 
MODULE 5- EDA.pptx
MODULE 5- EDA.pptxMODULE 5- EDA.pptx
MODULE 5- EDA.pptx
nikshaikh786
 
MODULE 4-Text Analytics.pptx
MODULE 4-Text Analytics.pptxMODULE 4-Text Analytics.pptx
MODULE 4-Text Analytics.pptx
nikshaikh786
 
Module 3 - Time Series.pptx
Module 3 - Time Series.pptxModule 3 - Time Series.pptx
Module 3 - Time Series.pptx
nikshaikh786
 
Module 2_ Regression Models..pptx
Module 2_ Regression Models..pptxModule 2_ Regression Models..pptx
Module 2_ Regression Models..pptx
nikshaikh786
 
MODULE 1_Introduction to Data analytics and life cycle..pptx
MODULE 1_Introduction to Data analytics and life cycle..pptxMODULE 1_Introduction to Data analytics and life cycle..pptx
MODULE 1_Introduction to Data analytics and life cycle..pptx
nikshaikh786
 
MAD&PWA VIVA QUESTIONS.pdf
MAD&PWA VIVA QUESTIONS.pdfMAD&PWA VIVA QUESTIONS.pdf
MAD&PWA VIVA QUESTIONS.pdf
nikshaikh786
 
Ad

Recently uploaded (20)

Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
SanjeetMishra29
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
Physical and Physic-Chemical Based Optimization Methods: A Review
Physical and Physic-Chemical Based Optimization Methods: A ReviewPhysical and Physic-Chemical Based Optimization Methods: A Review
Physical and Physic-Chemical Based Optimization Methods: A Review
Journal of Soft Computing in Civil Engineering
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
Jimmy Lai
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
AI-Powered Data Management and Governance in Retail
AI-Powered Data Management and Governance in RetailAI-Powered Data Management and Governance in Retail
AI-Powered Data Management and Governance in Retail
IJDKP
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
PPT on Sattelite satellite & Radar(1).pptx
PPT on Sattelite satellite & Radar(1).pptxPPT on Sattelite satellite & Radar(1).pptx
PPT on Sattelite satellite & Radar(1).pptx
navneet19791
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
🚀 TDX Bengaluru 2025 Unwrapped: Key Highlights, Innovations & Trailblazer Tak...
SanjeetMishra29
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
Jimmy Lai
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
AI-Powered Data Management and Governance in Retail
AI-Powered Data Management and Governance in RetailAI-Powered Data Management and Governance in Retail
AI-Powered Data Management and Governance in Retail
IJDKP
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
PPT on Sattelite satellite & Radar(1).pptx
PPT on Sattelite satellite & Radar(1).pptxPPT on Sattelite satellite & Radar(1).pptx
PPT on Sattelite satellite & Radar(1).pptx
navneet19791
 

Module 2- Control Structures

  • 1. MODULE 2: CONTROL STRUCTURES CO2: Implement, test and execute programs comprising of control structures.
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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.
  • 6. INTRODUCTION TO CONTROL STRUCTURES For Example #include<stdio.h> void main() { int a; int a=5; printf(“Value of a = %d”,a); }
  • 7. 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.
  • 8. 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); }
  • 9. 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.
  • 10. 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
  • 11. 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.
  • 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); }
  • 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); }
  • 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); }
  • 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. SELECTION STRUCTURE – SWITCH CASE
  • 26. 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(); }
  • 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.
  • 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; }
  • 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. REPETITION STRUCTURE - DO WHILE LOOP
  • 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; }
  • 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. REPETITION STRUCTURE - NESTED LOOP
  • 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 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;
  • 45. REPETITION STRUCTURE- CONTINUE The continue statement is almost always used with the if...else statement.
  • 46. 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; }
  • 47. REPETITION STRUCTURE- CONTINUE When the user enters a negative number, the continue statement is executed and it skips the negative number from the calculation.
  • 48. 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; }
  • 49. 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.
  • 51. 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
  • 53. PROBLEMS Example 1: Half Pyramid of * * ** *** **** *****
  • 54. 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; }
  • 55. PROBLEMS Example 2: Half Pyramid 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: Half Pyramid 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: Inverted half 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: Inverted half 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: Full Pyramid 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; }
  翻译: