selection statements in c programming

33
LOGICAL EXPRESSIONS IF STATEMENT SWITCH STATEMENT Selection Statements

Upload: kamal-acharya

Post on 06-May-2015

534 views

Category:

Education


4 download

DESCRIPTION

This slide provides the introduction to the all the selection statements available in C Programming.

TRANSCRIPT

Page 1: Selection Statements in C Programming

LOGICAL EXPRESSIONS IF STATEMENTSWITCH STATEMENT

Selection Statements

Page 2: Selection Statements in C Programming

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

Page 3: Selection Statements in C Programming

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

Page 4: Selection Statements in C Programming

Flow of Control

Sequential Flow

Page 5: Selection Statements in C Programming

Flow of Control

Selection Statements

Page 6: Selection Statements in C Programming

Flow of Control

Repetition

Page 7: Selection Statements in C Programming

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.

Page 8: Selection Statements in C Programming

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.

Page 9: Selection Statements in C Programming

If Statement

The syntax of an If statement in C Program is given below

Page 10: Selection Statements in C Programming

If Statements

Page 11: Selection Statements in C Programming

If Statement(Example)

Page 12: Selection Statements in C Programming

Output

Page 13: Selection Statements in C Programming

If…else Statement

If statements can be followed by the optional else statements, which get executed when the boolean expression is false.

Page 14: Selection Statements in C Programming

If…else Statement

Page 15: Selection Statements in C Programming

If…else Statement(Example)

Page 16: Selection Statements in C Programming

If…else Statement

Page 17: Selection Statements in C Programming

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.

Page 18: Selection Statements in C Programming

If…elseif…else Statement

Page 19: Selection Statements in C Programming

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;}

Page 20: Selection Statements in C Programming

If…elseif…else Statement

Page 21: Selection Statements in C Programming

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).

Page 22: Selection Statements in C Programming

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;}

Page 23: Selection Statements in C Programming

Nested if Statements

Page 24: Selection Statements in C Programming

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.

Page 25: Selection Statements in C Programming

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.

Page 26: Selection Statements in C Programming

Switch Statement

Page 27: Selection Statements in C Programming

Switch Statement

Page 28: Selection Statements in C Programming

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;}

Page 29: Selection Statements in C Programming

Switch Statement

Page 30: Selection Statements in C Programming

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.

Page 31: Selection Statements in C Programming

Nested Switch Statements

Page 32: Selection Statements in C Programming

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;}

Page 33: Selection Statements in C Programming

Nested Switch Statements