DECISION MAKING
STATEMENTS
Prof. Yashoda M B
Assistant Professor
Computer Science (UG)
Kristu Jayanti college
Bengaluru
if Statement
if- else Statement
if-else ladder
Nested if statement
switch statement
17/11/2023
2
Kristu Jayanti College
if statement: Syntax
17/11/2023
3
Kristu Jayanti College
Flowchart
17/11/2023
4
Kristu Jayanti College
Algorithm: To check the number is positive.
Step 1: Start
Step 2: Read a number and store in the variable Num.
Step 3: If the number stored in the variable Num is grater
than 0, print “ The number is Positive”.
Step 4: Stop
17/11/2023
5
Kristu Jayanti College
Example: C program to check the given number is positive.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num;
clrscr();
printf(“enter a numbern”);
scanf(“%d”,&Num);
// if statement
if(Num>0)
printf(“The number is positive”);
getch();
}
17/11/2023
6
Kristu Jayanti College
if-else statement: Syntax
17/11/2023
7
Kristu Jayanti College
Flowchart
17/11/2023
8
Kristu Jayanti College
Algorithm: To check the number is positive
or negative.
Step 1: Start
Step 2: Read a number and store in the variable Num.
Step 3: If the number stored in the variable Num is grater
than 0 go to Step 4, otherwise, go to step 5.
Step 4: print “ The number is Positive”. Goto Step 6.
Step 5: Print “The number is Negative”.
Step 6: Stop
17/11/2023
9
Kristu Jayanti College
Example: C program to check the given number is positive or negative.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num;
clrscr();
printf(“enter a numbern”);
scanf(“%d”,&Num);
// if else ladder
if(Num>0)
printf(“The number is positive”);
else
printf(“The number is negative”);
getch();
} 17/11/2023
10
Kristu Jayanti College
if-else ladder statement: Syntax
// any if-else ladder starts with an if statement only
if(condition1)
{
// statements
}
else if(condition2)
{
// this else if will be executed when condition in if is false and
// the condition of this else if is true
}
.... // once if-else ladder can have multiple else if
else
{
// at the end we put else
} 17/11/2023
11
Kristu Jayanti College
Flowchart
17/11/2023
12
Kristu Jayanti College
Example: C program to check the given number is positive or negative.
#include<stdio.h>
#include<conio.h>
void main()
{
int Num;
clrscr();
printf(“enter a numbern”);
scanf(“%d”,&Num);
// if else ladder
if(Num>0)
printf(“The number is positive”);
else if(Num<0)
printf(“The number is negative”);
else
printf(“The number is zero”);
getch();
17/11/2023
13
Kristu Jayanti College
nested if statement: Syntax
17/11/2023
14
Kristu Jayanti College
Flowchart
17/11/2023
15
Kristu Jayanti College
Example: C program to check the given number is positive or negative.
i) If positive, check whether if is odd or even
ii) if negative, display its square.
#include<stdio.h>
#include<conio.h>
void main() {
int Num;
clrscr();
printf(“enter a numbern”);
scanf(“%d”,&Num); // reading a number
if(Num>0)
{
printf(“The number is positive”);
if(Num%2==0) //nested if
printf(“The number is even”);
else
printf(“The number is odd”);
}
else {
printf(“The number is positive”);
printf(“The square of the number is %d”, Num*Num);
}
getch();
}
17/11/2023
16
Kristu Jayanti College
switch statement: Syntax
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
. . .
case value_n: statement_n;
break;
default: default_statement;
} 17/11/2023
17
Kristu Jayanti College
Flowchart
17/11/2023
18
Kristu Jayanti College
Example: C program to display the day on user’s choice using switch statement.
#include<stdio.h>
#include<conio.h>
void main() {
int choice;
clrscr();
printf(“enter a choicen”);
scanf(“%d”,&choice); // reading a number
switch(choice) {
case 1: printf(“It is Monday”); break;
case 2: printf(“It is Tuesday”); break;
case 3: printf(“It is Wednesday”); break;
case 4: printf(“It is Thursday”); break;
case 5: printf(“It is Friday”); break;
case 6: printf(“It is Saturday”); break;
case 7: printf(“It is Sunday”); break;
default: printf(“Invalid choice”);
}
getch();
} 17/11/2023
19
Kristu Jayanti College
17/11/2023
20
Kristu Jayanti College

DECISION MAKING STATEMENTS.pptx

  • 1.
    DECISION MAKING STATEMENTS Prof. YashodaM B Assistant Professor Computer Science (UG) Kristu Jayanti college Bengaluru
  • 2.
    if Statement if- elseStatement if-else ladder Nested if statement switch statement 17/11/2023 2 Kristu Jayanti College
  • 3.
  • 4.
  • 5.
    Algorithm: To checkthe number is positive. Step 1: Start Step 2: Read a number and store in the variable Num. Step 3: If the number stored in the variable Num is grater than 0, print “ The number is Positive”. Step 4: Stop 17/11/2023 5 Kristu Jayanti College
  • 6.
    Example: C programto check the given number is positive. #include<stdio.h> #include<conio.h> void main() { int Num; clrscr(); printf(“enter a numbern”); scanf(“%d”,&Num); // if statement if(Num>0) printf(“The number is positive”); getch(); } 17/11/2023 6 Kristu Jayanti College
  • 7.
  • 8.
  • 9.
    Algorithm: To checkthe number is positive or negative. Step 1: Start Step 2: Read a number and store in the variable Num. Step 3: If the number stored in the variable Num is grater than 0 go to Step 4, otherwise, go to step 5. Step 4: print “ The number is Positive”. Goto Step 6. Step 5: Print “The number is Negative”. Step 6: Stop 17/11/2023 9 Kristu Jayanti College
  • 10.
    Example: C programto check the given number is positive or negative. #include<stdio.h> #include<conio.h> void main() { int Num; clrscr(); printf(“enter a numbern”); scanf(“%d”,&Num); // if else ladder if(Num>0) printf(“The number is positive”); else printf(“The number is negative”); getch(); } 17/11/2023 10 Kristu Jayanti College
  • 11.
    if-else ladder statement:Syntax // any if-else ladder starts with an if statement only if(condition1) { // statements } else if(condition2) { // this else if will be executed when condition in if is false and // the condition of this else if is true } .... // once if-else ladder can have multiple else if else { // at the end we put else } 17/11/2023 11 Kristu Jayanti College
  • 12.
  • 13.
    Example: C programto check the given number is positive or negative. #include<stdio.h> #include<conio.h> void main() { int Num; clrscr(); printf(“enter a numbern”); scanf(“%d”,&Num); // if else ladder if(Num>0) printf(“The number is positive”); else if(Num<0) printf(“The number is negative”); else printf(“The number is zero”); getch(); 17/11/2023 13 Kristu Jayanti College
  • 14.
    nested if statement:Syntax 17/11/2023 14 Kristu Jayanti College
  • 15.
  • 16.
    Example: C programto check the given number is positive or negative. i) If positive, check whether if is odd or even ii) if negative, display its square. #include<stdio.h> #include<conio.h> void main() { int Num; clrscr(); printf(“enter a numbern”); scanf(“%d”,&Num); // reading a number if(Num>0) { printf(“The number is positive”); if(Num%2==0) //nested if printf(“The number is even”); else printf(“The number is odd”); } else { printf(“The number is positive”); printf(“The square of the number is %d”, Num*Num); } getch(); } 17/11/2023 16 Kristu Jayanti College
  • 17.
    switch statement: Syntax switch(expression) { casevalue1: statement_1; break; case value2: statement_2; break; . . . case value_n: statement_n; break; default: default_statement; } 17/11/2023 17 Kristu Jayanti College
  • 18.
  • 19.
    Example: C programto display the day on user’s choice using switch statement. #include<stdio.h> #include<conio.h> void main() { int choice; clrscr(); printf(“enter a choicen”); scanf(“%d”,&choice); // reading a number switch(choice) { case 1: printf(“It is Monday”); break; case 2: printf(“It is Tuesday”); break; case 3: printf(“It is Wednesday”); break; case 4: printf(“It is Thursday”); break; case 5: printf(“It is Friday”); break; case 6: printf(“It is Saturday”); break; case 7: printf(“It is Sunday”); break; default: printf(“Invalid choice”); } getch(); } 17/11/2023 19 Kristu Jayanti College
  • 20.