# Conditionals

A conditional is a programming construct that implements decisions.

• If the weather is good then we will go for a walk, else we will stay inside and watch TV.
• If it is cold enough to snow I will wear my heavy coat, else if it is warmer and just rains I will wear my rainjacket. The expression following each if or else must be true or false, i.e. a logical expression (in Fortran terminology).

## If - Else If - Else

The else if and else are optional. The parentheses around the Boolean expression are required.

   if ( boolean1 ) {
code for boolean1 true
} else if ( boolean2 ) {
more code for boolean2 true but boolean1 false
} else {
yet more code for both boolean1 and boolean2 false
}


Here the curly braces denote a code block. The else if and else can be on separate lines, but the layout illustrated is fairly conventional.

Only one branch will be executed. Once any Boolean is determined to be true, the corresponding code will be executed and then the flow will proceed beyond the if block.

Exercise Experiment with various truth values for bool1 and bool2.

#include <iostream>

int main() {

bool bool1=true, bool2=true;

if (bool1) {
std::cout<<"The if \n";
} else if (bool2) {
std::cout<<"The else if \n";
} else {
std::cout<<"The else \n";
}

}



## The ? Operator

C and C++ support a very succinct operator for cases in which the purpose of the if block is to assign a variable.

expr1 ? expr2 : expr3


where expr1 must evaluate to a Boolean. The expressions expr2 and expr3 should evaluate to the same type.

The operation should be read as “IF expr1 THEN expr2 ELSE expr3”. It returns the value of the selected expression.

    float v = (y>=0.0) ? sqrt(y) : 0.0;


This is equivalent to

   float v;
if (y>=0.0) {
v=sqrt(y);
} else {
v=0.0;
}


## Switch

Many “else ifs” can become confusing. The switch construct can simplify the statements, under the right conditions.
“Expression” must be an integer type or one that can be treated as an integer, e.g. char (but not string). The options must be literals or declared const. The break statements are optional but cause the flow of control to jump out of the switch if one is executed. Otherwise it will continue to the next case.

switch (expression) {
case const value0:
code;
break;  //optional
case const value1:
code;
break;  //optional
case const value2:
code;
break;  //optional
case const value3:
code;
break;
default :   //optional
code;
}


default is for the action, if any, to be taken if the expression does not evaluate to any of the options available.

Example:

#include <iostream>

int main() {

float x1,x2,y;
int chooser;

x1=11.53;
x2=57.9;

if (x1<0.) {
chooser=-1;
} else if (x1==0.) {
chooser=0;
} else {
chooser=1;
}

switch (chooser) {
case  (-1):
y=-x2;
break;
case (0):
y=0.;
break;
default:
y=x2+3./x1;
}

std::cout<<"Y is "<<y<<"\n";
}



Exercise:

This is the National Football Conference standings in late 2020: The Packers only need a win over the Chicago Bears to secure the No. 1 seed in the NFC. A loss or tie by the Seattle Seahawks would also give Green Bay the top spot. If the Packers lose, though, the Seahawks or New Orleans Saints could claim the top spot. The Saints would secure the No. 1 seed with a Packers loss, a win and a Seattle win. Seattle can get it with a win, a Green Bay loss and a New Orleans loss or tie.

Write a program to determine whether the Packers will win the No. 1 seed. Given the following conditions (not what happened), who won?

Packers lose to Bears
Seattle wins
The Saints tie

Hint: code can often be simplified with the introduction of logical variables which are sometimes also called flags.
Hint: if a variable is already a logical it is not necessary to test it against .true. or .false.
Hint: a loss or tie is not a win.

Example Solution

#include <iostream>

int main() {
bool packers_win, seahawks_win, saints_win;
bool packers_lose, seahawks_lose, saints_lose;

//Fill in with predictions
packers_win=false; packers_lose=true;
seahawks_win=true; seahawks_lose=false;
saints_win=false; saints_lose=false;

if ( packers_win || ( !seahawks_win ) ) {
}

if ( seahawks_win ) {
}  if ( packers_lose && !saints_win ) {
}

if ( saints_win ) {
}  if ( packers_lose && seahawks_win ) {
}

}   else if ( seahawks_advance )  {
}   else if ( saints_advance )  {