0% found this document useful (0 votes)
9 views18 pages

CLang Lect06

The document discusses if statements, else statements, and switch statements in C programming. It provides examples of using if-else statements to check if a number is odd or even, and using a switch statement to check the grade based on a student's marks. It also discusses common errors like missing semicolons, using = instead of == in comparisons, and the need for break statements in switch blocks. Exercises provided include using if-else statements to find the maximum of three numbers and determine the total days in a month based on the month number.

Uploaded by

Thành
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)
9 views18 pages

CLang Lect06

The document discusses if statements, else statements, and switch statements in C programming. It provides examples of using if-else statements to check if a number is odd or even, and using a switch statement to check the grade based on a student's marks. It also discusses common errors like missing semicolons, using = instead of == in comparisons, and the need for break statements in switch blocks. Exercises provided include using if-else statements to find the maximum of three numbers and determine the total days in a month based on the month number.

Uploaded by

Thành
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
You are on page 1/ 18

BRANCH

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);
}

• An empty statement is a single semicolon.


3
Example
• Read in a number, and echo it if it is odd.
#include <stdio.h>
int main()
{
int number;

printf(“Enter an integer: ");


scanf("%d", &number); there is no
then here
if (number % 2 != 0)
printf(“%d is an odd number“, number);

return 0;
}
4
Common errors

Should be equal
comparison == No ; here

if (number % 2 = 0);
{
printf(“%d\n”, number);
printf(“La so chan");
}

; creates an empty statement after if

5
else statement
if ( expression )
statement1
else statement2

• else statement can only occur after an if


statement
• else statement is only executed when the
if block does not execute
6
Example
• Check whether an interger is odd or even
#include <stdio.h>

int main()
{
int number;

printf(“Enter an integer: ");


scanf("%d", &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!

You might also like