The if-else statement

• Introduction

• The if-statement has a "cousin statement" (closely related statement) called the if-else-statement

• We will learn about this statement in the webpage.

• Motivational Example

• Consider the following problem:

• Given 3 variables" x, y and max

• The variables x and y already have some value assigned to them

• Determine the largest value between x and y and assign this largest value to the variable max

• A poor solution is:

 ``` if ( x > y ) max = x; if ( x <= y ) max = y; ```

• There are 2 problems with this approach:

• We need to write 2 separate conditions - it is more error prone.

• If for one reason or another, the first if-statement modifies the values in the variables x or y, then the condition in the second if-statement is changed and the program will become incorrect...

• This problem can better be solved using the if-else-statement

IMPORTANT

• The if-else   statement

• The syntax of the if-else-statement is:

 ``` if ( Condition ) ONE-single-statement1 // "then part" else ONE-single-statement2 // "else part" ```

• The meaning of the if-else-statement is:

 Evaluate the Condition-clause If the Condition-clause is TRUE, then execute statement1 and SKIP statement2 If the Condition-clause is FALSE, then execute SKIP statement1 and execute statement2

• The statement statement1 is called the "then" part (for obvious reasons).

• The statement statement2 is called the "else" part (for obvious reasons).

• NOTE: statement1 has a ";"

• NOTE: If you use a compound statement, then statement1; (including the ";") is replaced by the compound statement !!!

• Example of using the if-else   statement

• Let us write the solution of the following problem using the if-else   statement:

• Given 3 variables" x, y and max

• The variables x and y already have some value assigned to them

• Determine the largest value between x and y and assign this largest value to the variable max

• Solution:

 ``` public class FindMax { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x, y; int max; x = in.nextInt(); y = in.nextInt(); if ( x > y ) // if ( Condition) max = x ; // ONE-statement else // else max = y ; // ONE-statement } } ```

• Example Program: (Demo above code)

• NOTE: if we use compound statements for the "then" and "else" parts, we would write:

 ``` public class FindMax { public static void main(String[] args) { Scanner in = new Scanner(System.in); int x, y; int max; x = in.nextInt(); y = in.nextInt(); if ( x > y ) { // { \ max = x ; // max = x; +-> one (compound) statement } // } / else } // { \ max = y ; // max = y; +-> one (compound) statement } // } } } ```
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "http://www.mathcs.emory.edu/~cheung/Courses/170.2010/Syllabus/05/if-else.html", "fetch_time": 1550537432000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2019-09/segments/1550247489282.7/warc/CC-MAIN-20190219000551-20190219022551-00157.warc.gz", "warc_record_offset": 391230246, "warc_record_length": 2345, "token_count": 670, "char_count": 2694, "score": 3.65625, "int_score": 4, "crawl": "CC-MAIN-2019-09", "snapshot_type": "latest", "language": "en", "language_score": 0.7767715454101562, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00044-of-00064.parquet"}