SlideShare a Scribd company logo
An Autonomous Institute affiliate to VTU
Accredited by National Assessment & Accreditation
council (NAAC) with A grade
Department of Electrical and Electronics engineering
A Technical Seminar on
C Programming - Decision making,
Looping
Submitted by
MURALIDHAR R
Electrical and Electronics Engineering
Submitted by
MURALIDHAR R
 Decision Making
o Decision making with if statement
 Simple if statement
 The if…else statement
 Nested if…else statement
o Switch statement
 Looping
o Body of loop
o Entry-controlled loop
o Exit-controlled loop
o Formats of loop
 While Loop
 do…while loop
 for loop
o break statement
o continue statement
o Method of Structured Programming
 Sequence
 Selection
 iteration
 During programming, we have a number of situations
where we may have to change the order of execution of
statements based on certain conditions. This involves a
kind of decision making to whether a particular
condition has occurred or not and then direct to
computer to execute certain statements accordingly.
 C language has such decision making capabilities by
supporting the following statements:
If Statement
Switch statement
 These statements are popularly known as decision
making statements. Since these statements control the
flow of execution, they are also known as control
statements.
 The if statement is a powerful decision making
statement and is used to control the flow of execution
of statements. It is basically a two-way decision
statement and is used in conjunction with an
expression. It takes the following form:
if(test expression)
 It allows the computer to evaluate the expression first
then, depending on whether the value of the
expression is ‘true’ (or non-zero) or ‘false’ (zero), it
transfers the control to a particular statement.
 The if statement may be implemented in different
forms depending on the complexity of conditions to be
tested. The different forms are:
1. Simple if statement
2. if…else statement
3. Nested if…else statement
 The general form of a simple if
statement is
if(test expression)
{
Statement-block;
}
Statement-x;
 The ‘statement-block’ may be a single
statement or a group of statements. If
the test expression is true, the
statement-block will be executed;
otherwise the statement-block will be
skipped and the execution will jump to
the statement-x.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf(“n Enter your age : “);
scanf(“%d”,&age);
if ( age < 0 )
printf (“Negative age given…“);
if ( age > 0 )
printf (“Your age is %d”,age);
getch();
}
Input & Output:
Enter your age: 30
Your age is 30
 The if…else statement is an extension of the
simple if statement. The general form is
if(test expression)
{
True-block statement(s);
}
else
{
False-block statement(s);
}
Statement-x;
 If the test expression is true, then the true-
block statement(s), immediately following
the if statements are executed; otherwise,
the false-block statement(s) are executed. In
either case, either true-block or false-block
will be executed, not both.
Example: Write a C program to find maximum
number from two integer numbers.
#include<stdio.h>
#include<conio.h>
void main() {
int a, b;
clrscr();
printf(“Enter first number : “); scanf(“%d”,&a);
printf(“Enter second number : “);
scanf(“%d”,&b);
if ( a > b )
{ printf (“n First number = %d is maximum”,a); }
Else
{ printf (“n Second number = %d is
maximum”,b);
}
getch();
}
Output: Enter first number: 54 Enter second
number: 25 First number = 54 is maximum
 One if…else statement written inside another
if…else statement is known as nested if…else
statement. When a series of decisions are
involved, we may have to use more than one
if…else statement in nested form as shown
below:
if(test condition-1)
{
if(test condition-2)
{
Statement-1;
}
else
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-x;
Example: Write a C program to find maximum
number from three integer numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
printf(“Enter first number : “);
scanf(“%d”,&a);
printf(“Enter second number : “);
scanf(“%d”,&b);
printf(“Enter third number : “);
scanf(“%d”,&c);
if ( a > b && a > c)
{ printf (“n First number = %d is maximum”,a);
} else
{ if ( b > c ) { printf (“n Second number = %d
is maximum”,b); } else
{ printf (“n Third number = %d is
maximum”,c); } }
getch(); }
Output: Enter first number: 54 Enter second
number: 99 Enter second number: 85
Second number = 99 is maximum
 C has a built-in multi way decision
