0% found this document useful (0 votes)
17 views

Chapter 3

The document discusses different conditional statements in C programming including if, if-else, nested if, conditional operator, and switch-case statements. It provides examples and explanations of how each statement works and how to write them. It also notes some key aspects like using braces, relational vs assignment operators, and using break in switch-case statements.

Uploaded by

benrjebfatma18
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)
17 views

Chapter 3

The document discusses different conditional statements in C programming including if, if-else, nested if, conditional operator, and switch-case statements. It provides examples and explanations of how each statement works and how to write them. It also notes some key aspects like using braces, relational vs assignment operators, and using break in switch-case statements.

Uploaded by

benrjebfatma18
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/ 34

Chapter 3:

Conditional Statements

1
Chapter outcomes
• By the end of this chapter, the students will
• know how to create decision making statements using if ..
else statements
• know how to write decision making statements using switch
.. case statement
• be able to solve complex problems using the concepts
learned

2
Decision Control statement
• In decision control statements, a group of statements are
executed when the condition is true.

• If condition is false, then else part statements are executed.

• There are 4 types of decision making control statements in C


language. They are:
1. The if Statement
2. The if..else statements
3. Nested..if statements
4. switch..case statements
3
The if Statement
• The C programming language provides a general decision-
making capability in the form of a language construct
known as the if statement.
• Syntax: Flowchart:

If (condition) {
Statement/s to be executed if
condition is true;
}

If the result of the test condition is true,


then the instructions inside the IF body are
executed. If the result of the condition is
false, then the statements are skipped. 4
The if Statement
Algorithm example: Equivalent in C
Algorithm comparison #include<stdio.h>
main()
Var {
a,b : integer int a, b;
printf(“Enter two integers:\n“);
scanf(“%d %d“,&a, &b);
Begin
// a=5, b=5
if (a == b)
1- read(a,b)
{
2- If (a=b) printf (“ %d is equal to %d“
write (a, “is equal to”, b) ,a,b);
}
End } 5
Output: 5 is equal to 5
If….Else
• In this type of statements, group of statements are executed
when condition is true.
• If condition is false, then else part statements are executed.
• Syntax: Flowchart:
If (condition)
{ Statement/s to be executed if
condition is true;}
else
{ Statement/s to be executed if
condition is false;}

The IF statement is executed if the


statement inside the test condition
evaluates to true; otherwise the statements 6
inside the ELSE block is executed.
If….Else
Algorithm example: Equivalent in C:
Algorithm comparison #include<stdio.h>
main()
Var {
a,b : integer int a, b;
printf(“Enter two integers:\n“);
Begin scanf(“%d %d“,&a, &b);
// a=5, b=20
1- read(a,b) if (a == b)
2- if (a=b)
{
write (a, “is equal to”, b)
printf (“ %d and %d are equal“ ,a,b);
else
}
write ( a, “and”, b, “are
not equal”) else
End {
printf(“%d and %d are not equal“,a,b);
}
} 7

Output: 5 and 20 are not equal


Conditonal operator
• As conditional operator works on three operands, so it is also known as the
ternary operator.
• The behavior of the conditional operator is similar to the 'if-else' statement
as 'if-else' statement is also a decision-making statement.
• Note: the instruction is used only if we have one statement in if part and
one in else part.
If (condition)
Statement 1;
else
Statement 2;

condition ? Statement1 : Statement 2;


• If codition is true , Statement 1 is evaluated and its value is returned.
• If condition is false, then Statement 2 is evaluated and its value becomes the
value of the expression. 8

• This form has the advantage of brevity and is appropriate for simple
conditionals.

Examples with conditional operator
Example 1:
To assign the maximum of a and b to z: if (a>b)
z = (a>b) ? a : b; z = a;
which is the same as: else
Example 2: z=b;

9
Nested if
• The if...else statement can be used in nested form when
serious decisions are involved.
• This can be very useful if you want to test various types of
scenarios and situations.

• If the test condition is true, it will execute the code before else
block but, if it is false, the control of the program jumps to the
else block and check test condition 1 and the process
continues.

