SlideShare a Scribd company logo
DECISION MAKING and
BRANCHING
REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY
MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
Branching:
• The C language programs presented until now follows a sequential
form of execution of statements. Many times it is required to alter the
flow of the sequence of instructions.
• C language provides statements that can alter the flow of a
sequence of instructions. These statements are called Control
statements or Decision-making statements..
• These statements help to jump from one part of the program to
another.
• The control transfer may be conditional or unconditional.
• Decision making is used to see whether a particular condition has
occurred or not and then direct the computer to execute certain
statements accordingly.
Decision making statements are used to execute a part of statement
based on some condition and exclude other.
• C language possesses such decision-making capabilities by
supporting the following statements:-
1. If statement
2. Switch statement
3. conditional operator statement
4. goto statement
Decision making with if Statement:
• 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.
The if structure has the following syntax
• if (condition)
statement;
• It allows the computer to evaluate the expression first and 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 different forms of if statement are:-
1. simple if statement
2. if …else statement
3. Nested if statement
4. else ..if ladder
Simple if statement:-
The general form of a simple if statement is as follows,
if (test expression)
{
statement-block;
}
statement – x;
• Here, the statement block may contain a single statement or a
group of statements.
• If the test expression is true then the statement block will be
executed; otherwise it will be skipped to the statement – x.
Example program
Calculate the absolute value of an integer
# include <stdio.h>
void main ()
{
int number;
printf ("Type a number:");
scanf ("%d", &number);
if (number < 0)
number = -number;
printf ("The absolute value is %d n", number);
}
• The above program checks the value of the input number to see if it
is less than zero.
• If it is then the following program statement which negates the
value of the number is executed.
• If the value of the number is not less than zero, we do not want to
negate it then this statement is automatically skipped.
• The absolute number is then displayed by the program, and program
execution ends.
• The if else construct:
• The syntax of the If else construct is as follows:-
if (test expression)
statement1;
else
statement2;
• The if else is actually just an extension of the general format of if
statement.
• If the result of the test expression is true, then program statement 1
is executed,
• otherwise program statement 2 will be executed.
• If any case either program statement 1 is executed or program
statement 2 is executed but not both when writing programs this
else statement is so frequently required that almost all programming
languages provide a special construct to handle this situation.
For example:
• Program to find whether a number is negative or positive
#include <stdio.h>
void main ()
{
int num;
printf ("Enter the number");
scanf ("%d", &num);
if (num < 0)
printf ("The number %d is negative“,num);
else
printf ("The number %d is positive“,num);
}
#include<stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d",&number);
if(number%2==0)
{ printf("%d is an even number",number);
}
else
{
printf("%d is an odd number",number);
}
return 0;
}
In the above program
• the If statement checks whether the given number is less than 0.
• If it is less than zero then it is negative therefore the condition
becomes true then the statement
The number is negative is executed.
• If the number is not less than zero the If else construct skips the first
statement and prints the second statement declaring that the
number is positive.
Compound Relational tests:
• C language provides the mechanisms necessary to perform compound
relational tests.
• A compound relational test is simple one or more simple relational tests
joined together by either the logical AND or the logical OR operators.
• These operators are represented by the character pairs && // respectively.
• The compound operators can be used to form complex expressions in C.
a) if (condition1 && condition2 && condition3)
b) if (condition1 || condition2 || condition3)
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("Character is an ALPHABET.");
}
else
{
printf("Character is NOT ALPHABET.");
}
return 0;
}
if (isupper(ch))
{
printf(“n%c is uppercase alphabet.", ch);
}
else if(islower(ch))
{
printf(“n%c is lowercase alphabet.", ch);
}
else
{
printf(“n%c is not an alphabet.", ch);
} }
#include <stdio.h>
void main()
{
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||
month==12)
{
printf("31 days");
}
else if(month==4 || month==6 || month==9 || month==11) {
printf("30 days");
}
else if(month==2)
{
printf("28 or 29 days");
}
else
{
printf("Invalid input! Please enter month number between (1-12).");
Nested if Statement
When a series of decisions are involved, we may have to use more
than one if..else statement in nested form.
The if statement may itself contain another if statement is known as
nested if statement.
Syntax:
if (test condition1)
{
if (test condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
• The if statement may be nested as deeply as you need to
nest it.
• One block of code will only be executed if two conditions are
true. Condition 1 is tested first and then condition 2 is
tested.
• The second if condition is nested in the first.
• The second if condition is tested only when the first
condition is true else the program flow will skip to the
corresponding else statement.
Unit ii chapter 2 Decision making and Branching in C
#include <stdio.h>
#include <conio.h>
void main()
{
int no;
clrscr();
printf("n Enter Number :");
scanf("%d",&no);
if(no>0)
{
printf("nn Number is greater than 0 !");
}
else
{
if(no==0)
{
printf("nn It is 0 !");
}
else
{
printf("Number is less than 0 !");
}
}
getch();
}
The ELSE If Ladder:
• The ifs are put together when multipath decisions are involved. A
multipath decision is a chain of ifs in which that statement associated
with each else is an if.
Syntax:
• if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
…..
else
default statement;
statement-x;
Unit ii chapter 2 Decision making and Branching in C
#include<stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d",&number);
if(number>0)
{
printf("%d is a positive number", number);
}
else if(number<0)
{
printf("%d is a negative number", number);
}
else
{
printf("Number is zero");
}
}
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("%c is alphabet.", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("%c is digit.", ch);
}
else
{
printf("%c is special character.", ch);
}
• Example program using If else ladder to grade the student according
to the following rules.
Marks Grade
70 to 100
60 to 69
50 to 59
40 to 49
0 to 39
DISTINCTION
IST CLASS
IIND CLASS
PASS CLASS
FAIL
#include <stdio.h>
void main ()
{
int marks
printf ("Enter marksn") ;
scanf ("%d", &marks) ;
if (marks <= 100 && marks >= 70)
printf ("n Distinction") ;
else if (marks >= 60)
printf("n First class");
else if (marks >= 50)
printf ("n second class");
else if (marks >= 35)
printf ("n pass class");
else
printf ("Fail")
}
The Switch Statement:
• Unlike the If statement which allows a selection of two alternatives the
switch statement allows a program to select one statement for
execution out of a set of alternatives.
• During the execution of the switch statement only one of the possible
statements will be executed the remaining statements will be skipped.
• The usage of multiple If else statement increases the complexity of the
program since when the number of If else statements increase it affects
the readability of the program and makes it difficult to follow the
program.
• The switch statement removes these disadvantages by using a simple
and straight forward approach
switch (expression)
{
case value1: block-1
break;
case value2: block-2
break;
…………
default: default-block
break;
}
statement-x;
/*Program to find whether the number is odd or even*/
#include <stdio.h>
main()
{
int n;
printf(“Enter the number”);
scanf(“%d”,&n);
switch(n%2)
{
case 0 : printf(“nThe number %d is even",n);
break;
case 1 : printf(“nThe number %d is odd n",n);
break;
}
}
Unit ii chapter 2 Decision making and Branching in C
The ?: operator:-
• The conditional operator or ternary operator is useful for making two-way decisions.
This operator is a combination of ? and :, and takes 3 operands.
• The general form of use of the conditional operator is as follows:
Conditional expression ? expression1 : expression2
Example:
flag = (x < 0) ? 0 : 1;
• The conditional expression is evaluated first. If the result is nonzero, expression1
is evaluated and is returned as the value of the conditional expression. Otherwise,
expression2 is evaluated and its value is returned.
For example the statement
if (x <0)
flag=0;
else
flag=1;
The goto statement:-
• C supports the goto statement to branch unconditionally form one
point to another in the program.
goto label;
• The goto statement is simple statement used to transfer the program
control unconditionally from one statement to another statement.
Eg:
• goto read;
a>
goto label;
…………
…………
…………
Label:
Statement;
b>
label:
…………
…………
…………
goto label;
The goto requires a label in order to identify the place where the
branch is to be made.
A label is a valid variable name followed by a colon.
#include <stdio.h>
main ()
{
int n, sum = 0, i = 0;
printf ("Enter a number")
scanf ("%d", &n)
loop:
i++;
sum += i
if (i < n)
goto loop;
printf ("n sum of %d natural numbers = %d", n, sum);
}
Ad

More Related Content

What's hot (20)

C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
Ashim Lamichhane
 
C functions
C functionsC functions
C functions
University of Potsdam
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
Sahithi Naraparaju
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
Nurul Zakiah Zamri Tan
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
Muthuganesh S
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
 
Data types in C
Data types in CData types in C
Data types in C
Tarun Sharma
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
Hridoy Bepari
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Sowmya Jyothi
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
Priyansh Thakar
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
Ashim Lamichhane
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
Sahithi Naraparaju
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
Muthuganesh S
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
 
Data types in C
Data types in CData types in C
Data types in C
Tarun Sharma
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
Qazi Shahzad Ali
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
Hridoy Bepari
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Sowmya Jyothi
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
Decision making and branching in c programming
Decision making and branching in c programmingDecision making and branching in c programming
Decision making and branching in c programming
Priyansh Thakar
 

Similar to Unit ii chapter 2 Decision making and Branching in C (20)

C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
DEEPAK948083
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
ishaparte4
 
POP Unit 2.pptx.pdf for your time and gauss with example
POP Unit 2.pptx.pdf for your time and gauss with examplePOP Unit 2.pptx.pdf for your time and gauss with example
POP Unit 2.pptx.pdf for your time and gauss with example
siddarameshav871
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
baabtra.com - No. 1 supplier of quality freshers
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
imtiazalijoono
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
Munazza-Mah-Jabeen
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
Haard Shah
 
Lec 10
Lec 10Lec 10
Lec 10
kapil078
 
Control structures(class 02)
Control structures(class 02)Control structures(class 02)
Control structures(class 02)
Vinoth Chandrasekaran
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
Giri383500
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
DEEPAK948083
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
ishaparte4
 
POP Unit 2.pptx.pdf for your time and gauss with example
POP Unit 2.pptx.pdf for your time and gauss with examplePOP Unit 2.pptx.pdf for your time and gauss with example
POP Unit 2.pptx.pdf for your time and gauss with example
siddarameshav871
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
imtiazalijoono
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
Munazza-Mah-Jabeen
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
Haard Shah
 
Lec 10
Lec 10Lec 10
Lec 10
kapil078
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
Giri383500
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Ad

More from Sowmya Jyothi (19)

Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
Sowmya Jyothi
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHIBCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Sowmya Jyothi
 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
Sowmya Jyothi
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHINETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHIIntroduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHI
Sowmya Jyothi
 
Introduction to graphics
Introduction to graphicsIntroduction to graphics
Introduction to graphics
Sowmya Jyothi
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPT
Sowmya Jyothi
 
Internal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiInternal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya Jyothi
Sowmya Jyothi
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel  Chapter 2 Mrs.Sowmya JyothiIntroduction to the Kernel  Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya JyothiIntroduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
Sowmya Jyothi
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
Sowmya Jyothi
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
Sowmya Jyothi
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHIBCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
Sowmya Jyothi
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Sowmya Jyothi
 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
Sowmya Jyothi
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
Sowmya Jyothi
 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHINETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHIIntroduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHI
Sowmya Jyothi
 
Introduction to graphics
Introduction to graphicsIntroduction to graphics
Introduction to graphics
Sowmya Jyothi
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPT
Sowmya Jyothi
 
Internal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiInternal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya Jyothi
Sowmya Jyothi
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel  Chapter 2 Mrs.Sowmya JyothiIntroduction to the Kernel  Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya JyothiIntroduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Sowmya Jyothi
 
Ad

Recently uploaded (20)

How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
ColĂŠgio Santa Teresinha
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Michelle Rumley & MairĂŠad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & MairĂŠad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & MairĂŠad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & MairĂŠad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Michelle Rumley & MairĂŠad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & MairĂŠad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & MairĂŠad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & MairĂŠad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 

Unit ii chapter 2 Decision making and Branching in C

  • 1. DECISION MAKING and BRANCHING REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE
  • 2. Branching: • The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the flow of the sequence of instructions. • C language provides statements that can alter the flow of a sequence of instructions. These statements are called Control statements or Decision-making statements.. • These statements help to jump from one part of the program to another. • The control transfer may be conditional or unconditional. • Decision making is used to see whether a particular condition has occurred or not and then direct the computer to execute certain statements accordingly.
  • 3. Decision making statements are used to execute a part of statement based on some condition and exclude other. • C language possesses such decision-making capabilities by supporting the following statements:- 1. If statement 2. Switch statement 3. conditional operator statement 4. goto statement
  • 4. Decision making with if Statement: • 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. The if structure has the following syntax • if (condition) statement; • It allows the computer to evaluate the expression first and 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.
  • 5. The different forms of if statement are:- 1. simple if statement 2. if …else statement 3. Nested if statement 4. else ..if ladder
  • 6. Simple if statement:- The general form of a simple if statement is as follows, if (test expression) { statement-block; } statement – x; • Here, the statement block may contain a single statement or a group of statements. • If the test expression is true then the statement block will be executed; otherwise it will be skipped to the statement – x.
  • 7. Example program Calculate the absolute value of an integer # include <stdio.h> void main () { int number; printf ("Type a number:"); scanf ("%d", &number); if (number < 0) number = -number; printf ("The absolute value is %d n", number); }
  • 8. • The above program checks the value of the input number to see if it is less than zero. • If it is then the following program statement which negates the value of the number is executed. • If the value of the number is not less than zero, we do not want to negate it then this statement is automatically skipped. • The absolute number is then displayed by the program, and program execution ends.
  • 9. • The if else construct: • The syntax of the If else construct is as follows:- if (test expression) statement1; else statement2;
  • 10. • The if else is actually just an extension of the general format of if statement. • If the result of the test expression is true, then program statement 1 is executed, • otherwise program statement 2 will be executed. • If any case either program statement 1 is executed or program statement 2 is executed but not both when writing programs this else statement is so frequently required that almost all programming languages provide a special construct to handle this situation.
  • 11. For example: • Program to find whether a number is negative or positive #include <stdio.h> void main () { int num; printf ("Enter the number"); scanf ("%d", &num); if (num < 0) printf ("The number %d is negative“,num); else printf ("The number %d is positive“,num); }
  • 12. #include<stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d",&number); if(number%2==0) { printf("%d is an even number",number); } else { printf("%d is an odd number",number); } return 0; }
  • 13. In the above program • the If statement checks whether the given number is less than 0. • If it is less than zero then it is negative therefore the condition becomes true then the statement The number is negative is executed. • If the number is not less than zero the If else construct skips the first statement and prints the second statement declaring that the number is positive.
  • 14. Compound Relational tests: • C language provides the mechanisms necessary to perform compound relational tests. • A compound relational test is simple one or more simple relational tests joined together by either the logical AND or the logical OR operators. • These operators are represented by the character pairs && // respectively. • The compound operators can be used to form complex expressions in C. a) if (condition1 && condition2 && condition3) b) if (condition1 || condition2 || condition3)
  • 15. #include <stdio.h> int main() { char ch; printf("Enter any character: "); scanf("%c", &ch); if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { printf("Character is an ALPHABET."); } else { printf("Character is NOT ALPHABET."); } return 0; }
  • 16. if (isupper(ch)) { printf(“n%c is uppercase alphabet.", ch); } else if(islower(ch)) { printf(“n%c is lowercase alphabet.", ch); } else { printf(“n%c is not an alphabet.", ch); } }
  • 17. #include <stdio.h> void main() { int month; printf("Enter month number (1-12): "); scanf("%d", &month);
  • 18. if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) { printf("31 days"); } else if(month==4 || month==6 || month==9 || month==11) { printf("30 days"); } else if(month==2) { printf("28 or 29 days"); } else { printf("Invalid input! Please enter month number between (1-12).");
  • 19. Nested if Statement When a series of decisions are involved, we may have to use more than one if..else statement in nested form. The if statement may itself contain another if statement is known as nested if statement. Syntax:
  • 20. if (test condition1) { if (test condition2) { statement-1; } else { statement-2; } } else { statement-3; }
  • 21. • The if statement may be nested as deeply as you need to nest it. • One block of code will only be executed if two conditions are true. Condition 1 is tested first and then condition 2 is tested. • The second if condition is nested in the first. • The second if condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement.
  • 23. #include <stdio.h> #include <conio.h> void main() { int no; clrscr(); printf("n Enter Number :"); scanf("%d",&no);
  • 24. if(no>0) { printf("nn Number is greater than 0 !"); } else { if(no==0) { printf("nn It is 0 !"); } else { printf("Number is less than 0 !"); } } getch(); }
  • 25. The ELSE If Ladder: • The ifs are put together when multipath decisions are involved. A multipath decision is a chain of ifs in which that statement associated with each else is an if. Syntax: • if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; ….. else default statement; statement-x;
  • 27. #include<stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d",&number);
  • 28. if(number>0) { printf("%d is a positive number", number); } else if(number<0) { printf("%d is a negative number", number); } else { printf("Number is zero"); } }
  • 29. if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { printf("%c is alphabet.", ch); } else if(ch >= '0' && ch <= '9') { printf("%c is digit.", ch); } else { printf("%c is special character.", ch); }
  • 30. • Example program using If else ladder to grade the student according to the following rules. Marks Grade 70 to 100 60 to 69 50 to 59 40 to 49 0 to 39 DISTINCTION IST CLASS IIND CLASS PASS CLASS FAIL
  • 31. #include <stdio.h> void main () { int marks printf ("Enter marksn") ; scanf ("%d", &marks) ; if (marks <= 100 && marks >= 70) printf ("n Distinction") ; else if (marks >= 60) printf("n First class"); else if (marks >= 50) printf ("n second class"); else if (marks >= 35) printf ("n pass class"); else printf ("Fail") }
  • 32. The Switch Statement: • Unlike the If statement which allows a selection of two alternatives the switch statement allows a program to select one statement for execution out of a set of alternatives. • During the execution of the switch statement only one of the possible statements will be executed the remaining statements will be skipped. • The usage of multiple If else statement increases the complexity of the program since when the number of If else statements increase it affects the readability of the program and makes it difficult to follow the program. • The switch statement removes these disadvantages by using a simple and straight forward approach
  • 33. switch (expression) { case value1: block-1 break; case value2: block-2 break; ………… default: default-block break; } statement-x;
  • 34. /*Program to find whether the number is odd or even*/ #include <stdio.h> main() { int n; printf(“Enter the number”); scanf(“%d”,&n); switch(n%2) { case 0 : printf(“nThe number %d is even",n); break; case 1 : printf(“nThe number %d is odd n",n); break; } }
  • 36. The ?: operator:- • The conditional operator or ternary operator is useful for making two-way decisions. This operator is a combination of ? and :, and takes 3 operands. • The general form of use of the conditional operator is as follows: Conditional expression ? expression1 : expression2 Example: flag = (x < 0) ? 0 : 1; • The conditional expression is evaluated first. If the result is nonzero, expression1 is evaluated and is returned as the value of the conditional expression. Otherwise, expression2 is evaluated and its value is returned. For example the statement if (x <0) flag=0; else flag=1;
  • 37. The goto statement:- • C supports the goto statement to branch unconditionally form one point to another in the program. goto label; • The goto statement is simple statement used to transfer the program control unconditionally from one statement to another statement. Eg: • goto read;
  • 38. a> goto label; ………… ………… ………… Label: Statement; b> label: ………… ………… ………… goto label; The goto requires a label in order to identify the place where the branch is to be made. A label is a valid variable name followed by a colon.
  • 39. #include <stdio.h> main () { int n, sum = 0, i = 0; printf ("Enter a number") scanf ("%d", &n) loop: i++; sum += i if (i < n) goto loop; printf ("n sum of %d natural numbers = %d", n, sum); }