statement known as a switch. The switch
statement tests the value of a given
variable (or expression) against a list of
case values and when a match is found, a
block of statements associated, with that
case is executed. The general form of the
switch statement is as shown below:
Switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
……
……
default:
default-block;
break;
}
Statement-x;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“n Enter any number :”);
scanf(“%d”, &a);
switch(a)
{
case 1 :
printf(“n a is 1”);
break;
case 2 :
printf(“n a is 2”);
break;
default :
printf(“n Number other than 1 and 2”);
break;
}
getch();
}
 The switch expression must be an integral type.
 Case labels must be constants or constant expressions.
 Case labels must be unique. No two labels can have the
same value.
 Case labels must end with colon.
 The default label is optional. If present, it will be
executed when the expression does not find a matching
case labels.
 There can be at most one default label.
 The default may be placed anywhere but usually
placed at the end.
 It is permitted to nest switch statements.
 “When sequences of statements are executed repeatedly up to some condition then it is
known as Program Loop.”
 A loop consists mainly two parts:
Body of loop This part contains the sequences of statements, which are to be executed
repeatedly.
 Control statement
This part contains certain conditions, which indicates how many times we have to execute
the body of loop. The control statement should be writing carefully otherwise it may possible
your loop is executed infinite times which is known as Infinite Loop. Loop contains one or
more variable, which control the execution of loop that is known as the Loop Counter.
Loop is executed basically in following steps:
1. Setting and initialization of Loop Counter.
2. 2. Test the condition.
3. 3. Executing the body of loop.
4. 4. Incrementing or decrementing the Loop Counter value.
In above steps, 2nd and 3rd steps may be executed interchangeably that means either we can
perform step 2 first or step 3 first.
In general there are two types of loops: Entry Controlled loop and Exit controlled loop.
 In entry-controlled loop we first check the condition
and then the body of loop is executed. If at first time
condition is false then the body of loop and not
executed any time. In C programming language while
loop and for loop is example of entry-controlled loop.
 In exit-controlled loop we first execute the body of the
loop and then check the condition and if condition is
true then next again executed the body of the loop
otherwise not. In exit controlled loop it is sure the body
of loop is executed once. In C programming language
do while loop is example of the exit-controlled loop.
 In ‘C’ language we have three loops
format:
while loop
for loop
do-while loop
 While Loop
while loop is entry-controlled loop, in
which first condition is evaluated and if
condition is true the body of loop is executed.
After executing the body of loop the test
condition is again evaluated and if it is true then
again body of loop is executed. This process is
going to continue up to your test condition is
true. If the test condition is false on initial stage
then body of loop is never executed.
The format of while loop is:
while(test condition)
{
body of loop
}
 Body of loop has one or more statements.
Here the curly bracket is not necessary if
body of loop contains only one statement.
 As we known that ‘while’ loop is entry-
controlled loop in which test condition is
evaluated first and if test condition is true
then body of loop is executed. If test
condition is false on first time then body of
loop is never executed. But sometimes we
have such a condition in which it is
mandatory to execute the body of loop once
whether test condition is true or false. At
that time we have to use do-while loop. do-
while loop is exit-controlled loop, in which
first condition body of loop is executed first
and then the test condition is evaluated and
if the test condition is true then again body
of loop is executed. This process is repeated
continuously till test condition is true. Here
the body of loop must be executed at least
once.
The format of do-while loop is:
do
{
body of loop
}
while (test condition);
 for loop is entry-controlled loop, in which first
condition is evaluated and if condition is true
the body of loop is executed. After executing the
body of loop the test condition is again
evaluated and if it is true then again body of
loop is executed. This process is going to
continue up to your test condition is true. If the
test condition is false on initial stage then body
of loop is never executed. The format of for loop
is:
for (initialization ; test-condition ; increment-
decrement)
{
body of loop
}
 In above format the first line contain
