KEMBAR78
Conditional Statements | PDF | Computer Science | Computing
0% found this document useful (0 votes)
20 views25 pages

Conditional Statements

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views25 pages

Conditional Statements

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Conditional statements

What is Decision Making Statement?

• In the C programming language, the program


execution flow is line by line from top to bottom.
That means the c program is executed line by line
from the main method. But this type of execution
flow may not be suitable for all the program
solutions. Sometimes, we make some decisions or
we may skip the execution of one or more lines of
code. Consider a situation, where we write a
program to check whether a student has passed or
failed in a particular subject.
• Here, we need to check whether the marks
are greater than the pass marks or not. If
marks are greater, then we decide that the
student has passed otherwise failed. To solve
such kind of problems in c we use the
statements called decision making statements.
• Decision-making statements are the
statements that are used to verify a given
condition and decide whether a block of
statements gets executed or not based on the
condition result.
• In the c programming language, there are two
decision-making statements they are as
follows.
• if statement
• switch statement
• if statement in c
• In c, if statement is used to make decisions based on a
condition. The if statement verifies the given condition
and decides whether a block of statements are
executed or not based on the condition result. In c, if
statement is classified into four types as follows...
• Simple if statement
• if-else statement
• Nested if statement
• if-else-if statement (if-else ladder)
• Simple if statement
• Simple if statement is used to verify the given
condition and executes the block of statements
based on the condition result. The simple if
statement evaluates specified condition. If it is
TRUE, it executes the next statement or block of
statements. If the condition is FALSE, it skips the
execution of the next statement or block of
statements. The general syntax and execution flow
of the simple if statement is as follows.
• Example Program | Test whether given
number is divisible by 5.
• #include<stdio.h>
• void main()
• {
• int n ;
• printf("Enter any integer number: ") ;
scanf("%d", &n) ;
• if ( n%5 == 0 )
• printf("Given number is divisible by 5\n") ;
• }
• if-else statement
• The if-else statement is used to verify the
given condition and executes only one out of
the two blocks of statements based on the
condition result. The if-else statement
evaluates the specified condition. If it is TRUE,
it executes a block of statements (True block).
If the condition is FALSE, it executes another
block of statements (False block). The general
syntax and execution flow of the if-else
statement is as follows.
• #include<stdio.h>
• void main()
• {
• int n ;
• printf("Enter any integer number: ") ;
• scanf("%d", &n) ;
• if ( n%2 == 0 )
• printf("Given number is EVEN\n") ;
• else
• printf("Given number is ODD\n") ;
• }
• Nested if statement
• Writing a if statement inside another if
statement is called nested if statement. The
general syntax of the nested if statement is as
follows...
Example Program | Test whether given number is even or odd if it is below
100.
• #include<stdio.h>
• void main()
• {
• int n ;
• printf("Enter any integer number: ") ;
• scanf("%d", &n) ;
• if ( n < 100 )
• {
• printf("Given number is below 100\n") ;
• if( n%2 == 0)
• printf("And it is EVEN") ;
• else printf("And it is ODD") ;
• }
• else printf("Given number is not below 100") ;
• }
• if-else-if statement (if-else ladder)
• Writing a if statement inside else of an if
statement is called if-else-if statement. The
general syntax of the if-else-if statement is as
follows...
Example Program | Find the largest of three numbers.

• #include<stdio.h>
• void main()
• {
• int a, b, c ;
• printf("Enter any three integer numbers: ") ; scanf("%d%d%d", &a,
&b, &c) ;
• if( a>=b && a>=c)
• printf("%d is the largest number", a) ;
• else if (b>=a && b>=c)
• printf("%d is the largest number", b) ;
• else printf("%d is the largest number", c) ;
• }
'switch' statement in C

• Consider a situation in which we have many


options out of which we need to select only
one option that is to be executed. Such kind of
problems can be solved using nested
if statement. But as the number of options
increases, the complexity of the program also
gets increased. This type of problem can be
solved very easily using a switch statement.
• Using the switch statement, one can select
only one option from more number of options
very easily. In the switch statement, we
provide a value that is to be compared with a
value associated with each option. Whenever
the given value matches the value associated
with an option, the execution starts from that
option. In the switch statement, every option
is defined as a case.
• The switch statement contains one or more cases and
each case has a value associated with it. At first switch
statement compares the first case value with the
switchValue, if it gets matched the execution starts from
the first case. If it doesn't match the switch statement
compares the second case value with the switchValue
and if it is matched the execution starts from the
second case. This process continues until it finds a
match. If no case value matches with the switchValue
specified in the switch statement, then a special case
called default is executed.
• When a case value matches with the
switchValue, the execution starts from that
particular case. This execution flow continues
with the next case statements also. To avoid
this, we use the "break" statement at the end
of each case. That means the break statement
is used to terminate the switch statement.
However, it is optional.
Example Program | Display pressed digit in words.

• #include<stdio.h>
• void main()
• {
• int n ;
• printf("Enter any digit: ") ;
• scanf("%d", &n) ;
• switch( n )
• {
• case 0: printf("ZERO") ;
• break ;
• case 1: printf("ONE") ;
• break ;
• case 2: printf("TWO") ;
• break ;
• case 3: printf("THREE") ;
• break ;
• case 4: printf("FOUR") ;
• break ;
• case 5: printf("FIVE") ;
• break ;
• case 6: printf("SIX") ;
• break ;
• case 7: printf("SEVEN") ;
• break ;
• case 8: printf("EIGHT") ;
• break ;
• case 9: printf("NINE") ;
• break ;
• default: printf("Not a Digit") ;
• }
• }

You might also like