If-Else Statements: Branching
If-Else Statements: Branching
if-else statements
Conditional Statements
• A conditional statement lets us choose which
statement will be executed next
1
The if Statement
• The if statement has the following syntax:
if ( condition )
statement;
2
Logic of an if statement
condition
evaluated
true
false
statement
Relational Operators
• A condition often uses one of C's equality
operators or relational operators
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
3
The if-else Statement
• An else clause can be added to an if statement to
make an if-else statement
if ( condition )
statement1;
else
statement2;
4
Logic of an if-else statement
condition
evaluated
true false
statement1 statement2
5
Logic of previous example
marks >= 60
true false
#include <stdio.h>
main()
{
int n,r;
printf(“Enter a number:“);
scanf(“%d”, &n);
r = n%2;
if ( r == 0 )
printf( “EVEN\n" );
else
printf(“ODD\n”);
}
6
Block Statements
• Several statements can be grouped together into a
block statement delimited by braces
Block Statements
• Several statements can be grouped together into a
block statement delimited by braces
7
Examples
• Write down a program that will take two integers
as input and will print the maximum of two.
s=4 m = 21
Third element
c
8
Examples
9
Logic of an if-else if-else statement
false
condition 1
evaluated
false
true condition 2
evaluated
statement1
true condition k false
evaluated
statement2
statement
true
statement3
Example 1
The following chart will be used for a quick grade
conversion in C programming language course:
10
Example 2
It takes about
Precisely 365365.2425
it takes days to complete
days! one rotation
11
Leap year explained
Adjustments are needed!
12
• #include <stdio.h>
main()
{
int year;
printf(“Enter year: “);
scanf(“%d”, &year);
if ( year%400 == 0 )
printf( “Leap Year\n" );
else if ( year%100 == 0 )
printf( “ Not a Leap Year\n" );
else if ( year%4 == 0 )
printf( “Leap Year\n" );
else
printf( “ Not a Leap Year\n" );
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
13
Combining multiple conditions:
Logical Operators
• C defines the following logical operators:
! Logical NOT
&& Logical AND
|| Logical OR
Logical NOT
• The logical NOT operation is also called logical
negation or logical complement
• If some condition a is true, then !a is false; if a is
false, then !a is true
a !a
true false
false true
14
Example
• Selection structure:
– Used to choose among alternative courses of action
– Pseudocode: If student’s mark is greater than or equal to 40
Print “Passed”
• Pseudocode statement in C:
#include <stdio.h>
main()
{
float marks;
printf(“Enter your marks: “);
scanf(“%f”, &marks);
if ( marks >= 60 )
printf( "Passed\n" );
}
Example
• Selection structure:
– Used to choose among alternative courses of action
– Pseudocode: If student’s mark is greater than or equal to 40
Print “Passed”
• Pseudocode statement in C:
#include <stdio.h>
main()
{
float marks;
printf(“Enter your marks: “);
scanf(“%f”, &marks);
if (!( marks < 60 ))
printf( "Passed\n" );
}
15
Logical AND and Logical OR
• The logical AND expression
a && b
a || b
Logical Operators
• A truth table shows all possible true-false
combinations of the terms
a b a && b a || b
16
Example 1
The following chart will be used for a quick grade
conversion in C programming language course:
Example
• Write down a program that will take an English
letter as input and will determine whether it is a small
letter or capital letter.
17
ASCII chart
Example:
• Write a C program that calculates the discount of
Agora shop. The discount rate depends on the
purchase amount and provided below:
18
Example
A triangle is valid if sum of its two sides is greater
than the third side. Now, write a C program to check
the validity of a triangle if the three sides are given.
19
#include <stdio.h>
main()
{
int year;
printf(“Enter year: “);
scanf(“%d”, &year);
if ( BLUE OR GREEN )
printf( “Leap Year\n" );
else
printf( “Not a Leap Year\n" );
#include <stdio.h>
main(){
int year;
printf(“Enter year: “);
scanf(“%d”, &year);
if ((year%400 == 0)||((year%4 == 0) && (year%100 != 0))
printf( “Leap Year\n" );
else
printf( “Not a Leap Year\n" );
20
Boolean Expressions in C
• C does not have a boolean data type.
Values on condition
• Zero (0) False
• Anything nonzero TRUE
if (40)
printf(" Hi \n" );
else
printf(“ Bye \n" );
Output: Hi
if (-40)
printf(" Hi \n" );
else
printf(“ Bye \n" );
Output: Hi
21
Relational Operators
• Zero (0) False
• Anything nonzero TRUE
if (0)
printf(" Hi \n" );
else
printf(“ Bye \n" );
Output: Bye
a = 40;
if (a)
printf(" Hi \n" );
else
printf(“ Bye \n" );
Output: Hi
Relational Operators
• Zero (0) False
• Anything nonzero TRUE
a = 0;
if (a)
printf(" Hi \n" );
else
printf(“ Bye \n" );
Output: Bye
a = 30;
if (a = 0)
printf(" Hi \n" );
else
printf(“ Bye \n" );
Output: Bye
22
Relational Operators
• FALSE Zero (0)
• TRUE One (1)
a = 5;
printf("%d ", a > 5 );
Output: 0
a = 5;
printf("%d ", a == 5 );
Output: 1
Short-Circuited Operators
• The processing of logical AND and logical OR is
“short-circuited”
23
The Conditional Operator
• C has a conditional operator that uses a boolean
condition to determine which of two expressions
is evaluated
24
The Conditional Operator
• The conditional operator is similar to an if-else
statement, except that it is an expression that
returns a value
• For example:
larger = ((num1 > num2) ? num1 : num2);
Example:
• Write a C program that will find the absolute value
of a number. You can only use the ternary
operator.
25
The switch Statement
• The switch statement provides another way to
decide which statement to execute next
26
The switch Statement
• The general syntax of a switch statement is:
27
The switch Statement example
• Write down a program using switch structure that
will take an integer as input and will determine
whether the number is odd or even.
switch (n%2){
case 0:
printf(“It is Even”);
break;
case 1:
printf(“It is ODD”);
break;
}
switch (n%3){
case 0:
printf(“It is Multiple of 3”);
break;
case 1:
printf(“No it’s not”);
break;
case 2: printf(“No it’s not”);
break;
}
28
The switch Statement example
• Write down a program using switch structure that
will take an integer as input and will determine
whether the number is multiple of 3 or not.
switch (n%3)
{
case 0:
printf(“It is Multiple of 3”);
break;
default:
printf(“No it’s not”);
break;
}
This is deliberate….
scanf(“%c”,&ch);
switch (ch)
{
case ‘a’: printf(“It is Vowel”);
break;
case ‘e’: printf(“It is Vowel”);
break;
……
29
This is deliberate….
scanf(“%c”,&ch);
switch (ch)
{
case ‘a’: printf(“It is Vowel”);
break;
case ‘e’: printf(“It is Vowel”);
break;
……
This is deliberate….
scanf(“%c”,&ch);
switch (ch)
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’: printf(“It is Vowel”);
break;
default: printf(“It is consonant”);
}
30
Limitations of the switch Statement
• The expression of a switch statement must result
in an integral type, meaning an integer (byte,
short, int,) or a char
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
31
THE END
32