• If all the test expression are false then, the last statement is
executed.
10
• The ANSI standard specifies that 15 levels of nesting may be
continued.
Algorithm example
Algorithm Comparison

Var
n1,n2 : integer
Begin
1-write(“enter two integers to check”)
1- read(n1,n2)
2- if (n1=n2)
write (n1, “=”, n2)
else
if (n1>n2)
write (n1, “>”, n2)
else
write (n1, “<”, n2) 11
End
Equivalent in C

12
Notes
• If any action consists of more than one statement, it is enclosed within a
pair of braces.
if ( codition ) { statement1 statement2 }
• Opening and Closing braces are not required if there is only one
Statement after if.
• Opening and Closing braces are not required if there is only one
Statement after else.
• Block followed after if and before else is called “if block”
• We can write any number of statements inside block if condition is true
• if there is no statement after else then else can be dropped
• More than one Conditions can be Combined inside if
• Nested Control Statements are allowed
• "==" is a relational operator and "=" is an assignment operator.
• ! Operator Reveres the Value of Expression
13
• Logical && operator indicates “both Conditions must be TRUE”
• Logical || operator indicated “Either Conditions must be TRUE”
switch .. case statements
• If a programmer has to choose one among many alternatives,
if...else can be used but, this makes programming logic complex.
• This type of problem can be handled in C programming using
switch...case statement.
• Syntax:
switch (expression)
{
Note use of colon!
case constant1:
Instruction/s to be executed if expression equals to constant1;
break;
case constant2:
Instruction/s to be executed if expression equals to constant2;
break;
….
default: 14
Instruction/s to be executed if expression doesn't match to any cases;
}
switch .. case statements
Flowchart: :

