SlideShare a Scribd company logo
1 of 59
Download to read offline
1
U19CS101 – PROBLEM SOLVIG USING C
I- SEM
Presented By:
C.Ganesh., M.E., (Ph.D)
Assistant Professor
Department of ECE
Sri Eshwar college of Engineering
2
❑ Problem solving using Conditional or Selection or
Branching Statements:
❑ Structure of if, if-else, else-if ladder, nested-if, switch
constructs - Looping constructs: Structure of for, while, do-
while constructs, usage of break, return, goto and continue
keywords
UNIT II –
CONDITIONAL STATEMENTS AND LOOPING
CONSTRUCTS
1. if Statement
3
❑ The conditional branching statements help to jump from one part
of the program to another depending on whether a particular
condition is satisfied or not.
❑ The decision control statements include
❑ Simple if
❑ if-else
❑ if-else-if-ladder
❑ nested-if
❑ switch case
if Statement-Example
4
❑ The if statement is the simplest form of selection statement
that frequently used in decision making.
❑ The syntax for simple if statement is,
if - Flowchart
5
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.
If - Example
6
2. The if-else statement
7
❑ It is observed that if statement executed only when the condition
following if is true. It does nothing when the condition is false.
❑ The if-else statement take care of true as well as false conditions.
❑ It has two blocks.
❑ One block is for if and it is executed when the condition is true.
❑ The other block is for else block and it is executed when the condition
is false.
❑ The else statement cannot be used without if.
if-else -Syntax
8
❑ 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
if-else -Flowchart
9
if-else - Example
10
3. Nested if-else
11
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 the condition-1 is false, the statement-3 will be executed; otherwise it
continues to perform the second test.
❑ If the condition-2 is true, the statement-1 will be evaluated; otherwise
the statement-2 will be evaluated and then the control is transferred to the
statement-x.
3. Nested if-else - Flowchart
12
3. Nested if-else - Example
13
A commercial bank has introduced an incentive policy of giving bonus to all its deposit
holders. The policy is as follows: A bonus of 2 per cent of the balance held on 31st
December is given to every one, irrespective of their balance, and 5 per cent is given to
female account holders if their balance is more than Rs. 5000. This logic can be coded
as follows:
Dangling Else problem (Nested if-else)
14
❑ Once we start nesting if-else statements, we may encounter a classic
problem known as the dangling else.
❑ This problem is created when there is no matching else for every
if.
❑ The solution to this problem is to follow the simple rule: Always
pair an else to the most recent unpaired if in the current block
❑ Consider the example shown below from the code alignment, we
conclude that the programmer intended the else statement to be
paired with the first if.
❑ However, the compiler will pair it with the second if.
4. The if-else-if statement
15
❑ Sometimes we wish to make a multi-way decision based on several
conditions.
❑ The most general way of doing this is by using the else if variant on the if
statement.
❑ This works by cascading several comparisons.
❑ As soon as one of these gives a true result, the following statement or
block is executed and no further comparisons are performed.
❑ If none of the condition is true, the final else is executed.
❑ The final else statement is optional.
4. The if-else-if statement - Syntax
16
4. The if-else-if statement - Flowchart
17
4. The if-else-if statement - Example
18
4. The if-else-if statement - Example
19
5.Switch statement
20
❑ The switch() case statement is like the if statement that allows us to
make a decision from a number of choices.
❑ The switch statement evaluates the expression and then looks for its
value among case constants.
❑ If the value matches with a case constant, this particular case
statement is executed.
❑ If not, the default is executed.
Switch statement- Syntax
21
The expression is an integer
expression or characters. Value-1,
value-2 ..... are constants or
constant
expressions (evaluable to an
integral constant) and are known as
case labels. Each of these values
should be unique within a switch
statement. .... are statement lists and
may contain
zero or more statements. There is
no need to put braces around these
blocks. Note that labels
end with a colon (:).
Switch Statement flowchart
22
Switch Statements-Example
23
Looping (or) Iterative statements
24
❑ Loops are used to execute a group of statements (Or) block of
statements repeatedly until some condition is satisfied.
Looping - Steps
25
1. Initialization of counter
2. Testing condition
3. Execution of loop body (group of statements)
4. Modification (Increment/Decrement) of counter value
1.While Loop
26
❑ The while is an entry-controlled loop statement. The test-
condition is evaluated and if the condition is true then the body of
the loop is executed.
❑ After the execution of the body, the test condition is once again
evaluated and if it is true, the body is executed once again.
❑ This process of repeated execution of the body continues until the
test condition finally becomes false and the control is transferred
out of the loop.
❑ On exit, the program continues with the statement immediately
after the body of the loop.
1.While Loop (Pre-test/Entry controlled loop)
27
❑ The body of the loop may have one or more statements.
❑ The braces are needed only if the body contains two or more
statements.
❑ However, it is a good practice to use braces even if the body has only
one statement
❑ Steps:
1. Initialization of the counter variable.
2. Condition is checked. If condition is ‘True’
3. Body of the loop is executed
4. Modification (Increment or Decrement) of counter.
5. The steps 3,4 and 5 are executed
6. If condition is ‘False’, then control comes out of the loop and the
statements following the while loop are executed.
While Loop (Entry Controlled Loop)
28
While Loop - Example
29
Ex:1 Printing 1 to 5 numbers using while loop
#include<stdio.h>
int main()
{
int i=1; # counter initialization
while (i<=5) // condition checking
{
printf(“n%d”,i); // body of the while loop
i++; // incrementing counter
}
return 0;
}
While Loop - Example
30
Ex:2 Printing 5 to 1 numbers using while loop
#include<stdio.h>
int main()
{
int i=5; # counter initialization
while (i>=0) // condition checking
{
printf(“n%d”,i); // body of the while loop
i--; // incrementing counter
}
return 0;
}
While Loop - Example
31
Ex:3 Printing sum of ‘n’ natural numbers using while loop
#include<stdio.h>
int main()
{
int i=1; // counter initialization
int sum=0,n;
scanf(“%d”,&n);
while (i<=n) // condition checking
{
sum=sum+i; // body of the while loop
i++; // incrementing counter
}
printf(“The sum of first %d numbers is =%d”,n,sum);
printf(“The average value is %d”,sum/n);
return 0;
}
2.Do While Loop (exit controlled/post-test)
32
❑ The do-while loop is an exit controlled loop because the test condition
is evaluated at the end of the loop.
❑ The do-while loop will execute at least one time even if the condition
is false initially.
❑ Note that the test condition is enclosed in parenthesis and followed by
a semicolon.
Do While Loop – Exit Controlled Loop
33
while & do-while difference
34
While Loop - Example
Do While Loop - Example
35
Do While Loop - Example
36
While and Do While Loop - Comparison
37
While Do-while
1.Entry controlled or pre-test loop 1,Exit controlled or post-test
Loop
2. Should not be terminated with ; 2. Terminated with ;
3. If we use a single statement in
loop body, it does not need braces
for enclosing it
3. We use braces even if the loop
body contains a single statement.
4. If condition is failed at the first
time, loop body is not executed
4. Even if the condition is false for
the first time, loop is executed at
least once.
3.For Loop
38
❑ For loop is used to execute a set of statements repeatedly until a
particular condition is satisfied.
❑ It is a pre-test loop and it is used when the number of iterations of the
loop is known before the loop is entered.
The head of the loop consists of three parts.
▪ Initialization expression
▪ Test expression
▪ Update expression separated by semicolon.
Execution of for loop
1. It first evaluates the initialization code.
2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluates the increment/decrement condition and again follows
from step 2.
5. When condition expression becomes false, it exits the loop.
for Loop - Syntax
39
❑ This for loop is executed 10 times and prints the digits 0 to 9 in one
line. The three sections enclosed within parentheses must be
separated by semicolons.
❑ Note that there is no semicolon at the end of the increment section,
x = x+1.
For Loop - Flowchart
40
Steps in for-Loop
41
Flavors of for loop
42
For loop - Examples
43
For loop - Examples
44
Nested for-loop
45
❑ C allows its users to have nested loop, i.e., loops that can be placed
inside other loops.
❑ Although this feature will work with any loop like while, do-while
and for, it is most commonly used in for loop because this is easier to
control.
Nested for loop - Example
46
Nested for Loop - Example
47
Nested for loop - Example
48
Nested while loop - Example
49
Nested while loop - Example
50
The break statement
51
❑ The keyword break allows the programmers to terminate the loop.
❑ The break skips from the loop or block in which it is defined.
❑ The control automatically goes to the first statement after the loop or
block.
❑ The break statement is widely used with for loop, while loop, and do while
loop.
❑ In switch statement if the break statement is missing then every case
from the matched case label till the end of the switch, including the
default, is executed.
‘break’ statement
52
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
{
break;
}
printf("%dn",i);
}
return 0;
}
Output:
1
2
break statement
53
The continue statement
54
❑ The continue statement is exactly opposite to break.
❑ The continue statement is used to continue next iteration of
loop statement.
❑ When it occurs in the loop it does not terminate but it skip the
statement after this statement.
The continue statement
55
The continue statement: Example
56
Break and Continue : Comparison
57
THE goto STATEMENT
58
❑ This statement does not require any condition
❑ This statement passes control anywhere in the program, that is,
control is transferred to another part of the program without
testing any condition.
❑ The user has to define goto statement as follows.
❑ Where, the label name must start with any character.
❑ Label is the position where the condition is to be transferred.
goto statement - Example
59

