Programming 3
Programming 3
Programming 3
Topic 3
Definition of selection statement
Selection statement execute a set of instructions only
one. Select between one statement or another
Selection depending on condition is satisfied.
There are three kind of selection structure
If
If—else
Switch..case
A control statement is a statement that determines whether other
statements will be executed. It also know as decision making
statement. In these statements one group of statement depending on
the result of decisions. The result may be in the form of two
Expressions in True or False.
Selection statements are used to perform ‘decision making‘ and then
branch the program flow based on the outcome of decision making
There are three types of control statement available in C :
1) Sequence structure (straight line paths)
2) Selection structure (one or many branches) such as if, if-else,
nested if, if-if-else, if-else-if and switch-case-break.
3) Repetition(Loop) e repetition of a set of activities such as for,
while and do-while.)
Types of control statements :-
1. if statement
2. if- Else statement
3. nested if statement
4. switch statement
(a) if statement
decides whether to execute another statement, or
decides which of two statements to execute.
{
statement block
The state
ment wil
express l b e e xe
ion has cuted on
The state a nonzero ly if the
ment ca value –
} compou
Often co
nd.
n be eith tr
er simple e,
u
or
mp
include o ound statemen
ther con t which m
trol state ay
ments
condition
OPERATOR
MEANING EXAMPLE
== Equal to a==10
!= Not equal to Flag != DONE
< Less than a<b
<= Less than or equal to <=20
> Greater than pointer>end_of_list
>= Greater than or equal Lap>=start
to
Logical Task
Starting from the most basic if syntax,
if (expression)
statement;
next_statement;
1. (expression) is evaluated.
2. If TRUE (non-zero) the statement is executed.
3. If FALSE (zero) the next_statement following the if
statement is executed.
4. So, during the execution, based on some condition, some
codes were skipped.
Flowchart of IF
Flow chart for if
Example 1 : IF Statement
The answers:
#include <stdio.h>
main ()
{ int age, yrs;
printf("Type in your age: ");
scanf ("%d", &age);
return 0;
}
(c) Nested if...else statement
Nested if else statements are used when a series of
decision are involved
Nested if...else statement (if...else if....else Statement)
The nested if...else statement is used when program
requires more than one test expression.
Syntax of nested if...else statement.
if (expression1)
{
statement \\ be executed if test expression1 is true;
}
else if(expression2)
{
statement \\ to be executed if test expression1 is false and 2 is true;
}
else if (expression 3)
{
statement \\ to be executed if text expression1 and 2 are false and 3
is true;
}
. . . Else
{
Flowchart of nested IF-
ELSE
Example Program : Nested If
#include<stdio.h>
The answers :
main()
{
int number;
printf("Type a number:");
scanf("%d", &number);
if(number>100)
printf("Large\n");
else if (number>0)
printf("Not large but positive\n");
else if (number==0)
printf("or zero\n");
else
printf("Negative \n");
return 0;
}
In this nested form, condition_1 is evaluated. If it is zero (FALSE),
statement_1 is executed and the entire nested if statement is
terminated.
If non-zero (TRUE), control goes to the second if (within the first if)
and condition_2 is evaluated.
If it is zero (FALSE), statement_2 is executed; if not, control goes
to the third if (within the second if) and condition_3 is evaluated.
If it is zero (FALSE), statement_3 is executed; if not,
statement_4 is executed. The statement_4 (inner most) will only
be executed if all the if statement are TRUE.
Again, only one of the statements is executed other will be skipped.
If the else is used together with if, always match an else with the
nearest if before the else.
statements_x can be a block of codes and must be put in curly
braces.
(d) SWITCH statement
Switch is a multi branching control statement.
Used for multiple way selections that will branch
into different code segments based on the value of a
variable or expression.
This expression or variable must be of integer data
type.
Syntax for switch statement is shown below.
Break- it is used to exit from a loop or switch
Flow chart for
Switch case statement
The switch constructs has the following form:
switch(condition)
{
case 1 : statement(s);
break;
case 2 : statement(s);
break;
case 3 : statement(s);
break;
…
…
case n : statement(s);
break;
default : statement(s);
}
next_statement;
switch-case-break
statement(s) following the optional default label.
If no match is found and there is no default label, execution
passes to the first statement following the switch statement
closing brace which is the next_statement.
To ensure that only the statements associated with the
matching template are executed, include a break keyword
where needed, which terminates the entire switch statement.
The statement(s) can be a block of code in curly braces.
Example Program : Switch …Case
#include<stdio.h>
main()
{
char ch;
printf("\n Enter character A, B or C: ");
ch=getchar(); Some of the answers:
switch(ch)
{
case 'A':
printf("You Entered A");
break;
case 'B':
printf("You Entered B");
break;
case 'C':
printf("You Entered C");
break;
default:
printf("You Did Not Entered A, B or C!\n");
}
return 0;
}
3.2.1The differences between nested if and
switch
printf("Welcome to C programming\n");
printf("%d %s %c %f is the output.\n", no_bulat, perkataan, huruf, PI);
return 0;
}
Output:
Welcome to C programming
5 rempit s 3.140000 is the output.
c character int
d or i decimal integer int
x hexadecimal integer int
o octal integer int
u unsigned integer int
e scientific notation floating double
point
f “normal” notation floating double
point
g e or f format, whichever is double
shorter
s string pointer to char
p address format (depends on pointer
system)
3.2.5 Character Escape Sequences
There are several character escape sequences which can be used in place of a
character constant or within a string. They are:
puts("Welcome to C programming.");
return 0;
}
Output:
Welcome to C programming.
3.2.5 scanf() function
#include <stdio.h>
main()
{
int t_lahir, t_semasa, umur;
printf("Please enter your birth year :");
scanf("%d", &t_lahir);
umur=t_semasa-t_lahir;
printf("\nYour age is %d year old in year %d",umur, t_semasa);
return 0;
}
Output:
Please enter your birth year : 1990
Please enter the present year : 2011
the header file stdio.h, that reads a line from the standard
input and stores it in a buffer provided by the caller. Use
of gets
#include is strongly discouraged.
<stdio.h>
main()
{
char nama[20];
puts("Please insert your name.");
gets(nama);
return 0;
}
Output:
Please insert your name.
Ana Rafali