Chapter 3
Chapter 3
Chapter 3
• Statements can be
repeated.
• The number of
repetitions depends
on when the
condition turns false
Selection structure - if Statement
• The if statement allows the programmer to make
decisions on whether a section of code executes or
not.
Syntax:
if ( conditional expression )
statement;
Example:
int m=40,n=40;
if (m == n)
printf("m and n are equal");
Structure of if Statement
• The if keyword is followed by the
parenthesis, which contain a conditional
expression using a relational operator.
• Following this, there is the body of the if
statement, consisting of either a single
statement or multiple statements enclosed
by braces.
• Statements making up the body of the if, will
not be executed at all, if the condition is false.
if Statement flow chart
If the condition
is true, the
statement is
executed.
If it is false,
nothing
happens
int main ()
{
int percentage = 70;
if( percentage > 60 )
Printf(“You are passed”);
statements in
curly brackets
Compound Statements . . .
• For example,
if (Condition)
{
statement_1; /*as many statements as
necessary*/
statement_2; /*can be placed within the braces*/
• /*each statement must end with ;
*/
•
•
statement_n;
}
Compound Statement . . .
Example
if ( percentage >= 60 )
{
Printf("Passed.\n”);
Printf(“Allowed to enter in next semester\n”);
}
else
{
Printf("Failed.\n”);
Printf("You must take this course again.\n”);
}
Without braces,
Printf(“Allowed to enter in next semester\n”);
Printf("You must take this course again.\n”);
always executes
Class Activity
• Write a program to read a number. The
program should determine whether the
number is even or odd.
Class Activity
Write a program that ask user to enter two
numbers. Program should determine which
is smaller one and display that number on
screen.
Output of program:
Enter first number: 10
Enter second number: 15
Smaller number: 10
The Switch Statement
• The control statement that allows us to
make a decision from the number of
choices is called a switch.
• The Switch statement provides an
alternative to the else – if statement
construct. It is similar to the else – if
statement, but has more flexibility and a
clearer format.
Structure of the Switch Statement
switch ( conditional variable)
{
case 1:
statement ;
break ;
case 2 :
statement ;
break ;
default :
statement ;
}
Structure of the Switch Statement
• The statement starts out with the keyword
switch, followed by the parenthesis
containing an integer or character variable
which we will call the switch variable.
switch ( op )
{
case ‘ + ‘ :
printf( “ = “<<, num1 + num2) ;
break ;
case ‘ - ‘ :
printf( “ = “, num1 - num2) ;
break ;
case ‘ * ‘ :
printf(= “, num1 * num2) ;
break ;
case ‘ / ‘ :
printf( “ = “, num1 / num2 ) ;
break ;
default :
printf(Unknown Operator “) ;
}
}
Class Activity
• Attendance Marks:
• 40 – 42 10 marks
• 37 – 39 9 marks
• 34 – 36 8 marks
• 30 – 33 7 marks
• < 30 0 marks
Class Activity
• Enter any character : A
• You pressed Capital letter
Body of
for loop
Class Activity
1- Write a program which generates the following
output:
2 4 6 8 10 12 14 16 18 20