0% found this document useful (0 votes)
27 views

Lecture Notes 3

5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555544444444444444444444444444444444444

Uploaded by

Amir Chip
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lecture Notes 3

5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555544444444444444444444444444444444444

Uploaded by

Amir Chip
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

SELECTION

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.

The syntax of a simplest if statement is shown below:

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.

/* Program to demonstrate a if statement */

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

The If-Else Statement


if (expression)
action statement 1
else
action statement 2

In this type of if statement, the expression is evaluated first.

Otherwise action statement 2 will be 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);

The action statement can be another if else statement.


Example:
if (rain == 0)
if (fish == 0)
printf(It is not raining you do not have a
fish\n);
else
printf(It is not raining you got a fish\n);

Flowchart to find the greatest integer given three integer

/*Program to find the greatest integer given three integer */


#include <stdio.h>
void main(void)
{
int a, b, c,
scanf(%d%d%d, &a &b &c);
if (a>b)
{ if (a>c)
printf(%d is greater , a);
else
printf(%d is gretaer, c);
}
else
{ if (b>c)
printf(%d is greater ,b);
else
printf(%d is greater ,c);
return;}
}

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

/* Program to determine the entered number is odd or


even */
#include<stdio.h>
int main(void)
{
int number;
/* Get the number from the user */
printf(Enter a Number );
scanf(%d,&number);
/* Check the number is odd or even */
if(number % 2 == 1)
printf(%d is an odd number\n,number);
else
printf(%d is an even number\n, number);
return 0;
}

Enter a number 10
10 is an even number

What is the syntax error?


a)
if (x < 2) then
printf (%d\n, x);
b)

if x <2
printf (%d\n, x);

What is the output?


code = 1
if(code == 1) {
printf(Mathematician\n);
if (code = 2)
printf(Engineer\n);
printf(Scientist\n);
}
else
printf(doctor\n);

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

NESTED IF ELSE STATEMENT

The general syntax of a nested else statement is


if(expression-1)
action statement-1;
else if (expression-2)
action statement-2;
else if (expression-3)
action statement-3;
else if (expression-i)
action statement-i;
else if (expression-n)
action statement-n;

Find the first expression that is true (if-any). If expression i is


the first true condition, (if expression 1, 2, 3 are false), the
action statement- i is executed and all the other action
statement are skipped. If no action is true, no statement will
be executed.
ELSE-IF-ELSE STATEMENT
if(expression-1)
action statement-1;
else if (expression-2)
action statement-2;
else if (expression-3)
action statement-3;
else if (expression-i)
action statement-i;
else if (expression-n)
action statement-n;
else
action n + 1;

Find the first expression that is true, if any. If expression i is


the first condition, then action statement i will be executed
and all the other action statements are skipped.
If no expression is true, then the action statement n+1 will be
executed and all other statements are skipped.
Example:
if ((time >= 0.0) &&(time < 12.0))
printf(Good Morning\n);
else if ((time >=12.0) &&(time < 18.0))
printf(Good Afternoon\n);
else if ((time >= 18.0) &&(time < 24.0))
printf(Good Evening\n);
else
printf(Time Out of Range\n);

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

First the expression is evaluated . The value of the integer


expression is compared with the constant 1 then 2, 3, and so
on. [All the constant must be different].
If the integer expression does not equal to any of the
constant, execution will execute the statements in the default
class.
If the value of integer expression equals constant I, execution
will begin with statement i. [ie once the entry point has been
located by the switch statement, all following are executed.]

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

You might also like