DronDram
DronDram

Reputation: 33

How to loop if statements in C

So i have this if statement

if (c[1] < c[0] && c[2] < c[1] && c[3] < c[2] ...)

I dont know exactly how many statements it will be, it can be from 1 to 8:

if (c[1] < c[0])

if (c[1] < c[0] && c[2] < c[1] && c[3] < c[2] ... c[8] < c [7])

And i need to automate this code, but I have no idea how

Here is what i tried to do:

int i = 0;
int n; // number of statements
while(i < n)
{
   if(c[i+1] < c[0])
      if(c[i+2] < c[i+1])
                ...
   i++;              
}

Ps. I can only use while loop

Upvotes: 2

Views: 206

Answers (5)

John Bode
John Bode

Reputation: 123458

Easy if not necessarily the most verbose way:

int i = 0;
while( ++i < n && c[i] < c[i-1] )
  ; // empty loop body

if ( i == n )
  // all tests passed
else
  // c[i] >= c[i-1]

The result of ++i is the current value of i plus 1, and as a side effect i is incremented. So this loops from i = 1..n-1.

Upvotes: 1

Randoragon
Randoragon

Reputation: 98

If your conditions are always written in a Conjunctive Normal Form (CNF) or Disjunctive Normal Form (DNF), then the logic remains the same if you add conditions incrementally:

CNF (a && b && c && ...)

int result = 1;
for (int i = 0; i < n - 1; i++) {
    result &= (c[i + 1] < c[i]);
}
if (result) {
    printf("True\n");
else {
    printf("False\n");
}

DNF (a || b || c || ...)

(this one does not solve the problem in the post, it's just for the sake of an example)

int result = 0;
for (int i = 0; i < n - 1; i++) {
    result |= (c[i + 1] < c[i]);
}
if (result) {
    printf("True\n");
else {
    printf("False\n");
}

You can also squeeze some extra efficiency by terminating the loops whenever false is found in CNF, or true is found in DNF (see short-circuit evaluation)

Upvotes: 2

MED LDN
MED LDN

Reputation: 669

You can make your condition in the loop while like this

int i=0;
while(i<(n-1)&&c[i]>c[i+1])
{
   i++;
}
if(i==(n-1))
{
     printf("\nTrue condition \n");
}
else
{
     printf("\nFalse condition \n");
}

Upvotes: 1

Ted Lyngmo
Ted Lyngmo

Reputation: 117178

In C, you could do something like this:

#include <stdbool.h>

/* ... */

    bool sorted = true;
    for(size_t i = 1; i < sizeof(c)/sizeof(c[0]); ++i) {
        if(!(c[i] < c[i-1])) {
            sorted = false;
            break;
        }
    }
    if(sorted) {
        /* ... */
    }

C++:

#include <algorithm>   // std::is_sorted
#include <iterator>    // std::begin, std::end
#include <type_traits> // std::remove_extent

//...

    if(std::is_sorted(std::begin(c), std::end(c),
                      std::greater<std::remove_extent<decltype(c)>::type>{})) {

    }

Upvotes: 1

Yusuf Şeng&#252;n
Yusuf Şeng&#252;n

Reputation: 284

You can declare a counter and increment it every true condition end of while you can check it is equals to the number of statements or not

    int i = 0;
    int n; // number of statements
    int counter = 0;
    while(i < n)
    {
       if(c[i+1] < c[i])
           counter++;
                    ...
       i++;              
    }
    if(counter == n) // this will be your true condition
    else // then it false

Upvotes: 4

Related Questions