ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

Embed Size (px)

Citation preview

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    1/16

    ASSIGNMENT : C++ PROGRAMMING

    1. 

    Who is Written C++ ?

    STROUSTRUP began developing C++ in 1978 (then called “o with Classes”), and in his own words, “inventedC

    C++, wrote its early definitions, and produced its first implementation.. Chose and formulated the design

    criteria for C++, designed all its major facilities, and was responsible for the processing of extensionproposals in the C++ standards committee. “Stroustrup also wrote a textbook for the language, The C++

    Programming Language.

    STROUSTRUP was the head of Large-scale Programming Research department, from its creationAT&T Bell Labs’ 

    until late 2002. Stroustrup was elected member of the in 2004. He is a Fellow ofNational Academy of Engineering

    the (1994) and an I Fellow. He works at as a Distinguished Professor where he holdsACM EEE Texas A&M University

    the College of Engineering Endowed Chair in . He is also a visiting faculty in Computer ScienceComputer Science

    Department at Columbia University. ITMO University noble doctor since 2013.

    In 2015, he was made a Fellow of the for his invention of the C++ programmingComputer History Museum

    language.

    2. 

    State statements below and give an example application in C++ Program.

    a.  C++ go to Statement

    o  In C++ programming, go to statement is used for altering the normal sequence of program execution by

    transferring control to some other part of the program.

    o  SYNTAX OF GO TO STATEMENT

    Go to label ;

    ….. ….. …..

    ….. ….. …..….. …… ….

    label  :

    statement ;

    ….. ….. …..

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    2/16

     

    In syntax above, label  is an identifier. When go to label ; I encountered, the control of program jumps to label : and executes the code below it.

    FORWARD JUMP BACKWARD JUMP

    Example 1 : GO TO STATEMENT

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    3/16

    OUTPUT

    You can write any C++ program with use of go to statement and it is generally considered good idea not to use go to statement

    Reason to Avoid go to Statement

    o  The go to statement gives power to jump to any part of program but, makes the logic of the program complex and tangled. In modern

    programming, go to statement is considered a harmful construct and a bad programming practice.

    The go to statement can be replaced in most of C++ program with the use of break and continue statements.

    b.  WHILE

    o  While (condition ) {Code to execute while the condition is true } The true represents a Boolean

    expression which could be x== 1 or while (x !=7) (x does not equal 7). It can be any combination of

    Boolean statements that are legal. Even, (while x==5 || v ==7) which says execute the code while x

    equals five or while v equals 7. Notice that a while loop is the same a a for loop without the initialization

    and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.

    Example :c). Break and Continue It causes the execution flow to jump around and because it can make

    the flow of logic harder to follow Example :

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    4/16

     

    c.  C++ break and continue Statement

    o  There are two statements (break ; and continue;) built in C++ programming to alter the normal flow of

    program.

    Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to

    skip some statement/s inside loop or terminate the loop immediately with checking test condition. On

    these type of scenarios, continue; statement and break ; statement is used respectively. The break ;

    statement is also used to terminate switch statement.

    C++ break Statement

    o  The break; statement terminates the loop (for, while, and do.. while loop) and switch statement

    immediately when it appears.

    Syntax of break

    In real practice, break statement is a lmost always used inside the body of conditional statement(if …else) inside the loop.

    Break;

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    5/16

     

    Working of break Statement

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    6/16

    Example 1: C++ break

    C++ program to add all number entered by user until user enters 0.

    Output

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    7/16

    o  In this C++ program, the test expression is always true. The user is asked to enter anumber which is

    stored in variable number . If the user enters any number other than 0,that number is added to Sum and

    stored to it and again user is asked to enter anothernumber. When user enters 0, the test expression

    inside if statement is false and body of  else  is executed which terminates the loop. Finally, the sum is

    displayed.

    C++ continue Statement 

    It is sometimes necessary to skip some statement/s inside the loop. In that case, continue; statement is used in

    C++ programming.

    Syntax of continue

    In practice, continue; statement is almost always used inside conditional statement.

    Working of continue Statement

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    8/16

     

    Example 2 : C++ continue

    C++ program to display integer from 1 to 10 except 6 and 9.

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    9/16

    d.  While True

    o  I'm curious about using a while statement with a true condition. What is theadvantage of using a

    while(true) statement with break to exit the while statementover something like a for loop? I guess I'm

     just curious when a good time to usethis technique would be? I saw it demonstrated in a quick sort

    algorithm at schooltoday and was just curious about it. Insight? Example : e) Do \ White Thedo...while

    Loop is similar to while loop with one very important difference. Inwhile loop, check expression ischecked at first before body of loop but in case ofdo...while loop, body of loop is executed first then

    only test expression ischecked. That's why the body of do...while loop is executed at least

    once. Example :

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    10/16

    e.  Do while statements

    o  One interesting thing about the while loop is that if the loop condition is initially false, thewhile loop will

    not execute at all. It is sometimes the case that we know we want a loop toexecute at least once, such

    as when displaying a menu. To help facilitate this, C++ offersthe do-while loop: 

    o  The statement in a do-while loop always executes at least once. After the statement has been executed,

    the do-while loop checks the condition. If the condition is true, the CPU jumps back to the top of the do-

    while loop and executes it again. Here is an example of using a do-while loop to display a menu to the

    user and wait for theuser to make a valid choice:

    o  One interesting thing about the above example is that the selection variable must bedeclared outside of the do block. Why do you think that is?

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    11/16

    o  If the selection variable were to be declared inside the do block, it would be destroyed when the do

    block terminates, which happens before the while conditional is executed. But we need the variable to

    use in the while conditional -- consequently, the selection variable must be declared outside the do

    block.

    o  Generally it is good form to use a do-while loop instead of a while loop when you intentionally want the

    loop to execute at least once, as it makes this assumption explicit –however, it’s not that big of a deal either way. 

    f.  Jump \ Loop

    o  Cause a certain piece of program to be executed a certain number of times. Consider these

    scenarios:

    -You want to execute some code/s certain number of time.

    -You want to execute some code/s certain number of times depending upon input from user.

    Example : 

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    12/16

    Output :

    g.  C++ if/else statement

    o  An if statement can be followed by an optional else statement, which executes when the boolean

    expression is false.

    Syntax:

    o  The syntax of an if...else statement in C++ is:

    If the boolean expression evaluates to true, then the if block of code will be executed,otherwise else block of code will

    be executed.

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    13/16

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    14/16

    The if...else if...else Statement: 

    o  An if statement can be followed by an optional else if...else statement, which is very use full to test

    various conditions using single if...else if statement.

    When using if , else if , else statements there are few points to keep in mind.

    -An if can have zero or one else's and it must come after any else if's.

    - An if can have zero to many else if's and they must come before the else.

    - Once an else if succeeds, none of he remaining else if's or else's will be tested.

    Syntax:

    o  The syntax of an if...else if...else statement in C++ is:

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    15/16

    Example : 

  • 8/17/2019 ASSIGNMENT C++ NUR SYUHADA NABILA BINTI NOR AZMI

    16/16

      When the above code is compiled and executed, it produces the following result: