Date post: | 06-May-2015 |
Category: |
Education |
Upload: | kamal-acharya |
View: | 533 times |
Download: | 4 times |
LOGICAL EXPRESSIONS IF STATEMENTSWITCH STATEMENT
Selection Statements
Flow of Control
Unless specified , the order of statement execution through a C program is linear: one statement after the other, in sequence.
Some programming statements modify that order, allowing us to: decide whether or not to execute a particular
statement, or perform a statement over and over, repetitively
3
Flow of Control
These decisions are based on a boolean Or logical expression (also called a condition) that evaluates to true or false
The order of statement execution is called the flow of control
Flow of Control
Sequential Flow
Flow of Control
Selection Statements
Flow of Control
Repetition
Logical Expression
Logical expression is an expression which uses one or more logical operators, e.g., (temperature > 90.0 && humidity > 0.90) !(n <= 0 || n >= 100).
The output of the logical expression is the boolean value either true or false.
If Statements
If statements consists of boolean expression followed by one or more statements.
If the boolean expression evaluates to true, the statements inside the block get executed otherwise the first statement outside the block get executed.
The false value is o and all the other values are evaluated as true in C.
If Statement
The syntax of an If statement in C Program is given below
If Statements
If Statement(Example)
Output
If…else Statement
If statements can be followed by the optional else statements, which get executed when the boolean expression is false.
If…else Statement
If…else Statement(Example)
If…else Statement
If…elseif…else Statement
If statement can be followed by optional elseif..else statement, which is very useful to test various conditions using single if…elseif statement.
Following things should be kept in mind An if can have zero or one else's and it must come
after any else if's. An if can have zero to many else if's and they must
come before the else. Once an else if succeeds, none of the remaining else
if's or else's will be tested.
If…elseif…else Statement
If…elseif…else Statement(Example)
#include <stdio.h>#include<conio.h> int main (){ int a = 100; if( a == 10 ) { printf("Value of a is 10\n" ); } else if( a == 20 ) { printf("Value of a is 20\n" ); }
else if( a == 30 ) { printf("Value of a is 30\n" ); } else { printf("None of the values
is matching\n" ); } printf("Exact value of a is:
%d\n", a ); getch(); return 0;}
If…elseif…else Statement
Nested if Statements
It is always legal in C programming to nest if-else statements, which means we can use one if or else if statement inside another if or else if statement(s).
Nested if Statements
#include <stdio.h>#include <conio.h>int main (){ int a = 100; int b = 200; if( a == 100 ) { if( b == 200 ) { printf("Value of a is
100 and b is 200\n" );
} }printf("Exact value of a
is : %d\n", a ); printf("Exact value of
b is : %d\n", b ); getch(); return 0;}
Nested if Statements
Switch Statement
A switch statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for each switch case.
The following rules apply to a switch statement: The expression used in a switch statement must have an
integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
Switch Statement
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Switch Statement
Switch Statement
Switch Statement
#include <stdio.h>#include <conio.h>int main (){ char grade = 'B'; switch(grade) { case 'A' : printf("Excellent!\n" ); break; case 'B' : case 'C' : printf("Well done\n" ); break;
case 'D' : printf("You passed\n" ); break; case 'F' : printf("Better try again\n" ); break; default : printf("Invalid grade\n" ); } printf("Your grade is %c\n",
grade ); getch(); return 0;}
Switch Statement
Nested Switch Statements
It is possible to have a switch as part of the statement sequence of an outer switch.
Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.
Nested Switch Statements
Nested Switch Statements
#include <stdio.h>#include <conio.h> int main (){ int a = 100; int b = 200; switch(a) { case 100: printf("This is part of outer
switch\n", a ); switch(b)
{ case 200: printf("This is part of inner
switch\n", a );
} } printf("Exact value of
a is : %d\n", a ); printf("Exact value of
b is : %d\n", b ); getch(); return 0;}
Nested Switch Statements