initialization, test-condition, and increment-
decrement portion that means in ‘for’ loop
initialization, condition and
increment/decrement is done in only one line.
First the loop counter is initialized. After
initialization the test condition is evaluated and
if it true then body of loop is executed and after
execution of body of loop increment or
decrement of loop counter is done. Again the
test condition is evaluated and it is true then the
same process is done again. This is continuing
till the test condition is true. The flow chart of
the for loop can be drawn in Fig. 5
 break statement is used to terminate the loop. When
computer execute break statement the control
automatically goes to next immediate statement after
loop. We cannot transfer the control at any other place.
When you put break in nested loop then the only one
loop in which break statement is encounter is
terminated.
 During loop operations, it may be necessary to skip the
part of the body of the loop during certain condition.
We are using continue statement to skip the portion of
loop. For example we are determining the square root
of any number and print it. But our condition is that if
number is negative then we don’t want to determine
square root. At that time we can use continue
statement to skip the body of loop for negative
number.
 Large program are difficult to understand. Hence the large programs are written using
three logic structures:
1. Sequence
2. Selection
3 . Iteration
 A sequence construct is a set of instructions serially performed without any looping. The
normal flow of control is from top to bottom and from left to right unless there is an
iteration construct. Thus the first instruction in the program is executed first, then
control passes to the next instruction that sequence.
 A selection construct involves branching but the branches join before the exit point. The
selection is required and the flow of control depends on the selection. For example, if
statement, if…else statement and switch statement in C language.
 An iteration construct contains loops in it but has only one exit point. This construct
allows repeated execution of a set of codes without repeating codes in the program. It
saves the trouble of writing the same codes again and again in the program. For
example, while loop, do..while loop and for loop in C language.
C  Programming - Decision making, Looping
Ad

More Related Content

Similar to C Programming - Decision making, Looping (20)

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
 
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
 
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
 
UNIT 2 PPT.pdf
UNIT 2 PPT.pdfUNIT 2 PPT.pdf
UNIT 2 PPT.pdf
DhanushKumar610673
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
Hossain Md Shakhawat
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
 
control-statements detailed presentation
control-statements detailed presentationcontrol-statements detailed presentation
control-statements detailed presentation
gayathripcs
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
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
 
M C6java6
M C6java6M C6java6
M C6java6
mbruggen
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
baabtra.com - No. 1 supplier of quality freshers
 
control-statements, control-statements, control statement
control-statements, control-statements, control statementcontrol-statements, control-statements, control statement
control-statements, control-statements, control statement
crrpavankumar
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
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
 
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
 
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
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
TANUJ ⠀
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
JavvajiVenkat
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
JayBhavsar68
 
control-statements detailed presentation
control-statements detailed presentationcontrol-statements detailed presentation
control-statements detailed presentation
gayathripcs
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
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 Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
control-statements, control-statements, control statement
control-statements, control-statements, control statementcontrol-statements, control-statements, control statement
control-statements, control-statements, control statement
crrpavankumar
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 

Recently uploaded (20)

Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
Comprehensive Incident Management System for Enhanced Safety Reporting
Comprehensive Incident Management System for Enhanced Safety ReportingComprehensive Incident Management System for Enhanced Safety Reporting
Comprehensive Incident Management System for Enhanced Safety Reporting
EHA Soft Solutions
 
Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025
Phil Eaton
 
Unit Two - Java Architecture and OOPS
Unit Two  -   Java Architecture and OOPSUnit Two  -   Java Architecture and OOPS
Unit Two - Java Architecture and OOPS
Nabin Dhakal
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
Lumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free CodeLumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free Code
raheemk1122g
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Applying AI in Marketo: Practical Strategies and Implementation
Applying AI in Marketo: Practical Strategies and ImplementationApplying AI in Marketo: Practical Strategies and Implementation
Applying AI in Marketo: Practical Strategies and Implementation
BradBedford3
 
