Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,086 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,567 people online right now. Registration is fast and FREE... Join Now!




multiple if conditions

 
Reply to this topicStart new topic

multiple if conditions

techtoast
21 Apr, 2008 - 01:33 PM
Post #1

New D.I.C Head
*

Joined: 21 Apr, 2008
Posts: 2

i need to use a for loop and continue indentifier to create a program that counts to 20 skipping the odd numbers. my approach is to use an if statement to decide if the current number is odd and continue back to the for loop (skip printing odd number).

however, i am struggling to figure out how to nest the if statement, and how to write the sytax for multiple conditions (if number is equal to 1 OR 3 OR 5 etc...)

here is my code:
CODE
#include <iostream>
using namespace std;

int main()
{
    //variables
    int icount;
    for (icount = 1; icount < 21; icount++)
    {
        {
        if (icount == 1,3,5,7,9,11,13,15,17,19)
            continue;
    }
        cout << icount << endl;
    }
}



and i get a blank output.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Multiple If Conditions
21 Apr, 2008 - 01:36 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,791



Thanked: 91 times
Dream Kudos: 1200
My Contributions
cpp

if(statement)
//stuff
else if(statement)
//stuff
else if (statement)
//etc...


OR

cpp

if ((statement) || (statement) || (statement))
//etc


This post has been edited by KYA: 21 Apr, 2008 - 01:37 PM
User is online!Profile CardPM
+Quote Post

Martyr2
RE: Multiple If Conditions
21 Apr, 2008 - 01:40 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Better yet, check out the modulus operator (%) it will help you in this task tremendously!

even number % 2 = 0
odd number % 2 = 1

Test if it is 1 or 0 and then the magic world of modulus will open up to you. smile.gif
User is offlineProfile CardPM
+Quote Post

techtoast
RE: Multiple If Conditions
21 Apr, 2008 - 01:44 PM
Post #4

New D.I.C Head
*

Joined: 21 Apr, 2008
Posts: 2

aha! modulus, finding the remainder after a division. i didn't think of that. thank you so much!

updated code:
CODE

#include <iostream>
using namespace std;

int main()
{
    //variables
    int icount;
    int check;
    for (icount = 1; icount < 21; icount++)
    {
        check = icount %2;
        if (check == 1)
            continue;
        cout << icount << endl;
    }
    system ("PAUSE");
    return 0;
}


and here is my output:
CODE

2
4
6
8
10
12
14
16
18
20
press any key to continue...


works perfect! you guys are awsome!

This post has been edited by techtoast: 21 Apr, 2008 - 01:52 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:14PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month