Programming Fundamentals
Decisions
Lecture Outline
• Decisions
 The if statement
 The if-else statement
 The else-if construct
The switch statement
 The conditional operator
Decisions
• A decision causes a one time jump to a different
part of the program, depending on the value of
an expression.
• Decision types:
 if
 if-else
else-if
switch
conditional operator
if Statement
• if is the simplest of decision statements. It says that if the condition
is true the body is executed ONLY ONCE.
if Statement Operation
if Statement: Example
#include <stdio.h>
#include <conio.h>
int main()
{
int x;
printf(“Enter a number: ”);
scanf(“%d”, &x); if (x>100)
printf(“The number entered is greater than 100 n”); getch();
}
Exercise: What will be the output
Exercise: Write a program that prompts the
user to input integer and tells whether the
number is odd or even.
Exercise: Write a program that prompts the
user to input age in years and if the age is
greater than or equal to 18 then prints the
message “you are eligible for voting” and if the
age is less than 18 prints the message
“you are not eligible for voting.”
Nested if Statement: Example
#include <stdio.h>
#include <conio.h>
int main()
{
int x;
printf(“Enter a number: ”);
scanf(“%d”,
&x); if (x>100)
if (x<105)
printf(“The number entered is greater than 100 n”); getch();
}
if-else Statement
• The if statement lets
you do something if a
condition is true. If it
isn’t true, nothing
happens.
• In situations where
something happens
even if the condition is
false then if-else
construct is used.
if-else Statement Operation
if-else Statement: Example
#include <stdio.h>
#include <conio.h>
int main()
{
int x;
printf(“Enter a number: ” );
scanf(“%d”, &x);
if (x>100)
printf(“The number entered is greater than 100 n”); else
printf(“The number entered is not greater than 100 n”);
getch();
}
else-if Statement
• What if your task requires a number of alternatives or
options (choices): Example: “if marks > 95, you obtained
grade A”, “if marks == 80, you obtained grade B”, and
“if marks < 80, you earned grade C”.
• In such situations, else . . . if construct may suffice.
if (marks > 95)
printf(“you obtained grade A”);
else if (marks == 80)
printf(“you earned grade B”);
else if (marks < 80)
printf(“you earned grade C”);
else-if Statement
Logical operator “AND”
if (marks > 95 && marks <=100)
printf(“you obtained grade A”); else if
(marks > 80 && marks <= 95)
printf(“you earned grade B”); else if
(marks > 70 && marks <= 80)
printf(“you earned grade B”);
Logical Operators
• Logical operators are used to combine Boolean
variables or expressions that evaluate to true or false.
Example: (a>b || b>c)
Expression-1
Logical “OR”
Expression-2
Logical Opeators
Matching else With if
Matching else With if
Matching else With if
Exercise: What is the output
IF…ELSE With LOOPS
• Loops and if…else statements can be nested within each other.
int main()
{
int n;
printf(“Enter the value: ”); scanf(“%d”,
&n);
if (n == 0)
for (int j=0; j<=10; j+=2)
printf(“Even numbers: %d n”, j); else if
(n == 1)
for (int j=1; j<=10; j+=2)
printf(“Odd numbers: %d n”, j); else if
(n != 0 && n != 1)
printf(“Please re-try!”) ;
}
The Conditional Operator
if (alpha < 70)
result = beta;
else
result = gama;
Such statements are so common in C++ that developers
invented compressed way to write such statements. One
of the most popular way is to write them using
conditional operator.
result = (alpha < 70) ? beta : gama
The Conditional Operator
The Conditional Operator :
Example - 01
int main()
{
int result, alpha, beta, gama;
beta = 69; gama = 71;
printf("Enter value for alpha: “);
scanf(“%d”, &alpha); result =
(alpha<70) ? beta : gama;
printf(“%d”, result);
}
The Conditional Operator :
Example - 02
int main()
{
int j; for(j=0; j<24; j++)
{
char ch = (j%8) ? ‘ ‘ : ‘x’;
printf(“%c”, ch); }
}
The SWITCH Statement
Exercise
• Write a program using switch statement that prints
“Red” if “r” is entered, “Blue” if “b” is entered and
“Yellow” if “y” is entered.
• Write a program that tells whether the entered
number is even or odd.

Programming Fundamentals Decisions

  • 1.
  • 2.
    Lecture Outline • Decisions The if statement  The if-else statement  The else-if construct The switch statement  The conditional operator
  • 3.
    Decisions • A decisioncauses a one time jump to a different part of the program, depending on the value of an expression. • Decision types:  if  if-else else-if switch conditional operator
  • 4.
    if Statement • ifis the simplest of decision statements. It says that if the condition is true the body is executed ONLY ONCE.
  • 6.
  • 7.
    if Statement: Example #include<stdio.h> #include <conio.h> int main() { int x; printf(“Enter a number: ”); scanf(“%d”, &x); if (x>100) printf(“The number entered is greater than 100 n”); getch(); }
  • 8.
    Exercise: What willbe the output
  • 9.
    Exercise: Write aprogram that prompts the user to input integer and tells whether the number is odd or even.
  • 10.
    Exercise: Write aprogram that prompts the user to input age in years and if the age is greater than or equal to 18 then prints the message “you are eligible for voting” and if the age is less than 18 prints the message “you are not eligible for voting.” Nested if Statement: Example #include <stdio.h> #include <conio.h> int main() {
  • 11.
    int x; printf(“Enter anumber: ”); scanf(“%d”, &x); if (x>100) if (x<105) printf(“The number entered is greater than 100 n”); getch(); } if-else Statement
  • 12.
    • The ifstatement lets you do something if a condition is true. If it isn’t true, nothing happens. • In situations where something happens even if the condition is false then if-else construct is used.
  • 13.
    if-else Statement Operation if-elseStatement: Example #include <stdio.h>
  • 14.
    #include <conio.h> int main() { intx; printf(“Enter a number: ” ); scanf(“%d”, &x); if (x>100) printf(“The number entered is greater than 100 n”); else printf(“The number entered is not greater than 100 n”); getch(); }
  • 15.
    else-if Statement • Whatif your task requires a number of alternatives or options (choices): Example: “if marks > 95, you obtained grade A”, “if marks == 80, you obtained grade B”, and “if marks < 80, you earned grade C”. • In such situations, else . . . if construct may suffice. if (marks > 95) printf(“you obtained grade A”); else if (marks == 80) printf(“you earned grade B”); else if (marks < 80) printf(“you earned grade C”);
  • 16.
    else-if Statement Logical operator“AND” if (marks > 95 && marks <=100) printf(“you obtained grade A”); else if (marks > 80 && marks <= 95) printf(“you earned grade B”); else if (marks > 70 && marks <= 80) printf(“you earned grade B”);
  • 17.
    Logical Operators • Logicaloperators are used to combine Boolean variables or expressions that evaluate to true or false. Example: (a>b || b>c) Expression-1 Logical “OR” Expression-2
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
    Exercise: What isthe output IF…ELSE With LOOPS • Loops and if…else statements can be nested within each other.
  • 24.
    int main() { int n; printf(“Enterthe value: ”); scanf(“%d”, &n); if (n == 0) for (int j=0; j<=10; j+=2) printf(“Even numbers: %d n”, j); else if (n == 1) for (int j=1; j<=10; j+=2) printf(“Odd numbers: %d n”, j); else if (n != 0 && n != 1) printf(“Please re-try!”) ; }
  • 25.
    The Conditional Operator if(alpha < 70) result = beta; else result = gama; Such statements are so common in C++ that developers invented compressed way to write such statements. One of the most popular way is to write them using conditional operator. result = (alpha < 70) ? beta : gama
  • 26.
    The Conditional Operator TheConditional Operator : Example - 01
  • 27.
    int main() { int result,alpha, beta, gama; beta = 69; gama = 71; printf("Enter value for alpha: “); scanf(“%d”, &alpha); result = (alpha<70) ? beta : gama; printf(“%d”, result); } The Conditional Operator : Example - 02
  • 28.
    int main() { int j;for(j=0; j<24; j++) { char ch = (j%8) ? ‘ ‘ : ‘x’; printf(“%c”, ch); } }
  • 30.
  • 32.
    Exercise • Write aprogram using switch statement that prints “Red” if “r” is entered, “Blue” if “b” is entered and “Yellow” if “y” is entered. • Write a program that tells whether the entered number is even or odd.