0% found this document useful (0 votes)
16 views24 pages

Lecture 3

The document outlines the content of Lecture 3 for CS 101, focusing on conditional statements in programming, including if, if-else, and switch constructs. It covers Boolean expressions, logical operators, and introduces the ternary operator, along with homework assignments related to series calculations and trigonometric functions. Additionally, it provides examples of using conditional statements in C++ code to draw shapes based on user input.

Uploaded by

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

Lecture 3

The document outlines the content of Lecture 3 for CS 101, focusing on conditional statements in programming, including if, if-else, and switch constructs. It covers Boolean expressions, logical operators, and introduces the ternary operator, along with homework assignments related to series calculations and trigonometric functions. Additionally, it provides examples of using conditional statements in C++ code to draw shapes based on user input.

Uploaded by

gurvardaan01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Programming (CS 101)

Spring 2024

Lecture 3:
- Conditional (if, if-else, switch) blocks, Boolean expressions Instructor: Preethi Jyothi
- ternary operator
Recap

What is the output from the following


piece of code?
#include <simplecpp>

main_program{
A 0
int i = 1, j = 2, k = 5;
B 2 k += i - 2 * j - 4 / 2;
cout << k << endl;
C 4
}

D 1
Homework problems

1 1 1 1
[Q1]. Write C++ code to calculate the following series: 1 − + − + − …
2 3 4 5
HW
alt-harmonic.cpp

[Q2]. Write C++ code to calculate sin(x) using the Taylor series expansion (where x is in
radians): 3 5 7
x x x
sin(x) = x − + − +…
3! 5! 7!
Ask for x (in degrees) from the user and use a xed number of terms. You can use PI
(offered by simplecpp) to access the value of π.

HW
sine.cpp
fi
Another useful arithmetic operator: % (modulus)

• %: the modulo or modulus (remainder from division) operator

operand1 % operand2:

Works on integers and nds the remainder of operand1 / operand2

• Has the same level of precedence as multiplication and division

• Left-to-right associativity, i.e. a % b % c will be processed as ((a % b) % c)


fi
if statement
CS 101, 2025
if statement
• Syntax:
if(condition) {
body
}

• Semantics: body should be executed if condition is true

• condition is an expression that takes the value "false" (zero) or "true" (non-zero)

• condition can include relational operators.


Examples: i <= j, i > j, i >= j, i < j, i == j, i != j
Logical operators
if(condition) {
body
}

• condition can also include logical operators, that return the result of a Boolean operation

1. Logical NOT denoted by '!'

!expression evaluates to true if expression evaluates to false, and vice-versa


2. Logical AND denoted by '&&'

operand1 && operand2 evaluates to true if both operand1 and operand2 are true
Otherwise, the result is false.
3. Logical OR denoted by '||'

operand1 || operand2 evaluates to true if either operand1 or operand2 or both are true
Boolean variables

• Like integers takes values such as ..., -2, -1, 0, 1, 2, ..., Boolean variables take values
true, false

• Arithmetic operators such as +, *, etc. are used with integers, oats; Boolean variables
allow operations like && (AND), || (OR), ! (NOT)

• Data type bool in C++ is used to represent Boolean variables. Example:


bool done = false; // equivalent to bool done = 0;

fl
Boolean expressions
• Consider two Boolean variables x and y
• !x → NOT x is true if and only if x is false
• x && y → x AND y is true if and only if both x and y are true
• x || y → x OR y is true if and only if at least one of x or y is true

• A variable of type bool can be used to save the value of a Boolean expression

bool choice;
choice = (input == 'S' || input == 'D');

if(!choice)
cout << "Invalid input. Try again.";
Evaluating Boolean expressions

• To evaluate condition1 || condition2 rst condition1 is evaluated


• if condition1 is true, then the entire expression is taken to be true without evaluating
• otherwise, condition2 is evaluated and its value becomes the value of the expression

