Programming Language
BRANCHING
if-else statements
Conditional Statements
• A conditional statement lets us choose which statement will be
executed next
• Therefore they are sometimes called selection statements
• Conditional statements give us the power to make basic decisions
• The C conditional statements are the:
▫ if statement
▫ if-else statement
▫ if-else if-else if-else ladder
▫ switch statement
▫ Conditional operator (?:)
The if Statement
The if statement has the following syntax:
The condition must be a Boolean expression. It
must evaluate to either true or false.
if is a C reserved word
if(condition)
statement;
If the condition is true, the statement is executed.
If it is false, the statement is skipped.
The if Statement (Example)
• Selection structure:
– Used to choose among alternative courses of action
– Pseudocode: If student’s mark is greater than or equal to 60
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" );
}
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
• Note the difference between the equality operator (==) and the
assignment operator (=).
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;
• If the condition is true, statement1 is executed; if the condition is
false, statement2 is executed
• One or the other will be executed, but not both
The if-else Statement (Example)
• Selection structure:
– Used to choose among alternative courses of action
– Pseudocode: If student’s mark is greater than or equal to 60
Print “Passed”
otherwise
Print “Failed”
• Pseudocode statement in C:
#include<stdio.h>
main() {
float marks;
printf(“Enter your marks: “);
scanf(“%f”, &marks);
if( marks >= 60 )
printf( "Passed\n" );
else
printf( “Failed\n" );
}
Block Statements
In an if-else statement, the if portion, or the else portion, or both,
could be block statements
if(b == 0)
{
printf(“divide by zero!!");
errorCount++;
}
else
{
result = a/b;
printf (“Result of division: %d“, result);
}
Example
• Write down a program that will take two integers as input and will
print the maximum of two.
• Write down a program that will take three integers as input and will
print the maximum of three.
• Write down a program that will take three integers as input and will
print the second largest.
• Write a C program that calculates weekly wages for hourly
employees. Number of hours worked in a week will be input to your
program
Regular hours 0-40 are paid at the rate of $10/hours.
Overtime (> 40 hours per week) is paid at the rate of 150% of regular hourly
rate
The if-else if-else if –else ladder
• If-else if- else if –else can be used to select from multiple choices:
if ( condition1 )
statement1;
else if ( condition2 )
statement2;
…
…
else if ( conditionk )
statementk;
else
statement;
• If the condition1 is true, statement1 is executed; if condition2 is
true, statement2 is executed; and so on
Example
• The following chart will be used for a quick grade conversion in C
programming language course:
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
• Write down a program that will take a student’s mark as input and
will convert it to the corresponding letter grade.
Combining multiple conditions:
Logical Operators
• C defines the following logical operators:
! Logical NOT
&& Logical AND
|| Logical OR
• Logical NOT is a unary operator (it operates on one operand)
• Logical AND and logical OR are binary operators (each operates on
two operands)
The Conditional Operator
• C has a conditional operator that uses a Boolean condition to
determine which of two expressions is evaluated
• Its syntax is:
condition ? expression1 : expression2
• If the condition is true, expression1 is evaluated; if it is false,
expression2 is evaluated
• The value of the entire conditional operator is the value of the
selected expression
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);
• If num1 is greater than num2, then num1 is assigned to larger;
otherwise, num2 is assigned to larger
• The conditional operator is ternary because it requires three
operands
Example
• Write a C program that will find the absolute value of a number.
You can only use the ternary operator.
• Write a C program that will find the minimum, maximum and
second largest of three integers given as input. You can only use the
ternary operator.
The switch Statement
• The switch statement provides another way to decide which
statement to execute next
• The switch statement evaluates an expression, then attempts to
match the result to one of several possible cases
• Each case contains a value and a list of statements
• The flow of control transfers to statement associated with the first
case value that matches
The switch Statement
• Often a break statement is used as the last statement in each case's
statement list
• A break statement causes control to transfer to the end of the switch
statement
• If a break statement is not used, the flow of control will continue
into the next case
• Sometimes this may be appropriate, but often we want to execute
only the statements associated with one case
The switch Statement
• The general syntax of a switch statement is:
switch switch ( expression ) {
and case case value1 : statement-list1
are case value2 : statement-list2
reserved case value3 : statement-list3
words case ...
}
If expression matches
value2, control jumps
to here
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;
}
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
• It cannot be a floating point value (float or double)
• The implicit test condition in a switch statement is equality
• You cannot perform relational checks with a switch statement