More Related Content

Similar to UNIT 2 PPT.pdf

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 CSowmya Jyothi
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c languagesneha2494
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsLovelitJose
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxSKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Abou Bakr Ashraf
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loopsManzoor ALam
 
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.pptManojKhadilkar1
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
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 statementsRai University
 
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 statementsRai University
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programmingArchana Gopinath
 

Similar to UNIT 2 PPT.pdf (20)

M C6java6
M C6java6M C6java6
M C6java6
 
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
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
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
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 
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
 
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
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Control structure
Control structureControl structure
Control structure
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 

Recently uploaded

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 

Recently uploaded (20)

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 

UNIT 2 PPT.pdf

  • 1. 1 U19CS101 – PROBLEM SOLVIG USING C I- SEM Presented By: C.Ganesh., M.E., (Ph.D) Assistant Professor Department of ECE Sri Eshwar college of Engineering
  • 2. 2 ❑ Problem solving using Conditional or Selection or Branching Statements: ❑ Structure of if, if-else, else-if ladder, nested-if, switch constructs - Looping constructs: Structure of for, while, do- while constructs, usage of break, return, goto and continue keywords UNIT II – CONDITIONAL STATEMENTS AND LOOPING CONSTRUCTS
  • 3. 1. if Statement 3 ❑ The conditional branching statements help to jump from one part of the program to another depending on whether a particular condition is satisfied or not. ❑ The decision control statements include ❑ Simple if ❑ if-else ❑ if-else-if-ladder ❑ nested-if ❑ switch case
  • 4. if Statement-Example 4 ❑ The if statement is the simplest form of selection statement that frequently used in decision making. ❑ The syntax for simple if statement is,
  • 5. if - Flowchart 5 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.
  • 7. 2. The if-else statement 7 ❑ It is observed that if statement executed only when the condition following if is true. It does nothing when the condition is false. ❑ The if-else statement take care of true as well as false conditions. ❑ It has two blocks. ❑ One block is for if and it is executed when the condition is true. ❑ The other block is for else block and it is executed when the condition is false. ❑ The else statement cannot be used without if.
  • 8. if-else -Syntax 8 ❑ 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
  • 11. 3. Nested if-else 11 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 the condition-1 is false, the statement-3 will be executed; otherwise it continues to perform the second test. ❑ If the condition-2 is true, the statement-1 will be evaluated; otherwise the statement-2 will be evaluated and then the control is transferred to the statement-x.
  • 12. 3. Nested if-else - Flowchart 12
  • 13. 3. Nested if-else - Example 13 A commercial bank has introduced an incentive policy of giving bonus to all its deposit holders. The policy is as follows: A bonus of 2 per cent of the balance held on 31st December is given to every one, irrespective of their balance, and 5 per cent is given to female account holders if their balance is more than Rs. 5000. This logic can be coded as follows:
  • 14. Dangling Else problem (Nested if-else) 14 ❑ Once we start nesting if-else statements, we may encounter a classic problem known as the dangling else. ❑ This problem is created when there is no matching else for every if. ❑ The solution to this problem is to follow the simple rule: Always pair an else to the most recent unpaired if in the current block ❑ Consider the example shown below from the code alignment, we conclude that the programmer intended the else statement to be paired with the first if. ❑ However, the compiler will pair it with the second if.
  • 15. 4. The if-else-if statement 15 ❑ Sometimes we wish to make a multi-way decision based on several conditions. ❑ The most general way of doing this is by using the else if variant on the if statement. ❑ This works by cascading several comparisons. ❑ As soon as one of these gives a true result, the following statement or block is executed and no further comparisons are performed. ❑ If none of the condition is true, the final else is executed. ❑ The final else statement is optional.
  • 16. 4. The if-else-if statement - Syntax 16
  • 17. 4. The if-else-if statement - Flowchart 17
  • 18. 4. The if-else-if statement - Example 18
  • 19. 4. The if-else-if statement - Example 19
  • 20. 5.Switch statement 20 ❑ The switch() case statement is like the if statement that allows us to make a decision from a number of choices. ❑ The switch statement evaluates the expression and then looks for its value among case constants. ❑ If the value matches with a case constant, this particular case statement is executed. ❑ If not, the default is executed.
  • 21. Switch statement- Syntax 21 The expression is an integer expression or characters. Value-1, value-2 ..... are constants or constant expressions (evaluable to an integral constant) and are known as case labels. Each of these values should be unique within a switch statement. .... are statement lists and may contain zero or more statements. There is no need to put braces around these blocks. Note that labels end with a colon (:).
  • 24. Looping (or) Iterative statements 24 ❑ Loops are used to execute a group of statements (Or) block of statements repeatedly until some condition is satisfied.
  • 25. Looping - Steps 25 1. Initialization of counter 2. Testing condition 3. Execution of loop body (group of statements) 4. Modification (Increment/Decrement) of counter value
  • 26. 1.While Loop 26 ❑ The while is an entry-controlled loop statement. The test- condition is evaluated and if the condition is true then the body of the loop is executed. ❑ After the execution of the body, the test condition is once again evaluated and if it is true, the body is executed once again. ❑ This process of repeated execution of the body continues until the test condition finally becomes false and the control is transferred out of the loop. ❑ On exit, the program continues with the statement immediately after the body of the loop.
  • 27. 1.While Loop (Pre-test/Entry controlled loop) 27 ❑ The body of the loop may have one or more statements. ❑ The braces are needed only if the body contains two or more statements. ❑ However, it is a good practice to use braces even if the body has only one statement ❑ Steps: 1. Initialization of the counter variable. 2. Condition is checked. If condition is ‘True’ 3. Body of the loop is executed 4. Modification (Increment or Decrement) of counter. 5. The steps 3,4 and 5 are executed 6. If condition is ‘False’, then control comes out of the loop and the statements following the while loop are executed.
  • 28. While Loop (Entry Controlled Loop) 28
  • 29. While Loop - Example 29 Ex:1 Printing 1 to 5 numbers using while loop #include<stdio.h> int main() { int i=1; # counter initialization while (i<=5) // condition checking { printf(“n%d”,i); // body of the while loop i++; // incrementing counter } return 0; }
  • 30. While Loop - Example 30 Ex:2 Printing 5 to 1 numbers using while loop #include<stdio.h> int main() { int i=5; # counter initialization while (i>=0) // condition checking { printf(“n%d”,i); // body of the while loop i--; // incrementing counter } return 0; }
  • 31. While Loop - Example 31 Ex:3 Printing sum of ‘n’ natural numbers using while loop #include<stdio.h> int main() { int i=1; // counter initialization int sum=0,n; scanf(“%d”,&n); while (i<=n) // condition checking { sum=sum+i; // body of the while loop i++; // incrementing counter } printf(“The sum of first %d numbers is =%d”,n,sum); printf(“The average value is %d”,sum/n); return 0; }
  • 32. 2.Do While Loop (exit controlled/post-test) 32 ❑ The do-while loop is an exit controlled loop because the test condition is evaluated at the end of the loop. ❑ The do-while loop will execute at least one time even if the condition is false initially. ❑ Note that the test condition is enclosed in parenthesis and followed by a semicolon.
  • 33. Do While Loop – Exit Controlled Loop 33
  • 34. while & do-while difference 34 While Loop - Example
  • 35. Do While Loop - Example 35
  • 36. Do While Loop - Example 36
  • 37. While and Do While Loop - Comparison 37 While Do-while 1.Entry controlled or pre-test loop 1,Exit controlled or post-test Loop 2. Should not be terminated with ; 2. Terminated with ; 3. If we use a single statement in loop body, it does not need braces for enclosing it 3. We use braces even if the loop body contains a single statement. 4. If condition is failed at the first time, loop body is not executed 4. Even if the condition is false for the first time, loop is executed at least once.
  • 38. 3.For Loop 38 ❑ For loop is used to execute a set of statements repeatedly until a particular condition is satisfied. ❑ It is a pre-test loop and it is used when the number of iterations of the loop is known before the loop is entered. The head of the loop consists of three parts. ▪ Initialization expression ▪ Test expression ▪ Update expression separated by semicolon. Execution of for loop 1. It first evaluates the initialization code. 2. Then it checks the condition expression. 3. If it is true, it executes the for-loop body. 4. Then it evaluates the increment/decrement condition and again follows from step 2. 5. When condition expression becomes false, it exits the loop.
  • 39. for Loop - Syntax 39 ❑ This for loop is executed 10 times and prints the digits 0 to 9 in one line. The three sections enclosed within parentheses must be separated by semicolons. ❑ Note that there is no semicolon at the end of the increment section, x = x+1.
  • 40. For Loop - Flowchart 40
  • 42. Flavors of for loop 42
  • 43. For loop - Examples 43
  • 44. For loop - Examples 44
  • 45. Nested for-loop 45 ❑ C allows its users to have nested loop, i.e., loops that can be placed inside other loops. ❑ Although this feature will work with any loop like while, do-while and for, it is most commonly used in for loop because this is easier to control.
  • 46. Nested for loop - Example 46
  • 47. Nested for Loop - Example 47
  • 48. Nested for loop - Example 48
  • 49. Nested while loop - Example 49
  • 50. Nested while loop - Example 50
  • 51. The break statement 51 ❑ The keyword break allows the programmers to terminate the loop. ❑ The break skips from the loop or block in which it is defined. ❑ The control automatically goes to the first statement after the loop or block. ❑ The break statement is widely used with for loop, while loop, and do while loop. ❑ In switch statement if the break statement is missing then every case from the matched case label till the end of the switch, including the default, is executed.
  • 52. ‘break’ statement 52 #include <stdio.h> int main() { int i; for(i=1;i<=5;i++) { if(i==3) { break; } printf("%dn",i); } return 0; } Output: 1 2
  • 54. The continue statement 54 ❑ The continue statement is exactly opposite to break. ❑ The continue statement is used to continue next iteration of loop statement. ❑ When it occurs in the loop it does not terminate but it skip the statement after this statement.
  • 57. Break and Continue : Comparison 57
  • 58. THE goto STATEMENT 58 ❑ This statement does not require any condition ❑ This statement passes control anywhere in the program, that is, control is transferred to another part of the program without testing any condition. ❑ The user has to define goto statement as follows. ❑ Where, the label name must start with any character. ❑ Label is the position where the condition is to be transferred.
  • 59. goto statement - Example 59