0% found this document useful (0 votes)
41 views22 pages

08 - Conditional Statement

This document discusses different conditional statements in C programming including if statements, else if ladders, ternary operators, and switch statements. It provides examples of how to use each statement, describes when to use them, and includes activities for practicing each statement type.

Uploaded by

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

08 - Conditional Statement

This document discusses different conditional statements in C programming including if statements, else if ladders, ternary operators, and switch statements. It provides examples of how to use each statement, describes when to use them, and includes activities for practicing each statement type.

Uploaded by

ronny duc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture 08: Conditional

Statements
Objectives

• Basic programming constructs


• Sequence
• Selection statements
• 'if' statements
• Conditional/ternary/?: operator
• Switch statement

2
Basic Programming Constructs

• Basic programming constructs are


– Sequence, selection, and iteration (looping)
• Sequence construct
– Instructions are executed in the same order as they appear
• Selection structure
– Control flow can be changed by evaluating conditions
• Iterative structure
– A group of instructions is executed repeatedly
– Until some condition is satisfied

3
Statements in C

• Simple statement (expression statement)


– It is terminated by a semicolon (;)
– E.g., int a = 8;
• Compound statements/blocks
– Used to group statements into a single executable unit
– Includes of one or more statements enclosed within {}
– E.g.
{
int a = 10;
int b = 11;
int r = a+b;
}

4
Sequence

• A program, which consists int main(int argc, char *argv[]) {


// declare a variable of type string
of char s[10]; // s cannot have more than 10 characters
// ask user to enter a name
– Declaration statements printf("Enter a name: ");
// use gets to get a string from console
– Input-output statements gets(s);
– One or more simple // print result:
printf("Here is a name: \n");
expression statements puts(s);
return 0;
}

5
Selection/Conditional Statements

• Used to alter normal flow of control


• Provides ability to decide order of execution
• Selection constructs in C are
– “if” statement
– Conditional/Ternary operator statement (? : )
– “switch” statement

6
“if” Statement

• Allows us to establish decision-making in the programs


• Allow programs to test certain condition and make
decision about which code block to be executed
• The if statement has two basic forms
– if-else
– if-else if

7
Simple if-else

false true
if (condition){
//one or more statements;
}

false true
if (condition){
//one or more statements;
}else {
//one or more statements;
}

8
Example simple if example

void main()
{
int x;
printf(“Please input x: ");
scanf(“%d”, &x);
if(x > 10){
printf(”x is greater than 10");
} else {
printf(”x is smaller than or equal to 10");
}
}

9
Activity: Find maximum

• Create a program to
– Ask user to input two integers
– Find and display the larger one

10
if-else if ladder

• The condition is evaluated sequentially


– From the top of the ladder and moving downwards

if (condition){
//one or more statements;
}else if (condition) {
//one or more statements;

}else {
//one or more statements;
}

11
Example if-else if ladder

void main()
{
int x;
printf(“Enter your choice (1 - 3) : “);
scanf(“%d”, &x);
if (x == 1)
printf (“\nYour choice is one”);
else if ( x == 2)
printf (“\nYour choice is two”);
else if ( x == 3)
printf (“\nYour choice is three”);
else
printf (“\nInvalid choice“);
}

12
Activity: if-else if ladder

• Make a program to find grade classification


– Ask user to input a grade (ranging from 0 to 100)
– If that grade is < 40 -> Fail
– If that grade is >= 40 and < 50 -> 3rd class
– If that grade is >= 50 and < 60 -> 2nd class 2nd division
– If that grade is >= 60 and < 70 -> 2nd class 1st division
– If that grade is >= 70 -> first class
– And display corresponding message

13
Conditional/Ternary/? : Operator

• This operator takes 3 expressions/operands


• It is more efficient form for expressing simple if statement
• General form
– [variable = ] expr1 ? Expr2 : expr3
• This simply states
– if(expr1 is true) then expr2 else expr3
• Where
– expr2 is evaluated, if the value of expr1 is non-zero (true)
– expr3 is evaluated, if the value of expr1 is zero (false)

14
Example ternary operator

void main()
{
int x;
printf(“Enter a number from 1 to 10: “);
scanf(“%d”, &x);
int result = (x >= 1 && x <= 10)? 1 : 0;
if(result){
printf(“Valid input“);
}else{
printf(“Invalid input“);
}
}

15
Activity

• Make a program to
– Ask user to input two integers
– Find the larger one using Ternary operator

16
Switch Statements

• Can be used in place of if-else-if statements


• Used in situations where the expression being evaluated
results in multiple values.

17
General form of Switch statement

switch(expression){
case item1:
//one or more statements;
break;
case item2:
//one or more statements;
break;
case itemn:
//one or more statements;
break;
default:
//one or more statements;
break;
}

18
Example switch statement

void main(){
int x;
printf(“Enter a number from 0 to 3: “);
scanf(“%d”, &x);
switch(expression){
case 0:
printf(“The fan is off“);
break;
case 1:
printf(“The fan is running with speed one“);
break;
case 2:
printf(“The fan is running with speed two“);
break;
case 3:
printf(“The fan is running with speed three“);
break;
default:
printf(“Invalid input“);
break;
}
}
19
Activity: Arithmetic operators

• Make a program to
– Ask user to input two numbers
– Ask user to input one of these arithmetic operators represented
by character (‘+’, ‘-’, ‘*’, ‘/’)
– Display “invalid operator” if user inputs the wrong character
– Else, display the corresponding result

20
Activity: vowels

• Make a program to
– Ask user to input a character
– Display an error message if user inputs a non alphabet
character (check with ASCII e.g., ‘a’ < c <‘z’ for lower case
characters)
– If the character is alphabet, check if it is a vowel (‘a’, ‘e’, ‘i’, ‘o’,
‘u’) or a consonant
– Display the result

21
Summaries

• Basic programming constructs


• Sequence
• Selection statements
• 'if' statements
• Conditional/ternary/?: operator
• Switch statement

22

You might also like