Beruflich Dokumente
Kultur Dokumente
STRUCTURE : SELECTION –
PART I
1
WEEK 3 OUTLINE
Intro to control structure
Types of selection
One-way selection
Two-way selection
Multi-selection
Compound statement
2
Learning Objectives
Apply concepts for developing algorithms, using variables and
structuring a C program
3
Control Structures
All programs can be written in terms of three control structures (like
building blocks)
Sequence Structure
‘Built-in’ to C
Unless otherwise directed, one statement after the next is executed
Is a series of steps executed sequentially by default
4 End
Selection Structure
Used to choose among alternative courses of action
Used to control the flow of a program
Also called as decision or branches
Branches are conditions or choices used to enable selection of
program flow
C has three types:
If selection statement (one-way selection)
If…else selection statement (two-way selection)
Switch selection statement (multiple selection)
5
One-way statement: If
If structures is a single-entry/single-exit structure
The primary tool of selection structure
Used when we want a single or a group of statements to
be executed if the condition is true.
If the condition is false, the statement is skipped and
program continues by executing the first statement after
the if selection structure
Condition is express as a statement and two possible
outcomes are indicate by true and false (boolean
result).
6
If syntax – Part 1
if (test expression)
{
statement/s to be executed if test expression is true;
}
Exercise:
Draw flowchart for the syntax above.
7
Flowchart of if statement
Test True
expression
Statement (s)
False
8
If Syntax – Part II
if (this logical expression is true) NO SEMI-COLON!
{
statement 1;
}
next_statement;
9
if statement – basic understanding
Is it raining?
OR
True
True
11
If statement – Example 1
#include <stdio.h>
int main() {
int num;
printf("Enter a number to check.\n");
scanf("%d",&num);
if (num<0) { /* checking whether number is less than 0 or not. */
printf("Number = %d\n",num);
}
/*If test condition is true, statement above will be executed, otherwise it will not be executed */
printf("The if statement in C programming is easy.");
return 0; E
} Exercise:
• Draw flowchart for the above code
• Predict and display the output
12
If statement – Example 2
int main ()
{
int num;
printf (“Enter a number between -10 and 10:”);
scanf (“%d”, &num);
if (num > 0)
printf (“%d is a positive number\n”, num);
E return 0;
} Exercise:
• Draw flowchart for the above code
• Predict and display the output
13
If statement – Example 3
#include <stdio.h>
int main (void) {
int number = 0;
printf (“\nEnter an integer between 1 and 10);
scanf (“%d”, &number);
if (number > 5)
printf (“You entered %d which is greater than 5\n”, number);
if (number < 6)
printf (“You entered %d which is less than 6\n”, number);
return 0;
} E
Exercise:
• Draw flowchart for the above code
• Predict and display the output
14
If statement – Example 4
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a); E
return 0; Exercise:
• Draw flowchart for the above code
} • Predict and display the output
15
Two-way statement: if…else
Specifies and action to be performed both when the
condition is true and when it is false
If the boolean expression (or statements) evaluates to
true, then the if block of code will be executed,
otherwise else block of code will be executed.
16
If…else syntax
NO SEMI-COLON!
if (this logical expression is true)
{
statements(s) will be execute if the boolean expression
is true
}
NO SEMI-COLON!
else
}
statement(s) will be execute if the boolean expression is false
}
Exercise:
Draw flowchart for the syntax above.
17
Flowchart of if…else statement
True
Test
expression
False
If code
Else code
18
If…else statement – basic understanding
else
Exercise:
• Draw flowchart for the above statement
19
If…else statement – Example 1
#include <stdio.h>
nt main ()
{
int num;
printf (“Enter a number between -10 and 10;”);
scanf (“%d”, &num);
if (num > 0 )
printf(“%d is a positive number\n”, num);
else
printf (“%d is a negative number\n”, num);
return 0;
} Exercise:
• Draw flowchart for the above code
• Predict and display the output
20
If…else statement – Example 2
#include <stdio.h>
int main () {
int number;
printf (“Enter a number you want to check.\n”);
scanf (“%d”, &num);
if ((number%2) == 0) /*checking whether remainder is 0 or not*/
printf (%d is even.”, num);
else
printf (“%d is odd.”, num);
return 0;
E
}
Exercise:
• Draw flowchart for the above code
• Predict and display the output
21
If…else statement – Example 3
#include <stdio.h>
int main () {
int a = 100;
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" );
}
else {
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
E
printf("value of a is : %d\n", a);
Exercise:
return 0; • Draw flowchart for the above code
} • Predict and display the output
22
Multi-statement: if…else if
An if…else if control structure shifts program control,
step by step, through a series of statement blocks.
Is used when program requires more than one test
expression.
Very useful to test various conditions using single if…else
statatement.
23
If…else if syntax
if (test expression1) {
statements(s) to be execute if test expression1 is true;
}
else if (test expression2) {
statement(s) to be execute if test expression1 is false and 2 is true;
}
else if (test expression3) {
statement(s) to be execute if test expression1 and 2 are false and 3 is true;
}
.
.
else {
statements to be executed if all test expressions are false;
}
Exercise:
Draw flowchart for the syntax above.
24
If…else if statement – Example 1
#include <stdio.h>
int main(){
int numb1, numb2;
printf("Enter two integers to check".\n);
scanf("%d %d", &numb1,&numb2);
if(numb1==numb2) /*checking whether two integers are equal*/.
printf("Result: %d = %d",numb1,numb2);
else if(numb1>numb2) /*checking whether numb1 is greater than numb2*/
printf("Result: %d > %d",numb1,numb2);
else
printf("Result: %d > %d",numb2,numb1);
Exercise:
return 0; • Draw flowchart for the above code
} • Predict and display the output
25
If…else if statement – Example 2
#include <stdio.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 ); Exercise:
• Draw flowchart for the above code
return 0; • Predict and display the output
26
}
Compound (Block of)Statement:
Any block of the code is written or embedded inside the
pair of the curly braces.
If condition specified inside the ‘if block’ is true, then
code block written inside the pair of curly braces will be
executed
If expression evaluates to false, all the statements between
the braces following the else will be executed
In either case, execution continues with next statement.
27
Compound of statement: syntax
if (expression)
{
StatementA1;
StatementA2;
…..
}
else
{
StatementB1;
StatementB2;
…..
}
Next_Statement;
Exercise:
Draw flowchart for the syntax above.
28
Compound Statement– basic understanding
else
Exercise:
• Draw flowchart for the above statement
29
Compound statement– Example 1
int main ( )
{
int num = 10 ;
if(num > 0)
{
printf ("\nNumber is Positive");
printf ("\nThis is The Example of Compound Statement");
}
} Exercise:
• Draw flowchart for the above code
• Predict and display the output
30
Compound statement– Example 2
if (iAge > 17 )
{
printf (“Eligible to take car driving license\n”);
print (“Allow to register at driving school\n”);
}
else
{
printf (“Not eligible to drive\n”);
printf (“Not allows to register at driving school\n”);
Exercise:
} • Draw flowchart for the above code
• Predict and display the output
31
END
32