TRENDING NEWS

POPULAR NEWS

C Program - If Statements Help

Help with continue statement- c programming?

if (y==0) continue;
printf("%d\n", x/y);

C how to program! Writing statement ! Can you help me? :D plz?

Initialize the file “nameage.dat” so that there are 100 records with lastName = “unassigned”
firstName= “” and age = “0”.
Input 10 last names, first names and ages, and write then to the file.
Update a record: if there is no information in the record, tell the user “No info”
Delete a record that has information by reinitializing that particular record.

what do you think about this?...

please help me with this and other stuff, too... I hope someone can give me some explanation so I can learn something, too. I really appreciate for your valuable lesson, guys.

C programming if/else statements?

I put the dashed lines in to make this easier to read, delete them when you compile and replace them with spaces. Anyway, I haven't done C in a while, so I'm not sure about the specific functions, but looking at your logic, here's a simplification that should work great. && means AND and || means OR in C, C++, and Java. If you can't use those two operators (because the teacher won't let you use stuff you haven't gone over) then just do a nested if statements, ie:(else) if(condition 1) { if (condition 2){(stuff to do)}} for AND/&& statements and do if else statements for OR/||, ie: if(first test){stuff to do} else if(second test){stuff to do} else if(third test){stuff to do}

#include
int main(void)
{
---double num1, num2, num3;
---double hyp1, hyp2, hyp3, hypsum;

---printf("Please enter the length for side one: ");
---scanf("%lf", &num1);

---printf("Please enter the length for side two: ");
---scanf("%lf", &num2);

---printf("Please enter the length for side three: ");
---scanf("%lf", &num3);

---hyp1 = num1 * num1;
---hyp2 = num2 * num2;
---hyp3 = num3 * num3;
---hypsum = -1; // We can do this as the values of the side lengths squared are always positive

---if(hyp1 > hyp2 && hyp1 > hyp3)
---{
------hypSum = hyp2 + hyp3;
---}
---else if(hyp2 > hyp3 && hyp2 > hyp1)
---{
------hypSum = hyp3 + hyp1;
---}
---else if(hyp3 > hyp2 && hyp3 > hyp1)
---{
------hypSum = hyp2 + hyp1;
---}

---if(hypSum == hyp1 || hypSum == hyp2 || hypSum == hyp3)
---{
------printf("This is a right triangle.\n");
---}
---else
---{
------printf("This is NOT a right triangle.\n");
---}

---return 0;

}

Help with If Statement Programming c++?

include
using namespace std;

int main()
{
char s, t, Letter;
int base, height, length_S, Area1, Area2;

Area2 =((length_S * length_S)/ 2);
Area1 = (height * base);

cout << "Enter the type of figure (s or t): ";
cin >> Letter;
cout << endl;

if (Letter == s)
{
cout << "Enter the base: ";
cin >> base;
cout << "Enter the height: ";
cin >> height;
cout << "The area is " << Area1 << endl;

}

else if (Letter == t)
{
cout << "Enter the length of a side: ";
cin >> length_S;
cout << "The area is " << Area2 << endl;
}

system ("PAUSE");
return 0;
}

The assignment was this:
Write an interactive program that computes the area of a square (area = side2) or triangle (area = 1/2 * base * height) after prompting the user to type the first character of the figure name (t or s). Note that a square does not have a base and a height. It has only a side. Your variable names should reflect this.

Sample screen output 1:
Enter the type of figure (s or t): t
Enter the base: 4
Enter the height: 3
The area is 6

Sample screen output 2:
Enter the type of figure (s or t): s
Enter the length of a side: 8
The area is 64

Turn in your source code followed by 2 outputs, one reflecting each of the two sample screen outputs given above.

(I am a little unsure how to really do this because I need to see visually how to do all the if stuff correctly)

Need help with if statements (C++)?

I'm programming a guessing game. The program generates a random # and the user tries to guess what it is.

I'm trying to figure out how to set an "if" statement to true depending on how close the guess is to the actual #. (for example, have the "if" statement evaluate to true if the guess is 1-3 numbers lower than the actual number)

Thanks!

Help with Programming Again- Ending If Statements?

Hey Annie,

