Flow Control and Booleans in C

Sequential Execution

Sequential Execution
#include <stdio.h>
#include <math.h>
void main(){
	double l;	//Inductance in milihenrys
	double c;	//Capacitance in microfarads
	double omega;	//Resonance frequency in radians per second
	double f;	//resonace frequency in Hertz
	printf("Enter the inductance in millihenrys: ");
	scanf("&lf", &l);
	printf("Enter the capacitance in microfarads: ");
	scanf("%lf", &c);
	omega = 1.0 / sqrt( (l / 1000) * (c / 100000));
	f = omega / (2 * M_PI);
	printf("Resonant frequency: %.2f\n", f);
}
			
Flow Control and Booleans in C

Selective Execution

Syntax if statement

if(condition)
	statement;
				
Selective Execution
Flow Control and Booleans in C

if statement

if(age > 39)
	printf("You are so old!\n");
				

The if statement

Fundamental means of flow control

How we will make decisions

age > 39
c == 0
l <= 0
age >= 18 && age < 65
				

Boolean expressions

The actual determiantion of ther decision

Flow Control and Booleans in C

Structure of an if stament

if(expression1)
	statement1;
else if(expression2)
	statement2;
else
	statement3;
				

If expression1 is true, execute statement1.

Otherwise, test to see if expression2 is true. If so, execute statement2.

Otherwise, execute statment3.

The expressions are boolean expressions that resolve to true or a false.
Flow Control and Booleans in C

Basic Boolean Expressions

true
false
age < 18
divisor == 0
size > 1000000
ch == 'x'
				
<Less than
<=Less than or equal to
==Equal
!=Not equal
>=Greater than or equal to
>Greater than
Important: The test for equality is ==, not =. This is the most common error in a C program.
Flow Control and Booleans in C

Example if statement

if(age < 18)
	printf("Too young to vote!\n");
if(area == 0)
	printf("The plot is empty\n");
else
	printf("The plot has an area of %.1f\n", erea);
if(val < 0)
	printf("Negative input is not allowed\n");
else if(val == 0)
	printf("A value of zero is not allowed");
else
	printf("The reciprocal is %.2f\n", 1.0 / val);
			
Note the indentation
Flow Control and Booleans in C

Blocks

printf("This is statement\n");
				
Single Statment
{
	printf("All items in a curly brace\n");
	printf("as if there are one statement");
	printf("They are executed sequentially");
}
				
Block
Flow Control and Booleans in C

Where is this useful?

if(val > 0){
	result = 1.0 / val;
	printf("Result = %f\n", result);
}
				
If the statement is true, all of the statement in the block are executed
Flow Control and Booleans in C

Where is this useful?

if(val > 0){
	result = 1.0 / val;
	printf("Result = %f\n", result);
}
if(val > 0)
	result = 1.0 / val;
	printf("Result %f\n", result);
				
Will these two sections of code work differently?
Flow Control and Booleans in C

Nested Blocks

if(bobsAge != suesAge){// != means "not equal"
	printf("Bob and Sue are different ages\n");
	if(bobsAge > suesAge){
		printf("In fact, Bob is older than sue\n");
		if( (bobsAge - 20) > suesAge){
			printf("Wow, Bob is more than 20 years older\n");
		}
	}
}
			
Flow Control and Booleans in C

Boolean Expression

An expression whose value is true and false

In C:

integer value of 0 is "false"

nonzero integer value is "true"

Flow Control and Booleans in C

Suppose?

What if I want to know if a value in a range

Test for: 100 ≤ L ≤ 1000?

You can't do...

if(100 <= L <= 1000){
	printf("Value is in range...\n");
}
				
This code is WRONG and will fail.
Flow Control and Booleans in C

Why this fail...

if( (100 <= L) <= 1000){
	printf("Value is in range...\n");
}
				
C treats this code this way.
Suppose L is 5000. Then 100 <= L is true, so (100 <= L) evaluates to true, which, in C, is a 1. Then it tests 1 <= 1000, which also returns true, even though you expected a false
Flow Control and Booleans in C

Compound Expressions

Solution (not in C): (100 <= L) and (L <= 1000)

In C: (100 <= L) && (L <= 1000)

Logical Operators

And&&
Or||
Not!
Flow Control and Booleans in C

Truth Table

pq!pp && qp || q
truetruefalsetruetrue
truefalsefalsefalsetrue
falsetruetruefalsetrue
falsefalsetruefalsefalse
Flow Control and Booleans in C

Switch Statement

A less general substitute for the multibranch if. It is used for selecting among discrete values (int), i.e not continuos values.

switch(int_expression){
	case_list:
		statement_list;
	case_list:
		statement_list;
	default:
		statement_list;
}
				
switch case statement
Flow Control and Booleans in C

Behavior

The int_expression is evaluated. If the value is in as case_list, execution begins at that statement_list and continues through subsequent statement_lists until: break, return, or end of switch.

Flow Control and Booleans in C

Example switch case

int gender;
printf("Enter your gender (male=1, female=2): ");
scanf("%d", &gender);
switch(gender){
	case 1:
		printf("You are a male\n");
		break;
	case 2:
		printf("You are a female\n");
		break;
	default:
		printf("Not a valid input\n");
		break;
}