Showing posts with label If-else. Show all posts
Showing posts with label If-else. Show all posts

Sunday, March 12, 2017

C++ Part 18

A SINGLE STATEMENT FOR EACH ALTERNATIVE


if (Boolean_Expression)
Yes_Statement
else
No_Statement
If the Boolean_Expression evaluates to true, then the Yes_Statement is executed.
If the Boolean_Expression evaluates to false, then the No_Statement is executed.
Multi if-else Statement

if (Boolean_Expression_1)
Statement_1
else if (Boolean_Expression_2)
Statement_2
. . .
else if (Boolean_Expression_n)
Statement_n
else
Statement_For_All_Other_Possibilities

// EXAMPLE

if ((temperature < -10) && (day == SUNDAY))
cout << "Stay home.";
else if (temperature < -10) // and day != SUNDAY
cout << "Stay home, but call work.";
else if (temperature <= 0) // and temperature >= -10
cout << "Dress warm.";
else // temperature > 0
cout << "Work hard and play hard.";  

C++ Part 13


Types of Control

  • Selection: Execute a different instruction depending on the result of evaluating some [Boolean] condition.
    • if/else, multiway if, switch, ? :
  • Iteration: Repetitively execute some instruction until some condition is met.
    •  for, while, do while.

Conditions

A condition is a Boolean expression - i.e., one that evaluates to true or false.
Most commonly either a relational operator or a logical combination of conditions:
Relational (Comparison) Operators:
== != < > <= >=
Logical operators:
! && ||
For example :  
  • n <= 100 && n >= 0  // n less than equal to AND n must be greater than 0
  • n< 101 && n >= 0  // n less than 101 AND n greater than equal to 0
  • grade> ’c’ && grade <= ’f’ // grade greater than 'c' ( character c ) AND less than equal to 'f'
  • y< x && y >= 0   // 

Saturday, March 11, 2017

Intro to C++ Part 6

A Sample C++ Program  


##include <iostream>
using namespace std;The following line says that main is a function with no parameters that returns an int(integer) value:int main( )Some compilers will allow you to omit the int or replace it with void, which indicates
a function that does not return a value. However, the previous form is the most
universally accepted way to start the 
main function of a C++ program.
The program ends when the following statement is executed:
return 0;  


Digital Design Part 3

4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...