How to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryErrorHow to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
Digital Twins Software Service in Belfast
Digital Twins Software Service in BelfastDigital Twins Software Service in Belfast
Digital Twins Software Service in Belfast
julia smits
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World ExamplesMastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
jamescantor38
 
Robotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptxRobotic Process Automation (RPA) Software Development Services.pptx
Robotic Process Automation (RPA) Software Development Services.pptx
julia smits
 
Comprehensive Incident Management System for Enhanced Safety Reporting
Comprehensive Incident Management System for Enhanced Safety ReportingComprehensive Incident Management System for Enhanced Safety Reporting
Comprehensive Incident Management System for Enhanced Safety Reporting
EHA Soft Solutions
 
Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025Memory Management and Leaks in Postgres from pgext.day 2025
Memory Management and Leaks in Postgres from pgext.day 2025
Phil Eaton
 
Unit Two - Java Architecture and OOPS
Unit Two  -   Java Architecture and OOPSUnit Two  -   Java Architecture and OOPS
Unit Two - Java Architecture and OOPS
Nabin Dhakal
 
Medical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk ScoringMedical Device Cybersecurity Threat & Risk Scoring
Medical Device Cybersecurity Threat & Risk Scoring
ICS
 
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo Ltd. - Introduction - Mobile application, web, custom software develo...
Codingo
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
Wilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For WindowsWilcom Embroidery Studio Crack 2025 For Windows
Wilcom Embroidery Studio Crack 2025 For Windows
Google
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
Lumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free CodeLumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free Code
raheemk1122g
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Applying AI in Marketo: Practical Strategies and Implementation
Applying AI in Marketo: Practical Strategies and ImplementationApplying AI in Marketo: Practical Strategies and Implementation
Applying AI in Marketo: Practical Strategies and Implementation
BradBedford3
 
How to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryErrorHow to Troubleshoot 9 Types of OutOfMemoryError
How to Troubleshoot 9 Types of OutOfMemoryError
Tier1 app
 
Autodesk Inventor Crack (2025) Latest
Autodesk Inventor    Crack (2025) LatestAutodesk Inventor    Crack (2025) Latest
Autodesk Inventor Crack (2025) Latest
Google
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studiesTroubleshooting JVM Outages – 3 Fortune 500 case studies
Troubleshooting JVM Outages – 3 Fortune 500 case studies
Tier1 app
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
Digital Twins Software Service in Belfast
Digital Twins Software Service in BelfastDigital Twins Software Service in Belfast
Digital Twins Software Service in Belfast
julia smits
 
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
!%& IDM Crack with Internet Download Manager 6.42 Build 32 >
Ranking Google
 
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World ExamplesMastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
jamescantor38
 
Ad

