Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Control

Statements:
Part I
© G.Y.Namani Control Statements Part I Slide 1
Selection
Statements
© G.Y.Namani Control Statements Part I Slide 2
Introduction
• Control of statements "control" which
sections of code in a program are
executed.
• Specifying the order in which
statements (actions) execute in a
program is called program control.

© G.Y.Namani Control Statements Part I Slide 3


Introduction cont…
• This topic investigates program control
using C’s control statements.
• For example, we might want a set of
statements to execute only if certain
conditions are met, otherwise, the
statements would not be executed.

© G.Y.Namani Control Statements Part I Slide 4


Introduction cont…
There are three general types of control
statements.
i. Sequential statement – is default
ordering execution. Statements in a
program are executed one after the
other in the order in which they are
written.

© G.Y.Namani Control Statements Part I Slide 5


Introduction cont…
ii. Selection (decision/conditional)
statement – controls which block of
code within several alternatives is
executed.
iii. Repetition (iterative) statement –
controls how many times a block of
code is executed.

© G.Y.Namani Control Statements Part I Slide 6


Introduction cont…

The ability to control the flow of your
program, letting it make decisions on
what code to execute, is valuable to the
programmer.

One of the important functions of the
selection statement is that it allows the
program to select an action based upon
the user's input.
© G.Y.Namani Control Statements Part I Slide 7
Types of selection statements
C has three types of selection
statements.
i. The if statement
ii. The if...else statement
iii. The switch (case) statement

© G.Y.Namani Control Statements Part I Slide 8


if statement

The if statement either performs
(selects) an action, if a condition is
true, or skips it, if the condition is false.

The if statement is a single-selection
statement because it selects or ignores
a single action.

© G.Y.Namani Control Statements Part I Slide 9


if statement cont…
• The syntax of if statement:
if(condition){
//statements to execute if TRUE
}
• The example on next slide
demonstrates the use of if statement
in a C program.

© G.Y.Namani Control Statements Part I Slide 10


if statement cont…
/*program uses if statement to
print odd number*/
#include<stdio.h>
int main(){
int number;
printf("Enter your number\n");
scanf("%d",&number);
//program continues on next slide
© G.Y.Namani Control Statements Part I Slide 11
if statement cont…
if(number%2==1)
{
printf("%d is an odd number\
n",number);
}
return 0;
}
© G.Y.Namani Control Statements Part I Slide 12
if…else statement
• The if...else statement performs an
action if a condition is true and per-
forms a different action if the condition
is false.
• The if...else statement is called a
double-selection statement because it
selects between two different actions
(or groups of actions).
© G.Y.Namani Control Statements Part I Slide 13
if…else statement cont…
• The syntax of if…else statement
if (condition)
{
//statements to execute if TRUE
}
else
{
//statements to execute if FALSE
}

© G.Y.Namani Control Statements Part I Slide 14


if…else statement cont…
/*the program that uses if...else
statement*/
#include<stdio.h>
int main(){
double number1, number2, quotient;
printf("Enter the first number\n");
scanf("%lf",&number1);
printf("Enter the second number\n");
scanf("%lf",&number2);

© G.Y.Namani Control Statements Part I Slide 15


if…else statement cont…
if(number2!=0){
quotient=number1/number2;
printf("The quotient is %.2lf\
n",quotient);
}else{
printf("Division by zero is infinity! \
n");
}
return 0;
}
© G.Y.Namani Control Statements Part I Slide 16
Nested if…else statements
• if…else statement can be extended to
check multiple conditions.
• Nested if…else statements test for
multiple cases by placing if…else
statements inside if…else statements.

You can use "nested if...else
statements" following an if statement
and its body.
© G.Y.Namani Control Statements Part I Slide 17
Nested if…else statements
cont…
• If the first statement is true, the "else
if" will be ignored,
• but if the if statement is false, it will
then check the condition for the else if
statement.
• If all conditions are false the else block
will be executed.

© G.Y.Namani Control Statements Part I Slide 18


Nested if…else statements
cont…
• The syntax of nested if...else statements is:
   if (condition1) {
        //statements to execute if condition1 is TRUE;
      }
      else if (condition2)
      {
       //statements to execute if condition2 is TRUE;
      }
      else
      {
   //Statements to execute if all conditions are FALSE;

      }
© G.Y.Namani Control Statements Part I Slide 19
Nested if…else statements
cont…
/*the program that uses nested
if...else statement*/
#include<stdio.h>
int main(){
int x=4, y=5, z=6, biggest;
if(x>y && x>z){
biggest=x;

© G.Y.Namani Control Statements Part I Slide 20


Nested if…else statements
cont…
}else if(y>x && y>z){ biggest=y;
}else{
biggest=z;
}
printf("The biggest number is %d\
n",biggest);
return 0;
}
© G.Y.Namani Control Statements Part I Slide 21
switch (case) statement

The switch statement performs one of
many different actions, depending on
the value of a variable.
• The switch statement is called a
multiple-selection statement because it
selects among many different actions
(or groups of actions).

© G.Y.Namani Control Statements Part I Slide 22


switch (case) statement
cont…
The syntax of switch case is as follows:
switch (variable){
case this_value:
//code to execute if variable== this-value
break;
case that_value:
//code to execute if variable== that-value
break;
...
default:
//execute default if all cases are false
break;
}

© G.Y.Namani Control Statements Part I Slide 23


switch (case) statement
cont…
• The condition of a switch statement is
a value.
• The case says that if variable has the
value of whatever is after that case
then do whatever follows the colon.
• The break statement is used to break
out (exit) of the case statements.

© G.Y.Namani Control Statements Part I Slide 24


switch (case) statement
cont…
• The default case is optional, but it is
wise to include it as it handles any
unexpected cases.
• It can be useful to put some kind of
output to alert you to the code entering
the default case if you do not expect it
to.

© G.Y.Namani Control Statements Part I Slide 25


switch (case) statement
cont…
// program using switch case statement
#include<stdio.h>
int main(){
int day;
printf("Enter the day number\n");
scanf("%d",&day);
switch (day) {
case 1:
printf("Monday\n");
break;
© G.Y.Namani Control Statements Part I Slide 26
switch (case) statement
cont…
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
© G.Y.Namani Control Statements Part I Slide 27
switch (case) statement
cont…
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
© G.Y.Namani Control Statements Part I Slide 28
switch (case) statement
cont…
default:
printf("Enter numbers between 1
to 7 inclusive\n");
break;
} // end switch case statement
return 0;
} // end main

© G.Y.Namani Control Statements Part I Slide 29


Thanks for listening!

© G.Y.Namani Control Statements Part I Slide 30

You might also like