2 Decision F
2 Decision F
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);
}
• False
Conditio
n
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;
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!
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
Conditio
n
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>
F T
Conditio Statement/
n
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
27
Example: months.c int main()
{
#include <stdio.h>
/*************************\
/*************************\
Q:
printf(“S1\n”);
}
What is the output else if (letter <= ’z’)
{
if: printf(“S2\n”);
}
• letter is equal to else if (letter >= ’A’)
‘b’ {
printf(“S3\n”);
• letter is equal to }
else if (letter <= ’Z’)
‘z’ {
printf(“S4\n”);
• letter is equal to }
‘A’ 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
Conditio
T Conditio
T Conditio
T n n n
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 */
……
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" );
}
Q?