CLang Lect06
CLang Lect06
SOICT-HUST
if statement
if ( expression )
statement
• Determines whether a statement or block is
executed.
• Implements the selection instructions within
an algorithm.
• Decides what to do by evaluating a Boolean
expression.
• If the expression is true (non-zero), the
statement or block is executed.
2
What is a statement?
• Statements are lines of instructions in our
programs ending with a semicolon (;).
• A compound statement or block is a series of
statements surrounded by braces.
Example {
number = number + 1;
printf("%d\n", number);
}
return 0;
}
4
Common errors
Should be equal
comparison == No ; here
if (number % 2 = 0);
{
printf(“%d\n”, number);
printf(“La so chan");
}
5
else statement
if ( expression )
statement1
else statement2
int main()
{
int number;
if (number % 2 != 0)
printf("%d is an odd number\n", number);
else
printf("%d is an even number\n", number);
return 0
}
7
Common errors
no ; here
if (number % 2 != 0)
{
printf(“%d\n”, number);
printf(“is an odd number");
};
else
{
printf(“%d\n”, number);
printf(“is an even number");
}
8
Cascading if (else-if)
Example
if (expr1) if (ch >= ’a’ && ch <= ’z’)
{
statement1 printf(“%c is a lowercase”, ch);
else if (expr2) }
else if (ch >= ’A’ && ch <= ’Z’)
statement2 {
else if (expr3) printf(“%c is a upper case”. ch);
}
statement3 else if (ch >= ’0’ && ch <= ’9’)
else {
printf(“%c is a number”, ch);
statement4 }
•Cascading if: Multiple alternative blocks but at most only one block
will be executed
•Cascading if is used when we need to choose one among several
conditions 9
Exercise
1. Write a program to compute the total days of a
month
• Algorithm
if (month in September, April, June, November) then
output “30 days”
else if (month is February)
output “28 or 29 days”
else output “31 days”
10
Exercises
2. Write a program to get three numbers from input
and print out the maximum of those
3. Write a program to solve ax2 + bx + c = 0
4. Write a program to get two numbers a,b from input
and compute y = 15x2 + x + 12, in which:
a + b
3 + b if a b
x = 15,172 if a = b
a −b
a 2 + b 2 if a b
11
switch statement
switch (integer value)
{
case 1: statement1;
break; /* optional line */
case 2: statement2;
break; /* optional line */
....
default: default statement;
break; /* optional line */
}
• When a switch statement is encountered, the expression in the
parentheses is evaluated and the program checks to see whether the result
of that expression matches any of the constants labelled with case.
• If a match is made execution will start just after that case statement and will
carry on until either the closing brace } is encountered or a break statement
is found.
• Statements which follow the default case are executed for all cases which
are not specifically listed.
12
Example 1
printf(“Yes/No (Y/N)?”);
scanf(“%c”, &ch)
switch (ch)
{
case 'y' :
case 'Y' :
printf(“say yes”);
break;
default :
printf(“say no”);
}
13
Example 2
switch (digit){
case 0 : printf ("zero");
break;
case 1 : printf ("one");
break;
case 2 : printf ("two");
break;
...
case 9 : printf ("nine");
break;
default:
printf ("others");
}
14
Exercises
• Display grade of a student based on
marks
• diem = 9, 10: excellent
• diem = 7, 8: good
• diem = 5, 6: average
• other: weak
15
using break
• When a case of the switch statement is found, statements are
carried out from this point
• All following statements are carried out until a break statement
• break is a handy way of jumping straight out of the switch block
int a=1;
switch ( a ) {
case 1:
printf("a=1\n");
case 2: Output:
printf("a=2\n"); a=1
break; a=2
case 3:
printf("a=3\n");
} 16
Exercises
1. Write a program to get two numbers a,b from
input and compute y = 15x2 + x + 12, in which:
a + b
3 + b if a b
x = 15,172 if a = b
a −b
a 2 + b 2 if a b
2. Write a program to get an integer n (1 ≤n≤10)
and display the English name of that number.
For example, n = 2, display 2 → two.
17
Thank you
for your
attentions!