0% found this document useful (0 votes)
28 views47 pages

2 Decision

The document discusses C statements and control flow structures like if/else statements. It provides examples of using if/else statements to check if a number is odd or even, and explains that statements end with semicolons while compound statements are surrounded by braces. Comments are described as a way to explain special cases or non-obvious aspects of the code.
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)
28 views47 pages

2 Decision

The document discusses C statements and control flow structures like if/else statements. It provides examples of using if/else statements to check if a number is odd or even, and explains that statements end with semicolons while compound statements are surrounded by braces. Comments are described as a way to explain special cases or non-obvious aspects of the code.
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/ 47

Decision Making and Branching in

C
Comments
• Comments: /* This is a comment */
o Use them!
o Comments should explain:
• special cases
• the use of functions (parameters, return values, purpose)
• special tricks or things that are not obvious
o explain WHY your code does things the what it does.
C Statements
• In the most general sense, a statement is a part of your
program that can be executed.
• An expression is a statement.
a=a+1;
a--;
• A function call is also a statement.
printf("%d",a);
• Other statements ……
• C is a free form language, so you may type the statements in
any style you feel comfortable:
a=
a+
1;a--; line breaks can be anywhere
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.

E.g. {
number = number + 1;
printf("%d\n", number);
}

• An empty statement is a single


semicolon. 4
Compound Statements
• Sequences of statements can be combined into one with {...}
• Much like Java:
{
printf ("Hello, ");
printf ("world! \n");
}
• The C compiler treats the collection of these statements like
they are a single statement.
If statement

• False
Condition

True

Statement/
Expression
Syntax

if(boolean_expression)
{
/* statement(s) will execute if the boolean expression
is true */
}
Example
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;

/* check the boolean condition using if statement */


if( a < 20 ) {
/* if condition is true then print the following */ printf("a is less
than 20\n" );
}

printf("value of a is : %d\n", a);


return 0;
}
Notes on if
• Which of the following code fragments are equivalent?