15
Example 1:
Algorithm Appreciation
Var
grade : character
Begin
1. write( "Enter your current letter grade")
2. read(grade)
3. switch (grade)
{
case ('a') : case ('A') :
write ("Good Job!") ;
case ('b') : case ('B') :
write ("Pretty good.”) ;
case ('c') : case ('C') :
write ("Better get to work.") ;
case ('d') : case ('D') :
write ("You are in trouble.") ;
default :
write("You are failing!!") ; 16
}
End
Equivalent in C
#include <stdio.h>

int main ( )
{ •Input: ‘A’
char grade ;
printf ("Enter your current letter grade\n") ;
grade = getchar ( ) ;
switch (grade) • Output :
{
case ('a') : case ('A') :
Good Job!
printf ("Good Job!\n") ;
Pretty good.
case ('b') : case ('B') : Better get to work.
printf ("Pretty good.\n") ; You are in trouble.
case ('c') : case ('C') : You are failing!
printf ("Better get to work.\n") ;
case ('d') : case ('D') : Note: The input entered
printf ("You are in trouble.\n") ; matches the first case but
default : every action in the structure is
printf ("You are failing!!\n") ; executed. 17
} /* End of switch-case structure */
} /* End of main program
switch .. case statements : Notes

• If the expression does not match any of the constants in


cases, then the default statement is executed.

• In switch .. case statement, when the value in a case


matches the switch expression value, all the instructions
inner the following cases and the default will be executed
even they don’t match the test value.

18
Use of break;
• The problems with the previous program can be
corrected by use of the break statement.
• It can be used in either a conditional structure or a
repetition structure to break out of (that is, to exit from)
the structure.
• The syntax is:
break ;
• The following program illustrates an example with the
addition of the break statements.

19
Example 1 using break statement
#include <stdio.h>
main ( )
{ char grade ;
printf ("Enter your current letter grade\n") ;
grade = getchar ( ) ; • Input: ‘A’

switch (grade)
{ • Output : Good Job!
case ('a') : case ('A') :
printf ("Good Job!\n") ;
break;

case ('b') : case ('B') :


printf ("Pretty good.\n") ;
break;
case ('c') : case ('C') :
printf ("Better get to work.\n") ;
break;
case ('d') : case ('D') :
printf ("You are in trouble.\n") ;
break;
default :
printf ("You are failing!!\n") ;
} /* End of switch-case structure */ 20
} /* End of main program
Example 2 using break statement
Algorithm Guess_value
Var
value : integer

Begin
1. value ← 3
2. switch (value)
{
case 1 : write(" Value is 1”)
break
case 2 : write(" Value is 2”)
break
case 3 : write(" Value is 3”)
break
case 4 : write(" Value is 4”)
break
default
write(" Value is other than 1, 2, 3, 4”)
} 21
End
Equivalent in C
With switch ,, case statement With if .. else statement

22

Output: Value is 3 Output: Value is 3


Rules of Using Switch Case in C
Programming
1. Case Label must be unique
2. Case Labels must end with Colon
3. Case labels must be constants / constant expression
4. Case label must be of integral Type ( Integer,Character)
5. Switch case should have at most one default label
6. Default label is Optional
7. Default can be placed anywhere in the switch
8. Break Statement takes control out of the switch
9. Two or more cases may share one break statement
10. Nesting ( switch within switch ) is allowed.
11. Relational Operators are not allowed in Switch Statement. 23
Rule 1 : Case Label must be unique

int id = 3 ;
switch(id)
{
case 1:
printf("C Programming Language");
break;
case 2:
printf("C++ Programming Language");
break;
case 2:
printf("Web Technology");
break;
default :
printf("No student found");
break;
} 24
Rule 2 : Case Labels must end with Colon
case 1 :
printf("C Programming Language");
break;
Rule 3 : Case labels must be constants / constant
expression
case 1+1:
case 'A':
case 67:
=> These are allowed examples of switch case labels , however variables
are not allowed in switch case

case var : 25
case num1 :
case n1+n2 :
Rule 4 : Case label must be of integral Type (Integer,
Character)

Whereas case label should not be ‘floating point number ‘

These are allowed examples:


case 10:
case 20+20:

case 'A':
case 'a':

Following are illegal examples :

case 10.12:
case 7.5:
26
Rule 5 : Switch case should have at most one default
label
switch(roll)
{
case 1:
printf("C Programming Language");
break;
case 2:
printf("C++ Programming Language");
break;
case 3:
printf("Web Technology");
break;
default :
printf("Default Version 1");
break;
default :
printf("Default Version 2"); 27
break;
}
Rule 6 : Default label is Optional
switch(roll)
{
case 1 :
printf("C Programming Language");
break;
case 2 :
printf("C++ Programming Language");
break;
case 3 :
printf("Web Technology");
break;
}

28
default statement is optional. It can be neglected.
Rule 7 : Default can be placed anywhere in the switch
switch(roll)
{
case 1 :
printf("C Programming Language");
break;
default:
printf("No Student Found");
break;
case 2 :
printf("C++ Programming Language");
break;
case 3 :
printf("Web Technology");
break; 29
}
Rule 8 : Break Statement takes control out of the switch
Rule 9 : Two or more cases may share one break statement

switch(alpha)
{
case 'a':
case 'A':
printf("Alphabet A");
break;
case 'b':
case 'B':
printf("Alphabet B");
break;
}
30
Rule 10 : Nesting ( switch within switch ) is allowed

switch(alpha)
{case 'a':
case 'A':
printf("Alphabet A");
break;
case 'b':
case 'B':
switch(beta)
{ …
}
break;
} 31
Rule 11 : Relational Operators are not allowed in
Switch Statement

switch(num)
{
case >15:
printf("Number > 15");
break;
case =15:
printf("Number = 15");
break;
case <15:
printf("Number < 15");
break; 32
}
Rule 12 : Const Variable is allowed in switch Case
Statement

int const var = 2;

switch(num)
{
case var:
printf("Number = 2");
break;
}

33
Exercise :
• Write an algorithm called MONTH, which displays the name of the month
corresponding to a month number entered by the user

Algorithm MONTH
Var
m:integer
Begin
1. write(" Enter a month number")
2. read(m)
3. switch (m)
{
Case 1 : write ( "January’’)
Case 2 : write ( " February ‘’)
…………..
Case 12 : write ( " December’’)
Default
write (‘’error ! ! ! ! ‘’) 34
}
End

You might also like