I.T assignment
|
Code Sample |
Statement Type |
Explanation |
|
//get the value of score double score = 75;
if (score >= 60) { System.out.println(“Pass”); }
//the output of this example will be PASS //Nothing will happen if the given score was //less than 60
|
If-type Selection statements |
An if statement selects a statement for execution based on the value of a Boolean expression. An if statement may optionally include an else clause that executes if the Boolean expression is false. Comment by PlagScan: Possible source: http://www.c-source-code.blogspot.com/ The value of the score is first initialized to the value 75 and then if statements checks whether the value of score is greater than or equal to 60. If it returns true then it prints PASS. If it does not then nothing will be the output
|
|
if (condition)
{ //then-statements will be executed if the //condition is true Comment by PlagScan: Possible sources: http://innovatorswithc.blogspot.in/2013/02/c-decision-loops.html http://www.c-source-code.blogspot.com/ http://allintutor.blogspot.com/2012/12/if-in-c.html Comment by PlagScan: Possible sources: http://www.c-source-code.blogspot.com/ http://www.cs-tutorial.com/C-nested_if_else_statement-57 http://allintutor.blogspot.com/2012/12/if-in-c.html }
else { //else-statements will be executed if the //condition is false }
|
If- Else Selection Statement |
The if else statement is used if the programmer wants to execute some statement/s when the test expression is true and execute some other statement/s if the test expression is false. Comment by PlagScan: Possible sources: http://innovatorswithc.blogspot.in/2013/02/c-decision-loops.html http://www.c-source-code.blogspot.com/ http://ergopalkrishnawithc.blogspot.com/2012/07/loop-break-and-continue-in-c.html Comment by PlagScan: Possible sources: http://www.c-source-code.blogspot.com/ http://allintutor.blogspot.com/2012/12/if-in-c.html http://ergopalkrishnawithc.blogspot.com/2012/07/loop-break-and-continue-in-c.html http://innovatorswithc.blogspot.in/2013/02/c-decision-loops.html Comment by PlagScan: Possible sources: http://allintutor.blogspot.com/2012/12/if-in-c.html http://ergopalkrishnawithc.blogspot.com/2012/07/loop-break-and-continue-in-c.html In this type of if-statement, the first sub-statement will only be executedif and only ifthe expression is non-zero; otherwise, the second sub-statement will be executed. Comment by PlagScan: Possible source: http://en.wikibooks.org/wiki/C_Programming/Statements Comment by PlagScan: Possible source: http://en.wikibooks.org/wiki/C_Programming/Statements The if-else statement allows a choice to be made between two possible alternatives. Sometimes a choice must be made between more than two possibilities Comment by PlagScan: Possible sources: http://www.cs-tutorial.com/C-nested_if_else_statement-57 http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node99.html https://answers.yahoo.com/question/index?qid=20090812074527AAD6bWy http://www.chacha.com/question/what-is-a-nested-if-statement-in-c-programming Comment by PlagScan: Possible sources: http://www.cs-tutorial.com/C-nested_if_else_statement-57 http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node99.html https://answers.yahoo.com/question/index?qid=20090812074527AAD6bWy |
|
int option =1; int cost; int extra = 0; //can be 0 if no extras are requested, //1 if requested if (option == 1) { if (extra == 1) { //extras is requested cost = 8; System.out.println(“Customer selected the Standard wash+extra”); } else { //extras is NOT requested cost = 6; System.out.println(“Customer selected the Standard wash”); } }
else if (option == 2) { if (extra == 1) { //extras is requested cost = cost + 13; System.out.println(“Customer selected the Deluxe wash+extra”); } else { //extras is NOT requested cost = 10; System.out.println(“Customer selected the Deluxe wash”); } }
else if (option == 3) { if (extra == 1) { //extras is requested cost = 16; System.out.println(“Customer selected the Duper Deluxe Wash+extra”); } else { //extras is NOT requested cost = 12; System.out.println(“Customer selected the super deluxe wash”); } } //ends the main if
else { System.out.println(“invalid entry, please try again”); }
System.out.println(“Your total cost is” + cost);
//the output of this example will be: //Customer selected the standard wash //Your total cost is 6
|
Nested If-else selection statement |
Nested If statements are if statements that are inside other if statements. Nested if and if-else statements can be used to implement decisions which have more than two outcomes.
In this statement the conditions are evaluated from the top to bottom. If the outermost condition i.e., condition1 is true the statement(s) associated with this condition are executed as the inner if part is executed here. If condition2 is true the statement associated with this condition are executed otherwise the else (condition2’s false part) part’s statement(s) are executed. If the condition1 of outermost if statement is false then, else part of this if statements are executed Comment by PlagScan: Possible sources: http://www.c-source-code.blogspot.com/ http://dotprogramming.blogspot.com/2013/12/nested-if-or-if-else-statement-in.html Comment by PlagScan: Possible source: http://dotprogramming.blogspot.com/2013/12/nested-if-or-if-else-statement-in.html Comment by PlagScan: Possible source: http://dotprogramming.blogspot.com/2013/12/nested-if-or-if-else-statement-in.html
It will first evaluate the outer statements before executing the inner if statement. In this case it will first check if the options condition is true and if true it executesthe inner condition then if the initial first condition is true then “Customer selected the Standard wash+ extra” is printed as the output and also the value of cost is initialized to 8 but if the extra==1 condition is not satisfied it heads to the else condition that evaluates but this only happens if the value of option==1. Comment by PlagScan: Possible source: http://www.cs-tutorial.com/C-nested_if_else_statement-57 Comment by PlagScan: Possible source: http://www.cs-tutorial.com/C-nested_if_else_statement-57
If the user enters the value of option to be 2 then it gets inside and it evaluates for the first if statement encountered such that if the extra==1 is true then it prints “Customer selected the Standard wash+ extra” and the value of cost is initialized to 8 else if the it is initialized to 6 and “Customer selected the Standard wash” is printed and subsequently prints to total cost with regards to the cost initialized.
If the user enters the value of option to be 3 then it gets inside and it evaluates for the first if statement encountered such that if the extra==1 is true then it prints “ Customer selected the Duper Deluxe Wash+extra” and the value of cost is initialized to 16 else if the cost is initialized to 12 and “Customer selected the super deluxe wash” is printed and subsequently prints to total cost with regards to the cost initialized.
Finally if the user enters another different value of option from 1,2 or 3 then the system displays “invalid entry, please try again” and the output is not produced at all. |
|
switch (switch_expression) { case label1: //statements to be executed when label1 = //switch_expression break;
case label2: //statements to be executed when label2 = //switch_expression break;
case label3: //statements to be executed when label3 = //switch_expression break;
default: //statements to be executed when none of //the above labels matches the //switch_expression break; }
|
Switch case branching statements |
The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body. Control passes to the statement whose case constant-expression matches the value of switch (expression). Comment by PlagScan: Possible sources: http://ilmumarta.blogspot.com/2013/02/menggunakan-switch-case.html http://www.c-source-code.blogspot.com/ http://programmingzeal.blogspot.com/2013/01/conditional-statement-switch.html http://gambets.blogspot.com/ http://msdn.microsoft.com/en-us/library/66k51h7a.aspx http://msdn.microsoft.com/en-us/library/vstudio/66k51h7a(v=vs.100).aspx http://sct.emu.edu.tr/courses/it/itec114/userfiles/files/spring2013_2014/lecture/113Class Review.pdf http://tidx.ru/html/fbede014-23bd-4ab1-8094-c8d9d9cb963a.htm http://www.amibroker.com/guide/keyword/switch.html http://www.scribd.com/doc/44115372/C http://www.senore.com/C-programming-switch-statements-q1244145 https://answers.yahoo.com/question/index?qid=20080806003834AAFSvMn http://onlinecprogram.blogspot.com/ http://onlinecprogram.blogspot.com/2013/02/the-switch-statement-in-c-language.html#! http://www.cs-tutorial.com/C-switch_case_statement-59 http://www.learnconline.com/2010/03/switch-statement-in-c-programming-language.html http://www.sonarmods.com/sonarmods/forum/index.php?topic=635.0 http://stackoverflow.com/questions/20020927/c-switch-statement-with-non-compound-statement-use-case
The switch statement can include any number of case instances, but no two case constants within the same switch statement can have the same value. Comment by PlagScan: Possible sources: http://ilmumarta.blogspot.com/2013/02/menggunakan-switch-case.html http://www.c-source-code.blogspot.com/ http://programmingzeal.blogspot.com/2013/01/conditional-statement-switch.html http://gambets.blogspot.com/ http://msdn.microsoft.com/en-us/library/66k51h7a.aspx http://msdn.microsoft.com/en-us/library/vstudio/66k51h7a(v=vs.100).aspx http://sct.emu.edu.tr/courses/it/itec114/userfiles/files/spring2013_2014/lecture/113Class Review.pdf http://tidx.ru/html/fbede014-23bd-4ab1-8094-c8d9d9cb963a.htm http://www.amibroker.com/guide/keyword/switch.html http://www.scribd.com/doc/44115372/C http://www.senore.com/C-programming-switch-statements-q1244145 https://answers.yahoo.com/question/index?qid=20080806003834AAFSvMn http://onlinecprogram.blogspot.com/ http://onlinecprogram.blogspot.com/2013/02/the-switch-statement-in-c-language.html#! http://www.cs-tutorial.com/C-switch_case_statement-59 http://www.learnconline.com/2010/03/switch-statement-in-c-programming-language.html http://www.sonarmods.com/sonarmods/forum/index.php?topic=635.0 Execution of the statement body begins at the selected statement and proceeds until the end of the body or until a break statement transfers control out of the body. Comment by PlagScan: Possible sources: http://ilmumarta.blogspot.com/2013/02/menggunakan-switch-case.html http://www.c-source-code.blogspot.com/ http://programmingzeal.blogspot.com/2013/01/conditional-statement-switch.html http://gambets.blogspot.com/ http://msdn.microsoft.com/en-us/library/66k51h7a.aspx http://msdn.microsoft.com/en-us/library/vstudio/66k51h7a(v=vs.100).aspx http://sct.emu.edu.tr/courses/it/itec114/userfiles/files/spring2013_2014/lecture/113Class Review.pdf http://tidx.ru/html/fbede014-23bd-4ab1-8094-c8d9d9cb963a.htm http://www.amibroker.com/guide/keyword/switch.html http://www.scribd.com/doc/44115372/C http://www.senore.com/C-programming-switch-statements-q1244145 https://answers.yahoo.com/question/index?qid=20080806003834AAFSvMn http://onlinecprogram.blogspot.com/ http://onlinecprogram.blogspot.com/2013/02/the-switch-statement-in-c-language.html#! http://www.cs-tutorial.com/C-switch_case_statement-59 http://www.learnconline.com/2010/03/switch-statement-in-c-programming-language.html http://www.sonarmods.com/sonarmods/forum/index.php?topic=635.0 http://stackoverflow.com/questions/20020927/c-switch-statement-with-non-compound-statement-use-case |