A if (number % 2 != 0)
{
printf("%d", number);
}
printf(” is odd\n");

B if (number % 2 != 0)
printf("%d", number);
printf(” is odd\n");

C if (number % 2 != 0)
{
printf("%d", number);
printf(” is odd\n");
}
9
Notes on if
• Which of the following code fragments are equivalent?

A if (number % 2 != 0)
{
printf("%d", number);
}
printf(” is odd\n");

B if (number % 2 != 0)
printf("%d", number);
printf(” is odd\n");

C if (number % 2 != 0)
{
printf("%d", number);
printf(” is odd\n");
}
10
Notes on if
• Which of the following code fragments are equivalent?

A if (number % 2 != 0)
{
printf("%d", number);
} A Compound
printf(” is odd\n"); Statement

B if (number % 2 != 0)
printf("%d", number);
printf(” is odd\n");
A Statement

C if (number % 2 != 0)
{
printf("%d", number);
printf(” is odd\n");
}
11
Notes on if
• Common mistake

if (number % 2 != 0);
{
printf("%d is an odd ", number);
}
printf("number\n");

12
Notes on if
• Common mistake

if (number % 2 != 0);
{
printf("%d is an odd ", number);
}
printf("number\n");

No semi-
colon here!

The semicolon is an empty statement.

13
Notes on if
• Common mistake

if (number = 0)
{
printf("%d\n", number);
}
printf("%d\n", number);

14
Notes on if
• Common mistake

if (number = 0)
{
printf("%d\n", number);
}
printf("%d\n", number);

Should be ==

15
If else statement

• False
Condition

True

Statement/ Statement/
Expression Expression
Syntax
if(boolean_expression)
{ /* statement(s) will execute if the
boolean expression is true */
}
else
{ /* statement(s) will execute if the
boolean expression is false */
}
Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 ) {
/* if condition is true then print the following */ printf("a is less than 20\
n" );
}
else
{
/* if condition is false then print the following */
printf("a is not less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;

}
Example: oddeven.c #include <stdio.h>

/* Determine whether an input number


Read in a number, and determine is odd or even. */
if it’s odd or even.
main()
{
int number;

output “Enter an integer” printf("Enter an integer: ");


input number scanf("%d", &number);

if (number is odd) if (number % 2 != 0)


then {
{ printf("%d is an odd number\n",
output: number “ is an odd
number);
number”
} }
else
{
output: number “ is an even
number”
}
} 19
Example: oddeven.c #include <stdio.h>

/* Determine whether an input number


Read in a number, and determine is odd or even. */
if it’s odd or even.
main()
{
int number;

output “Enter an integer” printf("Enter an integer: ");


input number scanf("%d", &number);

if (number is odd) if (number % 2 != 0)


then {
{ printf("%d is an odd number\n",
output: number “ is an odd
number);
number”
} }
else else
{ {
output: number “ is an even printf("%d is an even number\n",
number” number);
} }
} 20
Example: oddeven.c #include <stdio.h>

/* Determine whether an input number


Read in a number, and determine is odd or even. */
if it’s odd or even.
main()
{
int number;

output “Enter an integer” printf("Enter an integer: ");


input number
No semicolons scanf("%d", &number);
here!
if (number is odd) if (number % 2 != 0)
then {
{ printf("%d is an odd number\n",
output: number “ is an odd
number);
number”
} }
else else
{ {
output: number “ is an even printf("%d is an even number\n",
number” number);
} }
} 21
Example: oddeven.c #include <stdio.h>

/* Determine whether an input number


Read in a number, and determine is odd or even. */
if it’s odd or even.
main()
{
int number;

output “Enter an integer” printf("Enter an integer: ");


input number scanf("%d", &number);

if (number is odd) if (number % 2 != 0)


then {
{ printf("%d is an odd number\n",
output: number “ is an odd
number);
number”
} }
else else
{ {
output: number “ is an even printf("%d is an even number\n",
number” number);
} }
} 22
The if...else if...else Statement
When using if , else if , else statements there are few points to
keep in mind:

 An if can have zero or one else's and it must come after any
else if's.
 An if can have zero to many else if's and they must come
before the else.
 Once an else if succeeds, none of the remaining else if's or
else's will be tested.
The if...else if...else Statement
T Statement/
Condition
Expression
F
T Statement/
Condition
Expression

F T
Condition Statement/
Expression

Statement/
Expression
Syntax
if(boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
}
else if( boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
else if( boolean_expression 3)
{
/* Executes when the boolean expression 3 is true */
}
else
{
/* executes when the none of the above condition is true */
Example
#include <stdio.h>
int main () {
/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a == 10 ) {
/* if condition is true then print the following */
printf("Value of a is 10\n" ); }
else if( a == 20 ) {
/* if else if condition is true */
printf("Value of a is 20\n" ); }
else if( a == 30 ) {
/* if else if condition is true */
printf("Value of a is 30\n" ); }
else {
/* if none of the conditions is true */
printf("None of the values is matching\n" ); }
printf("Exact value of a is: %d\n", a );
return 0;
Example: months.c

output “Enter an integer”


Determine the number of days in a
input month
given month:
if (month is September,
30 days hath September, or April,
or June,
April, June and November.
or November)
All the rest hath 31, then
Excepting February alone, {
Which hath 28 days clear, output “30 days”
}
And 29 in each leap year. else if (month is February)
{
output “28 or 29 days”
}
else
{
output “31 days”
}

27
Example: months.c int main()
{

#include <stdio.h>

/*************************\

Determine the number of days


in a given month:

30 days hath September,


April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/

const int September = 9;


const int April = 4;
const int June = 6;
const int November = 11;
const int February = 2; return 0;
} 28
Example: months.c int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>

/*************************\

Determine the number of days


in a given month:

30 days hath September,


April, June and November;
All the rest have 31,
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/

const int September = 9;


const int April = 4;
const int June = 6;
const int November = 11;
const int February = 2; return 0;
} 29
Example: months.c int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
/*************************\ month==April ||
month==June ||
Determine the number of days
in a given month: month==November )
{
30 days hath September, printf("30 days\n");
April, June and November;
All the rest have 31, }
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/

const int September = 9;


const int April = 4;
const int June = 6;
const int November = 11;
const int February = 2; return 0;
} 30
Example: months.c int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
/*************************\ month==April ||
month==June ||
Determine the number of days
in a given month: month==November )
{
30 days hath September, printf("30 days\n");
April, June and November;
All the rest have 31, }
Excepting February alone,
And that has 28 days clear
And 29 in each leap year.
\*************************/

const int September = 9;


const int April = 4; Common mistake:
const int June = 6;
const int November = 11;
if
const
(month==September
int February = 2;
|| April || June || November )
return 0;
} 31
Example: months.c int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
/*************************\ month==April ||
month==June ||
Determine the number of days
in a given month: month==November )
{
30 days hath September, printf("30 days\n");
April, June and November;
All the rest have 31, }
Excepting February alone, else if (month==February)
And that has 28 days clear {
And 29 in each leap year.
\*************************/
printf("28 or 29 days\n");
}
const int September = 9;
const int April = 4;
const int June = 6;
const int November = 11;
const int February = 2; return 0;
} 32
Example: months.c int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
/*************************\ month==April ||
month==June ||
Determine the number of days
in a given month: month==November )
{
30 days hath September, printf("30 days\n");
April, June and November;
All the rest have 31, }
Excepting February alone, else if (month==February)
And that has 28 days clear {
And 29 in each leap year.
\*************************/
printf("28 or 29 days\n");
}
const int September = 9; else
const int April = 4; {
const int June = 6; printf("31 days\n");
const int November = 11; }
const int February = 2; return 0;
} 33
Example: months.c int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
/*************************\ month==April ||
month==June ||
Determine the number of days
in a given month: month==November )
{
30 days hath September, printf("30 days\n");
April, June and November;
}
“Default” block.
All the rest have 31,
Excepting February alone, else if (month==February)
And that has 28 days clear {
And 29 in each leap year.
\*************************/
printf("28 or 29 days\n");
}
const int September = 9; else
const int April = 4; {
const int June = 6; printf("31 days\n");
const int November = 11; }
const int February = 2; return 0;
} 34
Example: months.c int main()
{
int month;
printf("Enter number of month: ");
scanf("%d", &month);
#include <stdio.h>
if (month==September ||
/*************************\ month==April ||
month==June ||
Determine the number of days
in a given month: month==November )
{
30 days hath September, printf("30 days\n");
April, June and November;
All the rest have 31, }
Excepting February alone, else if (month==February)
And that has 28 days clear {
And 29 in each leap year.
\*************************/
printf("28 or 29 days\n");
}
const int September = 9; else
const int April = 4; {
const int June = 6; printf("31 days\n");
const int November = 11; }
const int February = 2; return 0;
} 35
Notes on Cascaded if
if (letter >= ’a’)
{

Q:
printf(“S1\n”);
}
What is the output if: else if (letter <= ’z’)
{
• letter is equal to ‘b’ printf(“S2\n”);
}
• letter is equal to ‘z’ else if (letter >= ’A’)
{
• letter is equal to ‘A’ }
printf(“S3\n”);

else if (letter <= ’Z’)


• letter is equal to ‘X’ {
printf(“S4\n”);
}

36
More Examples
if (ch >= ’a’ && ch <= ’z’)
{
printf(“%c is in lower case.\n”, ch);
}
else if (ch >= ’A’ && ch <= ’Z’)
{
printf(“%c is in upper case.\n”. ch);
}
else if (ch >= ’0’ && ch <= ’9’)
{
printf(“%c is a digit with value %d.\n”, ch, ch - ’0’);
}

37
More Examples
if (ch >= ’a’ && ch <= ’z’)
{
printf(“%c is in lower case.\n”, ch);
}
else if (ch >= ’A’ && ch <= ’Z’)
{
printf(“%c is in upper case.\n”. ch);
}
else if (ch >= ’0’ && ch <= ’9’)
{
printf(“%c is a digit with value %d.\n”, ch, ch - ’0’);
}

38
Nested if statements
You can use one if or else if statement inside another if or else if
statement(s)
Syntax:
if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
}
Nested if statements
T T T
Condition Condition Condition

F
Statement/
Expression
Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
/* check the boolean condition */
if( a == 100 ) {
/* if condition is true then check the following */
if( b == 200 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}

}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
Topics

• The if statement
• The else statement
• Cascaded if
• Nested if

42
switch statement
A switch statement allows a variable to be tested for equality against a
list of values.
Syntax:
switch(expression){
case constant-expression :
statement(s); break; /* optional */
case constant-expression :
statement(s); break; /* optional */
……

/* you can have any number of case statements */


default : /* Optional */
statement(s);
}
switch statement
Condition

Case 1
Statement/
Expression
Case 2
Statement/
Expression

Default
Statement/
Expression
Example
#include <stdio.h>
int main () {
/* local variable definition */
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}

printf("Your grade is %c\n", grade );


The ? : Operator

Exp1 ? Exp2 : Exp3;

• It can be used to replace if...else statements.


Good Luck
Thank you 

Q?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy