Jump to content


Photo

Lesson 5 - IF


  • Please log in to reply
11 replies to this topic

#1 Detail

Detail

    King Detail

  • Hosted
  • 7,767 posts
  • Location:Dayonic
  • Projects:Dayvi.com
  •  Blu Spy

Posted 19 December 2003 - 06:37 PM

Lesson 5

'IF' is a word which makes most of the world. It's also in C++.


If (expression)
ProgramStatement;

Note: There is no semicolon after the brackets.

The compiler evaluates the expression and if it's value is true ProgramStatment is executed, otherwise ProgramStatment is ignored.



If (expression)
ProgramStatement1;
Else
ProgramStatement2;

The compiler evaluates expression and if it's value is true ProgramStatement1 is executed, otherwise ProgramStatement2 is executed.



#pragma hdrstop
#include <iostream.h>
#include <conio.h>

int main()
{
  
  int Age;
  cout << "Please enter your age: ";
  cin >> Age;
  
  if(Age <= 21)
    cout << "You are young.";
  else
    cout << "You are old.";

  getch();

}
This code asks the user how old thay are, then insults them depending on there answer.

The code includes a "relational operator".
< is less than
> is greater/more than
>= is greater than or equal to
<= is less than or equal to
== is equal to
!= is not equal to

If the users answer is under 21 the program says "You are young", else is says "You are old".

Attached Images

  • ageC.gif


#2 Stino

Stino

    AKA Stink_o

  • Hosted
  • 1,421 posts
  • Location:the Netherlands
  • Projects:Blitzkrieg II:The Finest Hour (BlitzII:TFH)
  •  Doer of Things

Posted 19 December 2003 - 06:48 PM

ProgramStarment and ProgramStatment, i think you mean ProgramStatement
Posted Image

#3 Phoib

Phoib
  • Members
  • 29 posts
  • Projects:Blitz2, Blitz3, TW2

Posted 19 December 2003 - 11:04 PM

First real correction of Detail (Thanks to Olaf for confirming), in C++ its optional to use the curly braces.
If (expression)
ProgramStatement1;
Else
ProgramStatement2;
would become
If (expression) {
ProgramStatement1;
} Else{
ProgramStatement2;
}


#4 the_kid

the_kid

    L337 Pwnerer of N00bs

  • Project Team
  • 1,734 posts
  • Location:La$ Vega$, NV
  • Projects:freelancing between what interests me
  •  + TRIPLEhelix

Posted 20 December 2003 - 06:25 AM

i was trying something like that the other day, and I had the ";" after teh ()... and it wouldn't work... it drove me crazy... But hopefully I will get it working now...

This is a funny program, give people the test to see if they are old... :p

#5 Stino

Stino

    AKA Stink_o

  • Hosted
  • 1,421 posts
  • Location:the Netherlands
  • Projects:Blitzkrieg II:The Finest Hour (BlitzII:TFH)
  •  Doer of Things

Posted 20 December 2003 - 11:25 AM

Why do you have to use those {}?
Posted Image

#6 Detail

Detail

    King Detail

  • Hosted
  • 7,767 posts
  • Location:Dayonic
  • Projects:Dayvi.com
  •  Blu Spy

Posted 20 December 2003 - 02:28 PM

You dont have to, there optional.
Thay help to orginise the code into sections.

#7 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 20 December 2003 - 10:02 PM

To correct a correction, they are optional, but only for one line if statements. If you have more than one line in the if clause, you need to use the braces.
@ The_Kid: If you have that semicolon after the if, you create a condition that does nothing. It evaluates it, but finds no code, so your next line always gets executed, no matter what.
:p
Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |

#8 Eradicator

Eradicator
  • Members
  • 220 posts
  • Location:Canada

Posted 21 December 2003 - 12:49 AM

The { and } are equivalent to a begin and end in other languages. They signify where the code that is in something (if, case, function) starts and ends.

#9 the_kid

the_kid

    L337 Pwnerer of N00bs

  • Project Team
  • 1,734 posts
  • Location:La$ Vega$, NV
  • Projects:freelancing between what interests me
  •  + TRIPLEhelix

Posted 23 December 2003 - 04:43 AM

I can't get it to work for words...

i have

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{

  printf ("Testing stuff by: Michael Hedden\n\n\n\n");
  
 char rating;
  
  printf ("please tell me how well this works (good or bad): ");
  cin >> rating;
  
  if (rating==good)
  {
  
  cout << ("\n\nThank you for choosing Good.\n\n");
  }
  
  if (rating==bad)
  {
  printf ("\n\nReally... thats too bad\n\n");
  }
  
	system ("pause");
  return 0;
}

and it won't work...

#10 vice

vice
  • New Members
  • 6 posts

Posted 24 December 2003 - 01:30 AM

well, youre getting way over your head there pal. strings in C/C++ is a topic far too advanced for someone at your current level of ability.

however, i will just skim over the topic for you.

a string is basically an array of characters, or a bunch of letters put together. so in order to make a string you need to define an array of characters

char myString[256];
// this defines an array of characters, which can hold, at most, 256 letters

i'm sure arrays will be covered in upcoming lessons so i won't go into them at this moment.

getting strings from the user is also difficult for someone with your level of ability.
so for now just stick with the basic cout and cin with the basic (primitive) types i.e. int, double, boolean etc. don't worry about using printf just yet, you will just confuse yourself.

#11 Mastermind

Mastermind

    Server Technician

  • Undead
  • 7,014 posts
  • Location:Cambridge, MA
  • Projects:MasterNews 3
  •  The Man Behind the Curtain

Posted 24 December 2003 - 04:29 AM

Otherwise, there is also getline, but that isn't easy to use either. Time will allow you to add more skills. Be patient, student.
:p
Posted Image

Well, when it comes to writing an expository essay about counter-insurgent tactics, I'm of the old school. First you tell them how you're going to kill them. Then you kill them. Then you tell them how you just killed them.

Too cute! | Server Status: If you can read this, it's up |

#12 Banshee

Banshee

    One Vision, One Purpose!

  • Network Admins
  • 9,045 posts
  • Location:Rio De Janeiro, RJ, Brazil.
  • Projects:PPM, PPM: Final Dawn, OS SHP Builder, OS Palette Editor, OS W3D Viewer, VXLSE III, etc...
  •  Retired Network Leader
  • Division:Revora
  • Job:Maintenance Admin

Posted 21 April 2004 - 04:15 AM

First real correction of Detail (Thanks to Olaf for confirming), in C++ its optional to use the curly braces.

If (expression)<!--QuoteEBegin-->ProgramStatement1;<!--QuoteEBegin-->Else<!--QuoteEBegin-->ProgramStatement2;
would become
If (expression) {<!--QuoteEBegin-->ProgramStatement1;<!--QuoteEBegin-->} Else{<!--QuoteEBegin-->ProgramStatement2;<!--QuoteEBegin-->}

You need braces if you will execute more than one command. I.e.:

Using braces:

#include<stdio.h>

void main (void)
{

int a,b,temp;

// grab numbers from user:
printf("Type 2 numbers: (i.e.: 1,2)");
scanf("%d,%d",&a,&b);

if (a < b)
{
   temp = b;
   b = a;
   a = temp;
}

// output:
printf("1st: %d and 2nd: %d",a,b);

}

if you insert "1,2", the output will be "1st: 2 and 2nd: 1".
if you insert "2,1", the output will be "1st: 2 and 2nd: 1".

Now, the same code without braces in the if:


#include<stdio.h>

void main (void)
{

int a,b,temp;

// grab numbers from user:
printf("Type 2 numbers: (i.e.: 1,2)");
scanf("%d,%d",&a,&b);

if (a < b)
   temp = b;

b = a;
a = temp;

// output:
printf("1st: %d and 2nd: %d",a,b);

}

if you insert "1,2", the output will be "1st: 2 and 2nd: 1".
if you insert "2,1", the output will be "1st: <trash> and 2nd: 2".

Trash will be a random number (trash), because temp wasnt initialized. The line temp = b; only runs when b > a, however the other 2 lines ignores the if...
Project Perfect Mod

Command & Conquer Mods, Mods Support, Public Researchs, Map Archives, Tutorials, Tools, A Friendly Community and much more. Check it out now!

Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users