• Similarly, while evaluating condition1 && condition2 if condition1 is false, the entire
expression is taken to be false, without evaluating condition2

• Operator precedence: ! is evaluated rst, followed by && and then || [1]

bool out = !a && b || c; Here, out is ((!a) && b) || c

[1] https://en.cppreference.com/w/cpp/language/operator_precedence
fi
fi
Code to draw a square or diamond depending on user choice

Demo in class and code shared on Moodle


Code with if statement

cout << "Do you want a square or diamond? Use S or D";


char input;
cin >> input;

if(input == 'D') {
right(45); if the body of if is a single statement,
} { and } can be omitted

repeat(4) {
forward(100); right(90);
}
Code with if and logical operators
cout << "Do you want a square or diamond? Use S or D";
char input;
cin >> input;

if(input == 'D') Can omit {} since repeat is a single


right(45); statement

if(input == 'D' || input == 'S') {


repeat(4) {
forward(100); right(90); Logical operators || and &&
}
}
Recall \n is a newline character
if(input != 'D' && input != 'S')
cout << "Input should be either D or S. Try again.\n"
Code with if and logical operators
cout << "Do you want a square or diamond? Use S or D";
char input;
cin >> input;

if(input == 'D')
right(45);

if(input == 'D' || input == 'S')


repeat(4) {
forward(100); right(90);
}

if(input != 'D' && input != 'S')


cout << "Input should be either D or S. Try again.\n"
if-else block
cout << "Do you want a square or diamond? Use S or D";
char input;
cin >> input;

if(input == 'D')
right(45);

if(input == 'D' || input == 'S')


repeat(4) {
forward(100); right(90);
}
else {
cout << "Input should be either D or S. Try again.\n";
}
Other forms of if statement
CS 101, 2025
if-else statement
• Syntax:
if(condition){
body1
} else {
body2
}

• Semantics: run body1 if condition is true and run body2 if condition is false

• Equivalent to:
if(condition){ body1 }

if(!condition){ body2 }
Chaining if-else statements if(condition1) {
body1
• Syntax: } else {
if(condition2) {
if(condition1) {
body2
body1
}
} else if(condition2) {
else {
body2
if(condition3) {
} else if(condition3) {
body3
body3
} else {
} else {
body4
body4
}
}
}
}

• Semantics: run body1 if condition1 is true, run body2 if condition2 is true,


run body3 if condition3 is true. If none of these conditions match, then run body4.
switch operator
CS 101, 2025
switch statement
• Transfers control to one of several code blocks, depending on the value of condition
switch(condition) {
• Semantics: condition is evaluated and
case constant1:
compared with the values of each case
body1 value (constant1, constant2). Run the
break; corresponding code (body1, body2) if
there is a match.
case constant2:
body2 • break inside each case block terminates
break; the switch statement. If no break, all the
statements after the matching case will be
default: executed until a break is encountered.
// execute if none of
// the constants are matched • If there is no match, then run body3 after
body3 default.
}
Some rules of switch
• Case values (constant1, constant2) must be either int or char type

• No duplicate case values are allowed

• default is an optional block

• break statements inside each case block are optional

• A case block can be empty

• Switch conditions can be expressions that evaluate to an int or char value. Example:
switch(1+2-3) is allowed.

• Position of the default case does not matter; can appear in the beginning, middle or end.
Typical to include it in the end.
ternary operator
CS 101, 2025
Ternary conditional operator
• What does this program do?

#include <simplecpp> equivalent code


main_program { using a #include <simplecpp>
int a, b, c; ternary operator main_program {
cin >> a >> b;
int a, b, c;
if(a > b)
cin >> a >> b;
c = a;
c = ((a > b) ? a : b);
else
}
c = b;
}

• Semantics of condition1 ? statement1 : statement2: if condition1


evaluates to true, then run statement1; otherwise, run statement2
Next class: Looping Statements
CS 101, 2025

You might also like