Lecture Notes 3
Lecture Notes 3
STRUCTURE
Selection
Selection is the second construct of a structured programming
language.
We perform an action depending upon the prevailing conditions.
Selection allows you to make some decisions and choose
between two or more alternatives. We make some decision and
select a particular choice.
Your program reflects the real world problem and it should have
the capability of making a decision and choose between two or
more alternatives
The if statement
The if statement is sometimes called a conditional statement.
The operation of a if statement is governed by a conditional
test. If the conditional test is true, one or more actions are
executed.
if(expression)
statement;
NOTE: In C, an expression is
true; it is evaluated to a
nonzero
value.
If
the
expression is zero, it is false.
#include<stdio.h>
int main(void)
{
int number;
printf(Enter a number );
scanf(%d,&number);
if (number%2 == 0)
printf(The number %d is an even number\n, number);
return 0;
}
If-Else Statement
An if-else statement is a compound statement used to make
a decision between two alternatives.
While using a if statement, the statement following the if
expression is evaluated if the expression is evaluated to true.
If the expression is false, the statement is simply skipped and
the control is passed to the immediate next statement.
In the case of a if-else statement, if the condition is true
one or more action statements are performed.
If the condition is false, then a different action or set
of statements are executed.
NOTE
1.
Either action statement 1 or 2 may consist of a single or
multiple statements
2.
A single statement is terminated with a semicolon.
3.
A multiple action statement is enclosed in braces.
Star
t
If
expression
yes
______
______
..
else
______
______
..
Exp
True
no
Example:
int number = 5;
if (number %2 == 0)
printf(the number %d is even\n, number);
else
printf(the number %d is odd\n, number);
Example:
What is the output:
int x, y;
x = 3;
y = 5;
if (x < 2)
printf(%d\n, x);
else
printf(%d\n ,y);
Result: 5
Enter a number 10
10 is an even number
if x <2
printf (%d\n, x);
if (x < 2) then
printf (%d\n,
x);
Double quote is
missing
Parenthesis ()
if x <2
missing
printf
(%d\n, x);
Double quote is
missing
Switch Statement
Switch statement can be regarded as a special instance of if
else, if-else-if, else-if-else statement.
The condition for branching in switch statement is by integer
values. The general form of switch statement is
switch (expression giving integer value)
{
case constant 1:
statement 1;
case constant 2:
statement 2;
case constant n:
statement n;
default:
statement n+1 ;
}
case
1
case
2
case
3
default
Example:
int code = 2;
switch (code) {
case 1:
printf(Bahasa Malaysia\n);
case 2:
printf(Mandarin\n);
case 3:
printf(English\n);
case 4:
printf(Tamil\n);
default: printf(Not a language\n);
}
Result:
Mandarin
English
Tamil
Not a language
BREAK STATEMENT
A break statement causes an immediate exit from the
innermost while, for, do while and switch statement.
If when a break statement is encountered at the end of a case,
it causes immediate exit from the while, for, do, do while and
switch statement.
Example:
int movie=1;
switch (movie)
{
case 1:
printf(Die Another Day\n);
break;
case 2:
printf(Lord of The Rings\n);
break;
case 3:
printf(The Ring\n);
break;
default: printf(No Movie\n);
}
Result: Die Another Day