M4 Assignment 2: The Structure of Statements: Translating If and And Statements

profileSS_Student
WatkinM_M4_A2.doc

Running head: STRUCTURED STATEMENTS 1

STRUCTURED STATEMENTS 2

The Structure of Statements: Translating If and And Statements

Marquita Watkins

HUM440 | Critical Thinking Common Sense and Everyday Life

Argosy University - Online

May 2, 2018

IF STRUCTURED STATEMENTS

Let us take an example in c-programming below which was claimed to be an if statement and analyse it:

// Program to display a number if user enters negative number

// If user enter positive number, that number won't be displayed

#include <stdio.h>

int main()

{

int number;

printf("Enter an integer: ");

scanf("%d", &number);

// Test expression is true if number is less than 0

if (number < 0)

{

printf("You entered %d.\n", number);

}

printf("The if statement is easy.");

return 0;

}

Output 1

Enter an integer: -2
You entered -2.
The if statement is easy.

Output 2

Enter an integer: 5
The if statement in C programming is easy.

Summary

This statement is used in programming decision making to specify the order in which statements are executed(Schach, 2007). The if statement evaluates the test expression inside the parenthesis and if its true(non-zero), the statement in the body of if is executed and if false (0), it is skipped from execution.

TRANSLATION

The program starts with an opening #include <stdio.h>,showing it’s the start of the program.It then proclaims that it only deals with integers therefore no fractions or decimals are accepted by the program int main().The program prompts the user to enter an integer and if it is less than 0,the statement ("You entered %d.\n", number, appears on the screen.Otherwise the statement "The if statement is easy.",appears on the screen.

EVALUATION

From output 1, when the user entered -2 as an integer,the program prompted -2 and “the if statement is easy”.since the integer was less than 0 as from the if condition,the results were true.

From output 2,when the user entered 5 as an integer,the program was skipped since it did not fulfil the if condition and therefore the result was false.

CONCLUSION

The above example is an if statement structured c program with the if statement flow and a condition in parenthesis.

AND STATEMENT

Let us look at the example below of an And statement in powershell as uded in programming to argue statements and conditions.

# PowerShell If AND Statement Simple Example Clear-Host $Calendar = Get-Date If ($Calendar.Day -eq '25' -And $Calendar.Month -eq '12') { "Christmas Day" } Else { "It's not Christmas yet! " + $Calendar.Month +"/" +$Calendar.Day }

Output 1

User input: 25

User input for month: 12

Output: Christmas day

Output 2:

User input: 23

User input for month: 12

Output: it’s not Christmas yet

SUMMARY

An and statement is used in an if statement to compare two or more conditions given and if the two set conditions are true a certain condition is fulfilled. If the conditions are false the program is skipped(Higham & Higham, 2016).

The given statements are mutually dependent and cannot be executed if each of the statement is not fulfilled.

TRANSLATION

The program assigns an integer to the character calendar. Calendar is equal to a date keyed in by the user as prompted. If the integer keyed in by the user as equated to calendar is 25 and if the calendar month is equal to 12, the result is true and therefore it’s a Christmas day. If the day of the calendar keyed in by the user is not equal to 25 and even if the calendar month is equal to 12 or not the result is not true therefore the program is skipped. Therefore, for the argument to be true in an and statement, the two condition statements must be fulfilled.

EVALUATION

Out put 1 shows a Christmas day. The user was prompted to enter an integer and 25 was it. After entering a month as 12,he/she had met all the conditions which were mutually dependent for condition and statement.therefore the result was true .

Output 2 shows it’s not christmas day . The user entered 23 as the calendar day and 12 as the calender month.since all the conditions were not met the result were false and the program was skipped.

Key symbols:

= : equal to

$;integer

%d : decimal value

References

Higham, D. J., & Higham, N. J. (2016). MATLAB guide (Vol. 150). Siam.

Schach, S. R. (2007). Object-oriented and classical software engineering (Vol. 6). New York: McGraw-Hill.