SlideShare a Scribd company logo
1 of 41
Control Flow in C
Control Flow
The control-flow of a language specify the order in which
computations are performed.
Statements: An expression such as x = 0 or i++ or printf(...)
becomes a statement when it is followed by a semicolon, as in
x = 0;
i++;
printf(...);
In C, the semicolon is a statement terminator, rather than a separator
Blocks or Statement Blocks: In C, any sequence of statement
can be grouped together by enclosing pair of curly braces.
Inside statement block variable declaration is allowed.
There is no semicolon after the right brace that ends a block.
2
Statement Blocks (cont..)
Statement block introduces a new scope in the program.
A scope is part of the program within which a variable remains
defined.
Every function has a function body consisting of s set of one or more
statements, i.e. a statement block, due to this reason , every function
body is confined within a pair of curly braces.
3
Program-1
#include<stdio.h>
main(){
int a=3;
{
int a=10;
printf(“a=%d",a);
}
printf(“na=%d",a);
}
Program-2
#include<stdio.h>
main(){
int a=3;
{
int b=10;
printf(“a=%d",a);
}
printf(“nb=%d",b);
}
a=10
a=3
What will be the output ?
main()
{
int x=40;
{
int x=20;
printf(“x=%d",x);
}
printf(“nx=%d",x);
}
 
4
x=20
x=40
Flow control Statements
Following are some constructs for controlling the flow of
execution in C:
1. if,
2. if – else,
3. Nested if and
4. if – else if
5. switch
6. conditional operator
7. goto
5 08/23/15
IF Statements
Syntax:
if(expression)
statement
• The expression is evaluated; if it is true (that is, if expression has a
non-zero value), statementis executed.
• Note: here statement may be single statement or Statement
Block.Just like
if(expression)
{
stmt 1;
stmt 2;
………..
}
6 08/23/15
IF Statements Example
7 08/23/15
IF Statements Example
8 08/23/15
Since an if tests the numeric value of an expression, certain
coding shortcuts are possible. The most obvious is writing
if (expression) instead of if (expression != 0)
in last example if(a%2!=0) can be written as if(a%2)
Following three statements are functionally equevalent
if(x)
if(x!=0)
if(!(x==0))
9 08/23/15
IF - ElSE Statements
if (expression)
statement1
else
statement2
 The expression is evaluated; if it is true (that is, if expression has
a non-zero value), statement1is executed. If it is false (expression
is zero) statement2is executed.
10 08/23/15
IF - ElSE Statements
if (n > 0)
if (a > b)
z = a;
else
z = b;
the else goes to the inner if, as we have shown by indentation. If that isn't what you
want, braces must be used to force the proper association:
if (n > 0) {
if (a > b)
z = a;
}
else
z = b;
11 08/23/15
if - else Example
12 08/23/15
if - else Example
13 08/23/15
NESTED IF Statements
14 08/23/15
NESTED IF Example
15 08/23/15
IF – ELSE IF Statements
16 08/23/15
IF – ELSE IF Statements
This sequence of if statements is the most general way of
writing a multi-way decision.
The expressions are evaluated in order; if an expression is
true, the statement associated with it is executed, and this
terminates the whole chain.
The last else part handles the “none of the above'' or default
case where none of the other conditions is satisfied.
Sometimes there is no explicit action for the default; in that
case the trailing
else can be omitted, or it may be used for error checking to
catch an ``impossible'' condition.
17 08/23/15
IF – ELSE IF Example
18 08/23/15
IF – ELSE IF Example
19 08/23/15
IF – ELSE IF Example
20 08/23/15
Switch Statements
Switch is used to Select one choice (case) from many cases.
21 08/23/15
Switch-Case Example1
22 08/23/15
Switch-Case Example2
23 08/23/15
Switch-Case Related Rules
24 08/23/15
Switch-Case Related Rules
25 08/23/15
Example3
26 08/23/15
What would be the o/p of the following program?
27
main()
{
int i=4;
switch(i)
{
default:
printf(“n a mouse”);
break;
case 1:
printf(“n a rabbit”);
break;
case 2:
printf(“n a tiger”);
break;
case 3:
printf(“n a lion”);
}
}
a mouse
08/23/15
Switch example
28 08/23/15
switch (choice = getchar())
{
case ‘r’ :
case ‘R’: printf(“Red”);
break;
case ‘b’ :
case ‘B’ : printf(“Blue”);
break;
case ‘g’ :
case ‘G’:
printf(“Green”);
break;
default:
printf(“Black”);
}
What would be the o/p of the following program?
29
main()
{
int x=1;
switch(x)
{
printf(“Hello”);
case 1:
printf(“n JUET”);
break;
case 2;
printf(“n Good”);
break;
}
}
Explanation: Though there is no error ,the first printf statement can never be
executed irrespective of the value of x . In other words all the statements in the
switch have to belong to some case or other.
JUET
08/23/15
What will be the output?
Q1:
main()
{
printf(“%d, %d, %d”,sizeof(3.14f),sizeof(3.14),sizeof(3.14l));
}
Q2:
main()
{
int a=010;
printf(“a=%d”,a);
}
Q3:main(){
float c=3.14;
printf(“%f”,c%2);
} 08/23/1530
4, 8, 10
a=8 // printf(“a=%o”); would print a=10
Similarly printf(“a=%x”); would print a=8( equivalent hex)
Error:Illigal use of floating poit in function main
What will be the output?
Q1:
main()
{int a=5;
a=printf(“Hello”) +printf(“JUET”);
printf(“%d”,a);
}
Q2:main(){
printf(“Hi”);
if(!-1)
printf(“Bye”);
}
Q3:main(){ int a=40000,b=20000;
if(a<b)
printf(“Wow!”);
else printf(“Really!”);
}
08/23/1531
o/p: HelloJUET9
o/p: Hi
o/p: Wow! //Because of integer overflow a
will have 40000-65536=-25536
The Conditional Operator ?:(Ternary
operator)
32 08/23/15
Conditional Operator Example-1
void main()
{
int a;
printf(“Enter a no.”);
scanf(“%d”,&a);
a%2==0?printf(“No. is even”):printf(“No. is odd”);
}
33 08/23/15
Conditional Operator Example-2
34 08/23/15
What will be the output?
main()
{
int a=10,b;
a>=5?b=100:b=200;
printf(“%d”,b);
}
08/23/1535
o/p: 100
Goto Statements
Goto changes the flow of execution without checking any
condition
36 08/23/15
Use of Goto Statements
Nevertheless, there are a few situations where gotos may find a place.
The most common use is to abandon processing in some deeply nested
structure, such as breaking out of two or more loops at once.
The break statement cannot be used directly since it only exits from
the innermost loop. Thus:
for ( ... )
for ( ... ) {
...
if (disaster)
goto error;
}
...
error: // goto will transfer the control to this statement.
37 08/23/15
Some facts about goto
A label has the same form as a variable name, and is followed
by a colon.
It can be attached to any statement in the same function as
the goto.
The scope of a label is the entire function.
With a few exceptions like previous example, code that uses
goto statements is generally harder to understand and its very
difficult to maintain as compare to the code without gotos.
Due to above reason goto is rarely used in programming.
08/23/1538
Some Tips
08/23/1539
What will be the Output
Q1#include<stdio.h>
main(){
int a=10,b=50;
if(a>b);
printf("a is greater than bn");
printf("End of program");
}
Q2:
main(){
int a=55,b=10;
if(a>b)
printf("a is greater than bn");
printf("Very Good!");
else printf("a is smaller than b");
}
08/23/1540
o/p: a is greater than b
End of program
o/p: Error: Misplaced else in function main
What will be the Output
Q3#include<stdio.h>
main(){
float F;
F=20000*3/3.0;
printf(“F=%f“,F);
}
Q4:
main(){
int a=20,b=10;
b=2a;
printf(“a=%dn“,a);
printf(“b=%d“,b);
}
08/23/1541
o/p: F=-1845.333374
o/p:Compilation Error

More Related Content

What's hot

What's hot (20)

C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 
Control statements
Control statementsControl statements
Control statements
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Control structure
Control structureControl structure
Control structure
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
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
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
M C6java5
M C6java5M C6java5
M C6java5
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 

Viewers also liked

Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 

Viewers also liked (10)

Storage classes
Storage classesStorage classes
Storage classes
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Loops in C
Loops in CLoops in C
Loops in C
 
Array in C
Array in CArray in C
Array in C
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Function in C program
Function in C programFunction in C program
Function in C program
 

Similar to 7 decision-control

Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
EXCEPTION HANDLING in C++
EXCEPTION HANDLING in C++EXCEPTION HANDLING in C++
EXCEPTION HANDLING in C++
Prof Ansari
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak Singh
 

Similar to 7 decision-control (20)

Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Control Structures
Control StructuresControl Structures
Control Structures
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
EXCEPTION HANDLING in C++
EXCEPTION HANDLING in C++EXCEPTION HANDLING in C++
EXCEPTION HANDLING in C++
 
C operators
C operatorsC operators
C operators
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Control All
Control AllControl All
Control All
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 

More from Rohit Shrivastava (13)

1 introduction-to-computer
1 introduction-to-computer1 introduction-to-computer
1 introduction-to-computer
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
14 strings
14 strings14 strings
14 strings
 
11 functions
11 functions11 functions
11 functions
 
10 array
10 array10 array
10 array
 
8 number-system
8 number-system8 number-system
8 number-system
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
2 memory-and-io-devices
2 memory-and-io-devices2 memory-and-io-devices
2 memory-and-io-devices
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 

Recently uploaded

如何办理加拿大麦克马斯特大学毕业证(McMaste 毕业证书)毕业证成绩单原版一比一
如何办理加拿大麦克马斯特大学毕业证(McMaste 毕业证书)毕业证成绩单原版一比一如何办理加拿大麦克马斯特大学毕业证(McMaste 毕业证书)毕业证成绩单原版一比一
如何办理加拿大麦克马斯特大学毕业证(McMaste 毕业证书)毕业证成绩单原版一比一
8jg9cqy
 
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
nirzagarg
 
如何办理田纳西大学毕业证(UTK毕业证)成绩单原版一比一
如何办理田纳西大学毕业证(UTK毕业证)成绩单原版一比一如何办理田纳西大学毕业证(UTK毕业证)成绩单原版一比一
如何办理田纳西大学毕业证(UTK毕业证)成绩单原版一比一
fhjlokjhi
 
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
62qaf0hi
 
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
wsppdmt
 
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
 
Illustrative History and Influence of Board Games - Thesis.pptx
Illustrative History and Influence of Board Games - Thesis.pptxIllustrative History and Influence of Board Games - Thesis.pptx
Illustrative History and Influence of Board Games - Thesis.pptx
HenriSandoval
 
Top profile Call Girls In Baranagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Baranagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Baranagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Baranagar [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
 

Recently uploaded (20)

如何办理加拿大麦克马斯特大学毕业证(McMaste 毕业证书)毕业证成绩单原版一比一
如何办理加拿大麦克马斯特大学毕业证(McMaste 毕业证书)毕业证成绩单原版一比一如何办理加拿大麦克马斯特大学毕业证(McMaste 毕业证书)毕业证成绩单原版一比一
如何办理加拿大麦克马斯特大学毕业证(McMaste 毕业证书)毕业证成绩单原版一比一
 
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
 
Bhilai Escorts Service Girl ^ 8250092165, WhatsApp Anytime Bhilai
Bhilai Escorts Service Girl ^ 8250092165, WhatsApp Anytime BhilaiBhilai Escorts Service Girl ^ 8250092165, WhatsApp Anytime Bhilai
Bhilai Escorts Service Girl ^ 8250092165, WhatsApp Anytime Bhilai
 
如何办理田纳西大学毕业证(UTK毕业证)成绩单原版一比一
如何办理田纳西大学毕业证(UTK毕业证)成绩单原版一比一如何办理田纳西大学毕业证(UTK毕业证)成绩单原版一比一
如何办理田纳西大学毕业证(UTK毕业证)成绩单原版一比一
 
Amreli } Russian Call Girls Ahmedabad - Phone 8005736733 Escorts Service at 6...
Amreli } Russian Call Girls Ahmedabad - Phone 8005736733 Escorts Service at 6...Amreli } Russian Call Girls Ahmedabad - Phone 8005736733 Escorts Service at 6...
Amreli } Russian Call Girls Ahmedabad - Phone 8005736733 Escorts Service at 6...
 
JOHN DEERE 7200R 7215R 7230R 7260R 7280R TECHNICAL SERVICE PDF MANUAL 2680PGS...
JOHN DEERE 7200R 7215R 7230R 7260R 7280R TECHNICAL SERVICE PDF MANUAL 2680PGS...JOHN DEERE 7200R 7215R 7230R 7260R 7280R TECHNICAL SERVICE PDF MANUAL 2680PGS...
JOHN DEERE 7200R 7215R 7230R 7260R 7280R TECHNICAL SERVICE PDF MANUAL 2680PGS...
 
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's WhyIs Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
 
Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best ServiceMuslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
 
Vip Call Girls Bengal 🐱‍🏍 Kolkata 0000000000Independent Escorts Service Kolka...
Vip Call Girls Bengal 🐱‍🏍 Kolkata 0000000000Independent Escorts Service Kolka...Vip Call Girls Bengal 🐱‍🏍 Kolkata 0000000000Independent Escorts Service Kolka...
Vip Call Girls Bengal 🐱‍🏍 Kolkata 0000000000Independent Escorts Service Kolka...
 
Premium Call Girls Aurangabad Call Girls 💯Call Us 🔝 6378878445 🔝 💃 Top Class ...
Premium Call Girls Aurangabad Call Girls 💯Call Us 🔝 6378878445 🔝 💃 Top Class ...Premium Call Girls Aurangabad Call Girls 💯Call Us 🔝 6378878445 🔝 💃 Top Class ...
Premium Call Girls Aurangabad Call Girls 💯Call Us 🔝 6378878445 🔝 💃 Top Class ...
 
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptxT.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
T.L.E 5S's (Seiri, Seiton, Seiso, Seiketsu, Shitsuke).pptx
 
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
一比一原版(Deakin毕业证书)迪肯大学毕业证成绩单留信学历认证
 
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
 
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Darbhanga [ 7014168258 ] Call Me For Genuine Models...
 
Illustrative History and Influence of Board Games - Thesis.pptx
Illustrative History and Influence of Board Games - Thesis.pptxIllustrative History and Influence of Board Games - Thesis.pptx
Illustrative History and Influence of Board Games - Thesis.pptx
 
Marathi Call Girls Santacruz WhatsApp +91-9930687706, Best Service
Marathi Call Girls Santacruz WhatsApp +91-9930687706, Best ServiceMarathi Call Girls Santacruz WhatsApp +91-9930687706, Best Service
Marathi Call Girls Santacruz WhatsApp +91-9930687706, Best Service
 
Call Girls In Delhi, Website Rent Mr Avishek {bookkdreamgirl@gmail.com} Escor...
Call Girls In Delhi, Website Rent Mr Avishek {bookkdreamgirl@gmail.com} Escor...Call Girls In Delhi, Website Rent Mr Avishek {bookkdreamgirl@gmail.com} Escor...
Call Girls In Delhi, Website Rent Mr Avishek {bookkdreamgirl@gmail.com} Escor...
 
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be WrongIs Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
 
Call Girls Kolkata +910000000000 call me Independent Escort Service Bengal
Call Girls Kolkata +910000000000 call me Independent Escort Service BengalCall Girls Kolkata +910000000000 call me Independent Escort Service Bengal
Call Girls Kolkata +910000000000 call me Independent Escort Service Bengal
 
Top profile Call Girls In Baranagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Baranagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Baranagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Baranagar [ 7014168258 ] Call Me For Genuine Models...
 

7 decision-control

  • 2. Control Flow The control-flow of a language specify the order in which computations are performed. Statements: An expression such as x = 0 or i++ or printf(...) becomes a statement when it is followed by a semicolon, as in x = 0; i++; printf(...); In C, the semicolon is a statement terminator, rather than a separator Blocks or Statement Blocks: In C, any sequence of statement can be grouped together by enclosing pair of curly braces. Inside statement block variable declaration is allowed. There is no semicolon after the right brace that ends a block. 2
  • 3. Statement Blocks (cont..) Statement block introduces a new scope in the program. A scope is part of the program within which a variable remains defined. Every function has a function body consisting of s set of one or more statements, i.e. a statement block, due to this reason , every function body is confined within a pair of curly braces. 3 Program-1 #include<stdio.h> main(){ int a=3; { int a=10; printf(“a=%d",a); } printf(“na=%d",a); } Program-2 #include<stdio.h> main(){ int a=3; { int b=10; printf(“a=%d",a); } printf(“nb=%d",b); } a=10 a=3
  • 4. What will be the output ? main() { int x=40; { int x=20; printf(“x=%d",x); } printf(“nx=%d",x); }   4 x=20 x=40
  • 5. Flow control Statements Following are some constructs for controlling the flow of execution in C: 1. if, 2. if – else, 3. Nested if and 4. if – else if 5. switch 6. conditional operator 7. goto 5 08/23/15
  • 6. IF Statements Syntax: if(expression) statement • The expression is evaluated; if it is true (that is, if expression has a non-zero value), statementis executed. • Note: here statement may be single statement or Statement Block.Just like if(expression) { stmt 1; stmt 2; ……….. } 6 08/23/15
  • 9. Since an if tests the numeric value of an expression, certain coding shortcuts are possible. The most obvious is writing if (expression) instead of if (expression != 0) in last example if(a%2!=0) can be written as if(a%2) Following three statements are functionally equevalent if(x) if(x!=0) if(!(x==0)) 9 08/23/15
  • 10. IF - ElSE Statements if (expression) statement1 else statement2  The expression is evaluated; if it is true (that is, if expression has a non-zero value), statement1is executed. If it is false (expression is zero) statement2is executed. 10 08/23/15
  • 11. IF - ElSE Statements if (n > 0) if (a > b) z = a; else z = b; the else goes to the inner if, as we have shown by indentation. If that isn't what you want, braces must be used to force the proper association: if (n > 0) { if (a > b) z = a; } else z = b; 11 08/23/15
  • 12. if - else Example 12 08/23/15
  • 13. if - else Example 13 08/23/15
  • 16. IF – ELSE IF Statements 16 08/23/15
  • 17. IF – ELSE IF Statements This sequence of if statements is the most general way of writing a multi-way decision. The expressions are evaluated in order; if an expression is true, the statement associated with it is executed, and this terminates the whole chain. The last else part handles the “none of the above'' or default case where none of the other conditions is satisfied. Sometimes there is no explicit action for the default; in that case the trailing else can be omitted, or it may be used for error checking to catch an ``impossible'' condition. 17 08/23/15
  • 18. IF – ELSE IF Example 18 08/23/15
  • 19. IF – ELSE IF Example 19 08/23/15
  • 20. IF – ELSE IF Example 20 08/23/15
  • 21. Switch Statements Switch is used to Select one choice (case) from many cases. 21 08/23/15
  • 27. What would be the o/p of the following program? 27 main() { int i=4; switch(i) { default: printf(“n a mouse”); break; case 1: printf(“n a rabbit”); break; case 2: printf(“n a tiger”); break; case 3: printf(“n a lion”); } } a mouse 08/23/15
  • 28. Switch example 28 08/23/15 switch (choice = getchar()) { case ‘r’ : case ‘R’: printf(“Red”); break; case ‘b’ : case ‘B’ : printf(“Blue”); break; case ‘g’ : case ‘G’: printf(“Green”); break; default: printf(“Black”); }
  • 29. What would be the o/p of the following program? 29 main() { int x=1; switch(x) { printf(“Hello”); case 1: printf(“n JUET”); break; case 2; printf(“n Good”); break; } } Explanation: Though there is no error ,the first printf statement can never be executed irrespective of the value of x . In other words all the statements in the switch have to belong to some case or other. JUET 08/23/15
  • 30. What will be the output? Q1: main() { printf(“%d, %d, %d”,sizeof(3.14f),sizeof(3.14),sizeof(3.14l)); } Q2: main() { int a=010; printf(“a=%d”,a); } Q3:main(){ float c=3.14; printf(“%f”,c%2); } 08/23/1530 4, 8, 10 a=8 // printf(“a=%o”); would print a=10 Similarly printf(“a=%x”); would print a=8( equivalent hex) Error:Illigal use of floating poit in function main
  • 31. What will be the output? Q1: main() {int a=5; a=printf(“Hello”) +printf(“JUET”); printf(“%d”,a); } Q2:main(){ printf(“Hi”); if(!-1) printf(“Bye”); } Q3:main(){ int a=40000,b=20000; if(a<b) printf(“Wow!”); else printf(“Really!”); } 08/23/1531 o/p: HelloJUET9 o/p: Hi o/p: Wow! //Because of integer overflow a will have 40000-65536=-25536
  • 32. The Conditional Operator ?:(Ternary operator) 32 08/23/15
  • 33. Conditional Operator Example-1 void main() { int a; printf(“Enter a no.”); scanf(“%d”,&a); a%2==0?printf(“No. is even”):printf(“No. is odd”); } 33 08/23/15
  • 35. What will be the output? main() { int a=10,b; a>=5?b=100:b=200; printf(“%d”,b); } 08/23/1535 o/p: 100
  • 36. Goto Statements Goto changes the flow of execution without checking any condition 36 08/23/15
  • 37. Use of Goto Statements Nevertheless, there are a few situations where gotos may find a place. The most common use is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop. Thus: for ( ... ) for ( ... ) { ... if (disaster) goto error; } ... error: // goto will transfer the control to this statement. 37 08/23/15
  • 38. Some facts about goto A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function. With a few exceptions like previous example, code that uses goto statements is generally harder to understand and its very difficult to maintain as compare to the code without gotos. Due to above reason goto is rarely used in programming. 08/23/1538
  • 40. What will be the Output Q1#include<stdio.h> main(){ int a=10,b=50; if(a>b); printf("a is greater than bn"); printf("End of program"); } Q2: main(){ int a=55,b=10; if(a>b) printf("a is greater than bn"); printf("Very Good!"); else printf("a is smaller than b"); } 08/23/1540 o/p: a is greater than b End of program o/p: Error: Misplaced else in function main
  • 41. What will be the Output Q3#include<stdio.h> main(){ float F; F=20000*3/3.0; printf(“F=%f“,F); } Q4: main(){ int a=20,b=10; b=2a; printf(“a=%dn“,a); printf(“b=%d“,b); } 08/23/1541 o/p: F=-1845.333374 o/p:Compilation Error