C Programming - Decision making, Looping

  • 1. An Autonomous Institute affiliate to VTU Accredited by National Assessment & Accreditation council (NAAC) with A grade Department of Electrical and Electronics engineering A Technical Seminar on C Programming - Decision making, Looping Submitted by MURALIDHAR R Electrical and Electronics Engineering
  • 3.  Decision Making o Decision making with if statement  Simple if statement  The if…else statement  Nested if…else statement o Switch statement  Looping o Body of loop o Entry-controlled loop o Exit-controlled loop o Formats of loop  While Loop  do…while loop  for loop o break statement o continue statement o Method of Structured Programming  Sequence  Selection  iteration
  • 4.  During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions. This involves a kind of decision making to whether a particular condition has occurred or not and then direct to computer to execute certain statements accordingly.  C language has such decision making capabilities by supporting the following statements: If Statement Switch statement  These statements are popularly known as decision making statements. Since these statements control the flow of execution, they are also known as control statements.
  • 5.  The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two-way decision statement and is used in conjunction with an expression. It takes the following form: if(test expression)  It allows the computer to evaluate the expression first then, depending on whether the value of the expression is ‘true’ (or non-zero) or ‘false’ (zero), it transfers the control to a particular statement.  The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are: 1. Simple if statement 2. if…else statement 3. Nested if…else statement
  • 6.  The general form of a simple if statement is if(test expression) { Statement-block; } Statement-x;  The ‘statement-block’ may be a single statement or a group of statements. If the test expression is true, the statement-block will be executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x. #include<stdio.h> #include<conio.h> void main() { int age; clrscr(); printf(“n Enter your age : “); scanf(“%d”,&age); if ( age < 0 ) printf (“Negative age given…“); if ( age > 0 ) printf (“Your age is %d”,age); getch(); } Input & Output: Enter your age: 30 Your age is 30
  • 7.  The if…else statement is an extension of the simple if statement. The general form is if(test expression) { True-block statement(s); } else { False-block statement(s); } Statement-x;  If the test expression is true, then the true- block statement(s), immediately following the if statements are executed; otherwise, the false-block statement(s) are executed. In either case, either true-block or false-block will be executed, not both. Example: Write a C program to find maximum number from two integer numbers. #include<stdio.h> #include<conio.h> void main() { int a, b; clrscr(); printf(“Enter first number : “); scanf(“%d”,&a); printf(“Enter second number : “); scanf(“%d”,&b); if ( a > b ) { printf (“n First number = %d is maximum”,a); } Else { printf (“n Second number = %d is maximum”,b); } getch(); } Output: Enter first number: 54 Enter second number: 25 First number = 54 is maximum
  • 8.  One if…else statement written inside another if…else statement is known as nested if…else statement. When a series of decisions are involved, we may have to use more than one if…else statement in nested form as shown below: if(test condition-1) { if(test condition-2) { Statement-1; } else { Statement-2; } } else { Statement-3; } Statement-x; Example: Write a C program to find maximum number from three integer numbers. #include<stdio.h> #include<conio.h> void main() { int a, b, c; clrscr(); printf(“Enter first number : “); scanf(“%d”,&a); printf(“Enter second number : “); scanf(“%d”,&b); printf(“Enter third number : “); scanf(“%d”,&c); if ( a > b && a > c) { printf (“n First number = %d is maximum”,a); } else { if ( b > c ) { printf (“n Second number = %d is maximum”,b); } else { printf (“n Third number = %d is maximum”,c); } } getch(); } Output: Enter first number: 54 Enter second number: 99 Enter second number: 85 Second number = 99 is maximum
  • 9.  C has a built-in multi way decision statement known as a switch. The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated, with that case is executed. The general form of the switch statement is as shown below: Switch(expression) { case value-1: block-1; break; case value-2: block-2; break; …… …… default: default-block; break; } Statement-x; Example: #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf(“n Enter any number :”); scanf(“%d”, &a); switch(a) { case 1 : printf(“n a is 1”); break; case 2 : printf(“n a is 2”); break; default : printf(“n Number other than 1 and 2”); break; } getch(); }
  • 10.  The switch expression must be an integral type.  Case labels must be constants or constant expressions.  Case labels must be unique. No two labels can have the same value.  Case labels must end with colon.  The default label is optional. If present, it will be executed when the expression does not find a matching case labels.  There can be at most one default label.  The default may be placed anywhere but usually placed at the end.  It is permitted to nest switch statements.
  • 11.  “When sequences of statements are executed repeatedly up to some condition then it is known as Program Loop.”  A loop consists mainly two parts: Body of loop This part contains the sequences of statements, which are to be executed repeatedly.  Control statement This part contains certain conditions, which indicates how many times we have to execute the body of loop. The control statement should be writing carefully otherwise it may possible your loop is executed infinite times which is known as Infinite Loop. Loop contains one or more variable, which control the execution of loop that is known as the Loop Counter. Loop is executed basically in following steps: 1. Setting and initialization of Loop Counter. 2. 2. Test the condition. 3. 3. Executing the body of loop. 4. 4. Incrementing or decrementing the Loop Counter value. In above steps, 2nd and 3rd steps may be executed interchangeably that means either we can perform step 2 first or step 3 first. In general there are two types of loops: Entry Controlled loop and Exit controlled loop.
  • 12.  In entry-controlled loop we first check the condition and then the body of loop is executed. If at first time condition is false then the body of loop and not executed any time. In C programming language while loop and for loop is example of entry-controlled loop.
  • 13.  In exit-controlled loop we first execute the body of the loop and then check the condition and if condition is true then next again executed the body of the loop otherwise not. In exit controlled loop it is sure the body of loop is executed once. In C programming language do while loop is example of the exit-controlled loop.
  • 14.  In ‘C’ language we have three loops format: while loop for loop do-while loop  While Loop while loop is entry-controlled loop, in which first condition is evaluated and if condition is true the body of loop is executed. After executing the body of loop the test condition is again evaluated and if it is true then again body of loop is executed. This process is going to continue up to your test condition is true. If the test condition is false on initial stage then body of loop is never executed. The format of while loop is: while(test condition) { body of loop }  Body of loop has one or more statements. Here the curly bracket is not necessary if body of loop contains only one statement.
  • 15.  As we known that ‘while’ loop is entry- controlled loop in which test condition is evaluated first and if test condition is true then body of loop is executed. If test condition is false on first time then body of loop is never executed. But sometimes we have such a condition in which it is mandatory to execute the body of loop once whether test condition is true or false. At that time we have to use do-while loop. do- while loop is exit-controlled loop, in which first condition body of loop is executed first and then the test condition is evaluated and if the test condition is true then again body of loop is executed. This process is repeated continuously till test condition is true. Here the body of loop must be executed at least once. The format of do-while loop is: do { body of loop } while (test condition);
  • 16.  for loop is entry-controlled loop, in which first condition is evaluated and if condition is true the body of loop is executed. After executing the body of loop the test condition is again evaluated and if it is true then again body of loop is executed. This process is going to continue up to your test condition is true. If the test condition is false on initial stage then body of loop is never executed. The format of for loop is: for (initialization ; test-condition ; increment- decrement) { body of loop }  In above format the first line contain initialization, test-condition, and increment- decrement portion that means in ‘for’ loop initialization, condition and increment/decrement is done in only one line. First the loop counter is initialized. After initialization the test condition is evaluated and if it true then body of loop is executed and after execution of body of loop increment or decrement of loop counter is done. Again the test condition is evaluated and it is true then the same process is done again. This is continuing till the test condition is true. The flow chart of the for loop can be drawn in Fig. 5
  • 17.  break statement is used to terminate the loop. When computer execute break statement the control automatically goes to next immediate statement after loop. We cannot transfer the control at any other place. When you put break in nested loop then the only one loop in which break statement is encounter is terminated.
  • 18.  During loop operations, it may be necessary to skip the part of the body of the loop during certain condition. We are using continue statement to skip the portion of loop. For example we are determining the square root of any number and print it. But our condition is that if number is negative then we don’t want to determine square root. At that time we can use continue statement to skip the body of loop for negative number.
  • 19.  Large program are difficult to understand. Hence the large programs are written using three logic structures: 1. Sequence 2. Selection 3 . Iteration  A sequence construct is a set of instructions serially performed without any looping. The normal flow of control is from top to bottom and from left to right unless there is an iteration construct. Thus the first instruction in the program is executed first, then control passes to the next instruction that sequence.  A selection construct involves branching but the branches join before the exit point. The selection is required and the flow of control depends on the selection. For example, if statement, if…else statement and switch statement in C language.  An iteration construct contains loops in it but has only one exit point. This construct allows repeated execution of a set of codes without repeating codes in the program. It saves the trouble of writing the same codes again and again in the program. For example, while loop, do..while loop and for loop in C language.
  翻译: