0% found this document useful (0 votes)
10 views23 pages

IP Lecture 3 ConditionalStatements (Autosaved)

Uploaded by

Al Nahian Abid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

IP Lecture 3 ConditionalStatements (Autosaved)

Uploaded by

Al Nahian Abid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Making Decisions using if-else

statements
Course Code: CSC1102 &1103 Course Title: Introduction to Programming

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 3 Week No: 2 Semester: Spring 23-24


Lecturer: Md. Faruk Abdullah Al Sohan; [email protected]
Lecture 3: Outline
 Making Decisions
 The if Statement
 The if-else Construct
 Logical Operators
 Boolean Variables
 Nested if Statements
 The else if Construct
 The switch Statement
 The Conditional Operator

 Character Input/Output
The if statement

if ( expression )
program statement

no If expression is true
expression (non-zero), executes
statement.
yes If gives you the choice
of executing statement
Program statement or skipping it.
Example - if

// Program to calculate the absolute value of an integer


int main ()
{
int number;
cout<<"Type in your number: “<<endl;
cin>>number;
if ( number < 0 ){
number = -number;
}
cout<<"The absolute value is “<<number<<endl;
return 0;
}
The if-else statement
if ( expression )
program statement 1
else
program statement 2 if-else statement:
enables you to
choose between
yes no two statements
expression

Program statement 1 Program statement 2


Example: if-else
// Program to determine if a number is even or odd
int main ()
{
int number_to_test, remainder;
cout<<“Enter your number to be tested: ”<<endl;
cin>>number_to_test;

remainder = number_to_test % 2;

if ( remainder == 0 ){
cout<<"The number is even“<<endl;}
else{
cout<<"The number is odd“<<endl;}
return 0;
}
Logical operators

Operator Symbol Meaning

AND && X && y is true if BOTH x and y are true


OR || X || y is true if at least one of x and y is true
NOT ! !x is true if x is false

Logical values as operands or in tests: true = non-zero, false=zero

Logical values returned as results of expressions: true = 1, false=zero

Example: 5 || 0 is 1
Precedence of operators

Precedence
!, ++, --, (type)
*, /, % Example for operator precedence:
+, - a > b && b > c || b > d
Is equivalent to:
<, <=, >, >=, ==, != ((a > b) && (b > c)) || (b > d)
&&
||
=
Example: compound relational
test
/* Program to determine if a number is even or odd
and also the number cannot be negative */
int main (){
int number_to_test, remainder;
cout<<“Enter your number to be tested: ”<<endl;
cin>>number_to_test;

remainder = number_to_test % 2;
if (number_to_test >= 0){
if (remainder == 0 ){
cout<<"The number is even“<<endl;}
else{
cout<<"The number is odd“<<endl;}
}
else{
cout<< “Not valid”;}
return 0;
}
Example:3
Example:4
Flowchart to determine if a year is a leap
year
Start

Input year

rem_4 = year % 4;
rem_100 = year % 100;
rem_400 = year % 400;

true (rem_4 == 0 && rem_100 != 0) false


|| rem_400 == 0

Leap Year Not Leap Year

End
Example: compound relational
test
// Program to determine if a year is a leap year or not
int main ()
{
int year, rem_4, rem_100, rem_400;
cout<<"Enter the year to be tested: “<<endl;
cin>>year;
rem_4 = year % 4;
rem_100 = year % 100;
rem_400 = year % 400;

if ( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 ){


cout<<“It's a leap year.”<<endl;}
else{
cout<<“It's not a leap year.“<<endl;}

return 0;
}
Nested if
statements
Multiple choices – else-if
int number;
if ( expression 1)
program statement 1
if negative
else if ( expression 2)
program statement 2
else
program statement 3

else if zero

Program style: this


else positive unindented formatting
improves the readability of
the statement and makes
it
clearer that a three-way
decision is being made.
Example – multiple choices
/* Program to evaluate simple expressions of the form
number operator number */
int main () {
float value1, value2;
char operator;
cout<<“Type in your expression”<<endl;
cin>>value1>>operator>>value2;
if ( operator == '+’ ){
cout<<value1 + value2<<endl;}
else if ( operator == '-’ ){
cout<<value1 - value2<<endl;}
else if ( operator == '*’ ){
cout<<value1 * value2<<endl;}
else if ( operator == '/’ ){
cout<<value1 / value2<<endl;}
else {cout<<"Unknown operator.";}
return 0;
}
The switch statement
switch ( expression )
{ The expression is
case value1: successively compared against
program statement the values value1, value2, ...,
...
valuen. If a case is found
break;
case value2: whose value is equal to the
program statement value of expression, the
... program statements that follow
break; the case are executed.
case valueN:
program statement
...
break;
The switch test expression must be
default: one with an integer value (including
program statement type char) (No float !).
... The case values must be integer-
break;
type constants or integer constant
}
expressions (You can't use a
variable for a case label !)
The switch statement (cont)

Break can miss !

Statement list on
switch (operator) a case can miss !
{
...
case '*':
case 'x':
printf ("%.2f\n", value1 * value2);
break;
...
}
Example - switch
The conditional operator
condition ? expression1 : expression2

condition is an expression that is evaluated first.


If the result of the evaluation of condition is TRUE (nonzero), then expression1
is evaluated and the result of the evaluation becomes the result of the operation.
If condition is FALSE (zero), then expression2 is evaluated and its result
becomes the result of the operation

maxValue = ( a > b ) ? a : b;

Equivalent to:

if ( a > b )
maxValue = a;
else
maxValue = b;
Example 1
#include <iostream>
#include <string>
using namespace std;
int main() {
double marks; // take input from users
cout << "Enter your marks: ";
cin >> marks;
// ternary operator checks if
// marks is greater than 40
string result = (marks >= 40) ? "passed" :
"failed";
cout << "You " << result << " the exam.";
return 0;
}
Example 2
#include <iostream>
#include <string>
using namespace std;
int main() {
int number = 0;
string result;
// nested ternary operator to find whether
// number is positive, negative, or zero
result = (number == 0) ? "Zero" : ((number > 0) ? "Positive" :
"Negative");
cout << "Number is " << result;
return 0;
}

You might also like