0% found this document useful (0 votes)
25 views74 pages

Chapter 5 Control Statement

Uploaded by

Farhana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views74 pages

Chapter 5 Control Statement

Uploaded by

Farhana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Control statements

Dr HO FU HAW
FKMP
-if Statement

• Four types of –if


• if
• Block if or if - else
• Nested if
• Multi alternative if
Simple if statement
• General form
• if (logical expression)
{
statements;
}
• If true, the statements executed,
if false, the statement are skipped
False true
logic

statements
Exercise

Construct a c coding to determine whether a student is


allow to attend graduation 2019. The requirement is
he/she must completed total credit hours = 135.
Using simple if, build a program to
evaluate exam mark..

• Condition 1 : <40
- Failed the exam
- Need to retake the exam

• Condition 2 : >=40
- Passed the exam
- No need to retake the exam
Exercise:
You are allow to vote if your age is 18 or above.
Develop a c program to determine whether you are
allow to vote in the coming general election. If no,
you need to wait for how many years?
• #include <stdio.h>
• int main()
• {
• int year;
• printf("Enter your age: ");
• scanf("%d",&year);

• // True if remainder is 0
• if( year>=21 )
• {printf("your are allow to vote in this general election",year);
• }
• if(year<21)
• {printf("Your not allow to vote in the general election.\n");
• printf("You need to wait %d year(s) to achieve 21 years old", 21-year);
• }
• return 0;}
Example: simple if
• Find y value.
2𝑥𝑥 + 1 ; 𝑥𝑥 < 2
• 𝑦𝑦 = �15𝑥𝑥 − 2 ; 𝑥𝑥 ≥ 2

C-Codes:
if(x<2)
{y=2*x +1;}
if(x>=2)
{y=15*x-2;}
if-else/ block if statement
• General form
• if (logical expression)
False true
{
logic
statements;
}
statements statements
else
{
statements;
}
• If true, the statements executed, if false, else statement will be executed
Example: if…else
• Find y value using if …else .

2𝑥𝑥 + 1 ; 𝑥𝑥 > 15
• 𝑦𝑦 = �
15𝑥𝑥 − 2 ; 𝑥𝑥 ≤ 15

……
if(x>15)
{y=2*x +1;}
else
{y=15*x-2;}
…..
Exercise – develop a c program to
calculate y value when x value is
inserted
5𝑥𝑥 3 ; 10 > 𝑥𝑥 > 0
Y=�
5𝑥𝑥 + 10 ; 15 > 𝑥𝑥 > 11
Exercise : Develop C code for below function
using simple if

5𝑥𝑥 3 ; 10 > 𝑥𝑥 > 0


Y=�
5𝑥𝑥 + 10 ; 15 > 𝑥𝑥 > 7
Build a program to determine
even/odd number…
• Even number: 0,2,4,6,8..
• Odd number: 1,3,5,7,9,11…
*hint: use modulo division (%)
Answer:
Nested if statements
• Nested if...else statement (if...elseif....else Statement)
• Combination of block if statements (within a group of
statement)
• Example
• Z= x+y if x>5, y>1 (first condition)
• Z=x*y if x>5, y<=1 (second condition) (within same group)
• Z=x-5 if x<5 (third condition)
Syntax of nested if ..else…else if….else
Exercise: using nested if

• X<5 y=10+x
• X<15 y=12+x
• X<25 y=13+x
• X<35 y=15+x
Example if…else if…else
Build a program using nested if..elseif..else

Conditions:
- If marks : <40 : Grade E
- If marks : 40 – 50: Grade D
- If marks : 51 – 60: Grade C
- If marks : 61- 80: Grade B
- If marks : 81 – 100: Grade A
Answer:
Exercise

Build a c program to evaluate temperature level to


evaluate weather:
Below 0 : freezing
1 – 10: very cold
11-18: cold
19-25: warm
26-35: hot
Above 35: very hot
Exercise:
Finding the prime number using
if..else if..else
*prime number is a number that
is divisible only by itself and 1
(e.g. 2, 3, 5, 7, 11).

*hint: use modulo division (%)


Fulfilling 2 conditions or more...
Build a program to evaluate 2
numbers
• Number 1 = number 2 : A and B are save value
• Number 1 > number 2 : A is larger than B
• Number 1< number 2 : A is smaller than B

Steps:
1. Enable c-program to insert 2 values
2. First value save in A, second value save in B
3. Compare value A vs B
Build a program to evaluate below
functions
2a+3b-2c a<2;b<5;c<3

3b-2a a<5;b>5;c<10
Y

45b+2c-5 a<10;c>10
Answer:
• #include <stdio.h>
• #include <conio.h>
• int main(void)
• { int a,b,c,y;
• printf("Enter 3 numbers: ");
• scanf("%d%d%d",&a,&b,&c);
• if(a<2 && b<5 && c<3 )
• printf("%d",y=2*a+3*b-2*c);
• else if (a<5 && b>5 && c<10)
• printf("%d",y=3*b-2*a);
• else if (a<10 && b<10 && c>10)
• printf("%d",45*b+2*c-5);
• else
• printf("no condition fulfilled");
• return 0;}
Multi alternative if statement

𝑥𝑥 + 5, 𝑖𝑖𝑖𝑖 𝑥𝑥 < 0
• 𝑦𝑦 = �2𝑥𝑥, 𝑖𝑖𝑖𝑖 0 ≤ 𝑥𝑥 < 2
𝑥𝑥 − 1, 𝑖𝑖𝑖𝑖 𝑥𝑥 ≥ 2

• Modification of if statement with in the form else if


Assignment to be submitted to
Edmodo
Please check your edmodo account, download the
question and submit the assignment before 21/4/2020
(1pm)
The goto statement
• goto statement is used for jumping from one position to other
place within main or some other function
• Format:
……
Label:


goto Label;
Example of c code- Goto code
#include<stdio.h>
#include<conio.h>
int main()
{ int y, x;
Start:
printf("Enter value x:");
scanf("%d",&x);
if (x<2)
{y=2*x+1;
}
if (x>=2)
{y=15*x-2;}
printf("y=%d",y);
goto Start;
getch();
}
Combination of if ..else if..else and
goto….
Build a program to evaluate:
a < 5 goto formula1: y=10a + 5
a < 8 goto formula2: y= 25-2a
a > 8 goto formula3: y= 12a-4

If y value > 20, repeat the evaluation program, else end


the program.
Answer:

• int a,b,c,y;
• start:
• printf("Enter a value: ");
• scanf("%d",&a);

• if(a<5)
• {
• goto formula1;
• formula1:
• printf("%d",y=10*a+5);
• }
• else if(a<8)
• {
• goto formula2;
• formula2:
• printf("%d",y=25-2*a);
• }
• else
• {
• goto formula3;
• formula3:
• printf("%d",y=12*a-4);
• }
• if(y>20)
• goto start;
• return 0;
•}
Exercise:
Develop a single layer password c-code to
unlock your safety box
Hints:
• User to set their own password
• User to test the password (after set pw)
• If first password is
correct, the program will show “door open”,
else the program will request user to try again.
do-while statements

• Similar to while statement, but one difference is that


the logical condition which appears in this statement
testes at the end of the loop
do
statements
{
Statements; False true
logic
}while(logical expression);
The -while statement

• Used for repeated execution of group statements.


• The repetition of the loop is defined only on a
logical expression
while(logical expression)
{ False true
logic
statements;
}
statements
Example:
Exercise:

1. Write a program to count from 1 to 50 using while


function

2. Write a program to count in backward from 100 to 50


using while function
Exercise:

• Write a program to evaluate x! (factorial), where x is


positive integer
Example:
Exercise (using while looping)

• Write a program to find sum of 20 to 30.


Exercise: What are outputs of below codes?

• #include<stdio.h>
• int main()
• {int x=20,sum=0;
• while(x<31)
• {
• sum+=x;
• ++x;}
• printf("total is %d\n",sum);
• return 0;
• }
They are both the same with one exception:

Will only execute if condition evaluate to true. That means it


runs at least zero (0) times

Will execute immediately, before checking the condition. If the


condition evaluates to true the loop continue. That means it runs
at lease one time through.
Guess what are the outputs of below
codes:
• #include <stdio.h>
• int main ()
• {
• int a = 10;
• do
• { if( a == 15)
• printf("value of a: %d\n", a); a++; }
• while( a < 20 );
• return 0;
• }
Do…while
Answer:

• #include<stdio.h>
• int main()
• {
• int Number, i = 1, Sum = 0;

• printf("\nPlease Enter any Integer Value\n");
• scanf("%d", &Number);

• while(i <= Number)
• {
• Sum = Sum + i;
• i++;
• }

• printf("Sum of Natural Numbers = %d", Sum);
• return 0;
• }
Answer:

• #include <stdio.h>
• int main ( )
• { /* local variable definition */
• int a = 10;
• printf("enter your number: ", a);
• scanf("%d",&a);

• /* while loop execution */


• while( a <= 100 ) {
• printf("value of a: %d\n", a);
• a=a+a;
• }
• return 0;}
Example: Do….While looping

#Include <stdio.h>
int main()

{ double number, sum = 0; // loop body is executed at least once

do {
printf("Enter a number: ");
scanf("%lf", &number); sum += number;
}
while(number != 0.0);

printf("Sum = %.2lf",sum);

return 0;
}
Exercise

• Using Do…While looping to display even value


from 0 until 50.
Exercise:
Using Do…While looping to display even value from 30
until 0
‘for’ in c programming
• The “for loop” loops from one
number to another number and
increases by a specified value each
time.
Comma operator for -for
• Comma can be used if expression or statement contained
more than one statement. It can be separated by comma
operator

for(A=0, J=0;A<=10, J>=5; A++,J--)


{statements;

}
for loop
• A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number
of times.
• One or more variable are used to control the loop
• These variables are called the control variable
for(exp1;exp2;exp3)
{
Statements;
}
// exp1- initial value of control variable
//exp2- logical expression
//exp3- unary expression or assignment statement
break statement

• Used to terminate loops and break the control of


the statements
• Generally used in while, do-while, for and switch
statements
• General form
break;
Use break in a while loop
Use break in a for loop
Exercise

Given for(a=10;a<20;a++) , write a code to “break” or


stop this program when a>15.
-continue statement
• Generally used within the loop to bypass or skip
a portion of the loop
• The continue statement causes the loop to be
continued with next iteration after skipping any
statement in between
• General form
continue;
Exercise

• Using “continue” feature, write a program to show


0,2,4,8,10,14), the missing value are 6 and 12.
switch statement
• This statement is mainly used when a particular statement is selected from a
group statements based on the value of an identifier/ expression
• General form
switch(expression)
{
case expression1:
Statements;
break;

case expression2:
Statements;
break;

default:
statements;
}
Exercise

• Lucky draw number: Develop a c code to enable its


recognize the input number and the prize as below
using switch case approach.
No 123 - first prize
No 456 - second prize
No 789 – third prize
Other – no prize
Exercise

Write a program to enable the system to pick a number


any number and prize to be won as below:

no 123 – Myvi
no 456 – EX5 bike
no 789– Raleigh mountain bike
Else: mineral water
• #include<stdio.h>
• int main()
• {int number;

• start:
• printf("key in your number:");
• scanf("%d", &number);
• switch(number)
• {case 123:
• printf("you won a Myvi");
• break;
• case 456:
• printf("you won an EX5 motorbike");
• break;
• case 789:
• printf("you won a Raleigh mountain bike");
• break;
• default:
• printf("too bad, you won a mineral water!!");
• }return 0;}
Exercise
Write a program to enable the system to recognize character to represent a
specific day:

S- Sunday
M- Monday
T- Tuesday
W- Wednesday
t- Thursday
F- Friday
s- Saturday
Thank you.

You might also like