If you always use the following "if" format, you can't really go wrong.:

program

begin

If then
begin

end else begin

end;

end.

The very last "end." with a full stop marks the end of the program, and should be the only end followed by a full stop in the program.

Obviously, anything in <> is a place holder for your own code.

Hope this helps... :)

na

C programming switch statements

In computer programming, a switch statement is a type of control statement that exists in most modern imperative programming languages (e.g., C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution. In some other programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement or a select statement.

The following are simple examples, written in the various languages, that use switch statements to print one of several possible lines, depending on the value of an integer entered by the user. The lack of break keywords to cause fall through of program execution from one block to the next is used extensively. For example, if n=5, the third case statement will produce a match to the control variable. Since there are no statements following this line and no break keyword, execution continues through the 'case 7:' line and to the next line, which produces output. The break line after this causes the switch statement to conclude. If the user types in more than one digit, the default block is executed, producing an error message.

switch(n) {
case 0:
printf("You typed zero.\n");
break;
case 1:
case 9:
printf("n is a perfect square\n");
break;
case 3:
case 5:
case 7:
printf("n is a prime number\n");
break;
case 2:
printf("n is a prime number\n");
case 4:
case 6:
case 8:
printf("n is an even number\n");
break;
default:
printf("Only single-digit numbers are allowed\n");
break;
}

What is the use of switch statements in C programming?

Switch statements are used when you clearly know what are possible values for the condition variable. Each value in that list of possible value is called case. When the value given in input matches the value in the case statement, the block of code below case gets executed until it reaches the break statement.Break is optional. If break statement is not given, the next case statement (if any) will also get executed.for example, consider the following code:#include  int main (){  char grade = 'B';    switch(grade)   {    case 'A' :             printf("Excellent!\n" );              break;    case 'B' :              printf("Well done\n" );    case 'C' :               printf("Good\n" );               break;    case 'D' :               printf("You passed\n" );               break;    case 'F' :              printf("Better try again\n" );              break;    default :               printf("Invalid grade\n" );    }    printf("Your grade is  %c\n", grade );     return 0; }The output of the above program is :Well doneGoodYour grade is BThe value of the variable passed to the switch statement is checked against the case statements. Case 'B' matches. Hence the printf() executed. (Well Done - printed).As there is no break statement, the next case 'C' also executed. (Good - printed). As there is 'break' statement, the statement after the switch is executed. Therefore 'Your grade is B' - printed.An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions. Once the condition is true, other conditions are not validated.In switch case, mostly constant values are given in the condition while in if..else mostly boolean expressions are given. for example,#include int main (){ int a=10;int b=20;if (a>100)    {     printf("Value of a is greater than 100");    }elseif (b==20)    {     printf("Value of b is 20");     }elseif (a<100)    {     printf("Value of a is less than 100");    }else   {    printf("Condition fails");    }printf("Value of a is %d", a);return 0;}Output of above program is:Value of b is 20Value of a is 10The first if condition fails, so second if is checked.The condition is true, so the statements inside that if gets executed.The next if statements are not executed even if the condition is true.

What are the types of conditional statements in C programming?

Conditional statements helps to make decision based on certain condition. Condition are specified by a set of conditional statements have boolean expressions, its evaluted to a value true or false. These conditional statements are given below:1. if statement2. if-else statement3. If-else If ladder4. Nested if-else statement5. LOOP statement6. Switch statementConditional statements Syntax are given here:A) If statement:if(expression){// code to be executed}B) if-else statement :if(expression){//code to be executed if condition is true}else{//code to be executed if condition is false}C). If else-if ladder Statementif(condition 1){//code to be executed if condition 1 is true}else if (condition 2){//code to be executed if condition 2 is true}else if(condition 3){// code to be executed if conditon 3 is true}else{// code to be executed if all the conditions are false}2. Swicth Statementswitch(expression){case value1:// code to be executed;break; //optionalcase value2:// code to be executedcase value3 :// code to be executed;..........default :code to be executed if all cases are not matched;}3) Loops Statement sysntaxdo {//code to be executed}while(condition);while(condition){//code to be executed}4). for lood Syntaxfor(initialization;condition;incr/decr){//code to be executed;}

TRENDING NEWS